Page 1 of 1

About Question enthuware.ocpjp.v7.2.1185 :

Posted: Mon Aug 19, 2013 3:36 pm
by arnoldnitesh
this is not complete answer, why DAO and composite is not program to interface ? even composite pattern encourage program to interface.

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

Posted: Mon Aug 19, 2013 7:45 pm
by admin
The question is not which pattern encourages "program to an interface" principle.
This concept is not integral to DAO and composite patterns.

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

Posted: Tue Aug 20, 2013 3:03 am
by arnoldnitesh
I have read in book and it's part of DAO and Composite.They provide a common interface for client so client doesn't need to think about implementation class detail.In composite pattern component is a interface which is used by client.client doesnt need to think about leaf class or composite class.In DAO pattern client doesnt need to think about data source because DAO provides a interface.

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

Posted: Tue Aug 20, 2013 6:38 am
by admin
Yes, the main purpose of DAO is to shield the db access. You can do it without using interfaces also and it would still be DAO. If you provide an interface to DAO, you are using the "program to an interface" principle also but that is not required for DAO. Same for composite.

Factory pattern however cannot be done without interfaces.

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

Posted: Tue Aug 20, 2013 6:42 am
by arnoldnitesh
Even in factory pattern interface is not necessary, you can do thru abstract class also.

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

Posted: Tue Aug 20, 2013 9:49 am
by admin
Even with abstract class, you are relying the the "program to an interface" principle in the sense that you are using the interface (conceptually, i.e.) provided by the abstract class. That is the key point here. Program to an interface doesn't necessarily mean java interface definition but the concept that that implementation can change as long as the conceptual interface remains the same. Whether you do it with abstract class or interface is a language specific thing.

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

Posted: Tue Aug 20, 2013 10:10 am
by arnoldnitesh
You said
Yes, the main purpose of DAO is to shield the db access. You can do it without using interfaces also and it would still be DAO.
Please tell me without interface how can i do this for DAO?

if your answer is abstract class then as you said below,DAO also ultimately relying on interface.
Even with abstract class, you are relying the the "program to an interface" principle in the sense that you are using the interface (conceptually, i.e.) provided by the abstract class.
your explanation is like round robin.

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

Posted: Tue Aug 20, 2013 10:37 am
by admin
No, an interface is not critical for DAO. You can have a concrete class that has methods to do persistence. The user of the class can just do a new on that concrete class. Again, creating an interface for the DAO and then creating a class that implements that interface would be better, but not related to DAO as such. That is exactly what Factory pattern is.

You are confusing a combination of factory pattern + DAO with DAO.

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

Posted: Tue Aug 20, 2013 10:41 am
by admin

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

Posted: Tue Oct 08, 2013 2:30 pm
by Student
"Since the return type of the factory method is an interface"

Is that always true? I.e. is that a definite requirement of the Factory pattern? I thought the Factory pattern was everywhere and that it just meant you get an X without having to invoke X's constructor eg

http://docs.oracle.com/javase/7/docs/ap ... teInstance()

DF isn't an interface; it's a class. DF's getXXX methods are supposed to be Factory methods aren't they?
Cheers

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

Posted: Tue Oct 08, 2013 2:39 pm
by admin
It is a just a pattern, not a hard and fast rule. So variations are possible. But conceptually, yes, the return type of the method should be an interface and not a class.

HTH,
Paul.

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

Posted: Fri Jan 30, 2015 8:21 pm
by ewebxml
The Factory pattern cannot be created or applied without interfaces.

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

Posted: Mon Feb 02, 2015 10:21 pm
by admin
ewebxml wrote:The Factory pattern cannot be created or applied without interfaces.
That is not completely true. Factory pattern can be used with abstract classes also. In fact, it is used with regular classes as well in many cases. For example, in Java 8, LocalDate/LocalDateTime classes have several instance creator methods. These are basically static factory methods.
But yes, the classic factory pattern involves interfaces.

HTH,
Paul.