Page 1 of 1
About Question enthuware.ocpjp.v7.2.1183 :
Posted: Fri Apr 03, 2015 1:20 am
by ThufirHawat
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!
Re: About Question enthuware.ocpjp.v7.2.1183 :
Posted: Fri Apr 03, 2015 2:05 am
by admin
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 -
Code: Select all
class WriterFactory{
public static Writer getWriter(){ //Assume that Writer is an interface.
{
return new FileWriter(); //or new URLWriter();
}
}
You can read more about it here:
http://www.oodesign.com/factory-pattern.html
Re: About Question enthuware.ocpjp.v7.2.1183 :
Posted: Sat Apr 04, 2015 12:06 am
by ThufirHawat
thanks in advance, sir.
Re: About Question enthuware.ocpjp.v7.2.1183 :
Posted: Wed Jun 17, 2015 6:04 pm
by leorbarbosa
In this question I couldn't see why the item:
"It eliminates the need for overloaded constructors"
was not considered correct.
Re: About Question enthuware.ocpjp.v7.2.1183 :
Posted: Wed Jun 17, 2015 8:47 pm
by admin
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.
Re: About Question enthuware.ocpjp.v7.2.1183 :
Posted: Fri Dec 30, 2016 3:31 pm
by jagoneye
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.
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%
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.