About Question enthuware.ocpjp.v7.2.1183 :

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
ThufirHawat
Posts: 28
Joined: Wed Feb 25, 2015 9:03 am
Contact:

About Question enthuware.ocpjp.v7.2.1183 :

Post 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!

Online
admin
Site Admin
Posts: 10067
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1183 :

Post 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
If you like our products and services, please help us by posting your review here.

ThufirHawat
Posts: 28
Joined: Wed Feb 25, 2015 9:03 am
Contact:

Re: About Question enthuware.ocpjp.v7.2.1183 :

Post by ThufirHawat »

thanks in advance, sir.

leorbarbosa
Posts: 17
Joined: Wed Nov 26, 2014 1:59 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1183 :

Post by leorbarbosa »

In this question I couldn't see why the item:

"It eliminates the need for overloaded constructors"

was not considered correct.

Online
admin
Site Admin
Posts: 10067
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1183 :

Post 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.
If you like our products and services, please help us by posting your review here.

jagoneye
Posts: 97
Joined: Wed Dec 28, 2016 9:00 am
Contact:

Re: About Question enthuware.ocpjp.v7.2.1183 :

Post 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.

Post Reply

Who is online

Users browsing this forum: admin and 224 guests