What does that means?
"Since the client does not contain references to the implementation class names, it is not tied down to a particular implementation. This allows the client to use another implementation even at run time."
Can you guys explain in a different way? Maybe with a example. This really confuses me.
Tnx!
About Question enthuware.ocpjp.v7.2.1183 :
Moderator: admin
-
- Posts: 28
- Joined: Wed Feb 25, 2015 9:03 am
- Contact:
Online
-
- Site Admin
- Posts: 10067
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1183 :
In factory pattern, the return type of the factory method is an interface and not a class. So you can change the code in the factory method to return an instance of a different class. For example -
You can read more about it here: http://www.oodesign.com/factory-pattern.html
Code: Select all
class WriterFactory{
public static Writer getWriter(){ //Assume that Writer is an interface.
{
return new FileWriter(); //or new URLWriter();
}
}
If you like our products and services, please help us by posting your review here.
-
- Posts: 28
- Joined: Wed Feb 25, 2015 9:03 am
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1183 :
thanks in advance, sir.
-
- Posts: 17
- Joined: Wed Nov 26, 2014 1:59 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1183 :
In this question I couldn't see why the item:
"It eliminates the need for overloaded constructors"
was not considered correct.
"It eliminates the need for overloaded constructors"
was not considered correct.
Online
-
- Site Admin
- Posts: 10067
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1183 :
Because it doesn't eliminate the need for overloaded constructors. The factory pattern doesn't affect how the object is constructed. It only moves the responsibility of creating an object in multiple classes to just one class. Instead of multiple classes instantiating the class, they request the factory class to get an instance. The factory then instanciates the class using whichever constructor is appropriate. If the class to be instantiated in multiple ways, it will have overloaded constructors, and the factory class cannot do anything about it.
HTH,
Paul.
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 97
- Joined: Wed Dec 28, 2016 9:00 am
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1183 :
I think the option of eliminating the need of overloaded constructors is also correct because I read in the book Core Java Volume 1 as follows:
The NumberFormat class uses factory methods that yield formatter objects for various styles.
Why doesn’t the NumberFormat class use a constructor instead? There are two reasons:
• You can’t give names to constructors. The constructor name is always the same as the class name. But we want two different names to get the currency instance and the percent instance.
• When you use a constructor, you can’t vary the type of the constructed object. But the factory methods actually return objects of the class DecimalFormat, a subclass that inherits from NumberFormat.
The above text is taken from the book so this states that overloaded constructors can be avoided using factory pattern.
The NumberFormat class uses factory methods that yield formatter objects for various styles.
Code: Select all
NumberFormat currencyFormatter = NumberFormat.getCurrencyInstance();
NumberFormat percentFormatter = NumberFormat.getPercentInstance();
double x = 0.1;
System.out.println(currencyFormatter.format(x)); // prints $0.10
System.out.println(percentFormatter.format(x)); // prints 10%
• You can’t give names to constructors. The constructor name is always the same as the class name. But we want two different names to get the currency instance and the percent instance.
• When you use a constructor, you can’t vary the type of the constructed object. But the factory methods actually return objects of the class DecimalFormat, a subclass that inherits from NumberFormat.
The above text is taken from the book so this states that overloaded constructors can be avoided using factory pattern.
Who is online
Users browsing this forum: admin and 224 guests