Factory Pattern - Abstract or Interface
When using the Simple Factory Pattern to hide the creation of the concrete object, should the abstraction class be an interface or an abstract class...or does it matter. I have seen books say "The Factory Method Pattern defines an interface for creating an object"...Head First Design Patterns. But the J2EE DAO pattern description says the that the class can be an abstract like the DAOFactory in http://java.sun.com/blueprints/corej2eepatterns/Patterns/DataAccessObject.html
Thanks,
jay
[507 byte] By [
jaybyteza] at [2007-10-2 7:52:11]

either one will work, but the interface will be more flexible.
understand why?
java only allows single inheritance of implementation, but multiple inheritance of interfaces.
if your factory returns an abstract type, only classes that extend your abstract type can be returned. not everyone will want your abstract default implementation, so the best thing to do is return the interface type.
as an example, look at java.util Collections API. Look at the methods that return List (interface), not AbtractList (abstract class).
%
Do you really need that level of encapsulation/indirection? I agree with Duffy that if the requirement is present, all things being equal, I would use the interface over the abstract class. However, is your requirement valid? Or is this pattern-fever?- Saish
Saisha at 2007-7-16 21:40:29 >
