The other day we had a discussion related to the abstract factory and let me tell you, flames flew and it was high on melodrama.
Anyways coming back to the main point, the root of whole discussion was,
A: Why should I use the abstract factory, just for a parallel set of classes, what am I getting out of it ?
B: You are abstracting your product, that's why you are creating it. Apart from that, its a you have a mechanism for creating related set of products, which is important here, you see you should not forget that, its for creating a set of related family of products.
C: But say if you are just creating is two set of components, a UI Components for rich mode and the simple mode, could you not do with a static Factory implementation instead of so much of fun fare? And also if you look closely, what you are doing is essentially now binding your classes to the implementation of the Abstract Factories some where, because to get the code running you have to write the Implementation class some where, so would not it be better if we had just a static factory class which gives us what we need.
B: hmmmm...
A: Apart from that so much of code to manage .... That's a good one C:)
C: Thanks! Man
B: Factory Method defines an interface for creating an object, but lets subclasses decide which of those to instantiate. A factory method lets classes defer instantiation to subclasses.By contrast, an Abstract Factory provides an interface for creating families of related or dependent objects without specifying their concrete classes.
C: We have a family of related objects but, ... I don't want to take a purists view or go by book view.. I look at convinience and unless you tell me why, I don't see any reason why I should not go for a Factory than a Abstract Factory here.
A: And if you ask me, I guess you are over complicating, is not this kind of an YAGNI.
B: Well, let me examine the definitions a bit more:
Factory Method defines an interface for creating an object, but lets subclasses decide which of those to instantiate. A factory method lets classes defer instantiation to subclasses.
Which essentially means, in this case the decision is taken by sub class -> Inheritance is in action.
Abstract Factory provides an interface for creating families of related or dependent objects without specifying their concrete classes. Now in this case the job of creating the instance of the products are delegated -> Composition in Action.
And as you guys already know Composition allows you to delay the creation of back-end objects until (and unless) they are needed, as well as changing the back-end objects dynamically throughout the lifetime of the front-end object. With inheritance, you get the image of the superclass in your subclass object image as soon as the subclass is created, and it remains part of the subclass object throughout the lifetime of the subclass.
So I would say, if you need it dynamically vary, take the composition root and enjoy the benefits of Abstarct Factory else, Factory should be just fine for you.
C: hmmm..
A: ok
Hope you like the dialogue....:)