Click here to Skip to main content
15,888,527 members
Please Sign up or sign in to vote.
2.92/5 (3 votes)
See more:
Hell Every One,

I have gone through many articles/blog regarding Factory Method Pattern, some of them are :-
dofactory, stackoverflow , sample and many others.
Still i am not getting a very clear picture of the this pattern, may be because i recently started learning design pattern.

Correct me if i am wrong for below points
1. We have to have a factory method which will be taking care of creating new objects.
2. We will have a interface/ abstract class for our object type i.e. Product.
3. This product class should be further sub classed as concrete productA and concrete ProductB.
4. FactoryMethode function can be implemented in different classes based on a parent class Creator. i.e. ConcreteCreatorA and ConcreteCreatorB.

We can call our code from client side as given in dofactory.Basically more or less i am referring dofactory. now above all points are necessary for this pattern or we can skip one or more?

Thanks
Girish
Posted

1 solution

Let's see:
  1. Not "have to have", you just can have it. All patterns don't dictate anything mandatory. This is just a recommended pattern suitable for a class of typical similar problems. Yes, the factory method creates some object.
  2. Yes, you will, but in what sense do you "have" it? You need to see the difference between compile-time type and runtime type, which is a part of the heart of OOP. When you call a factory method, you always get an instance of some non-abstract class or structure (yes, structure, too, if the method return type is some interface, because structures can implement interfaces, too). After all, there are no instances of interfaces or abstract classes. But the compile-time type (which is appear in the declaration of the factory method) is abstract. Or, strictly-speaking, it can be pseudo-abstract (non-abstract type which is primarily used for exact same purpose as the abstract class, as a base class for some set of derived classes, but is formally non-abstract and can be instantiated).
  3. Nothing is "further sub-classed". See the previous item. The method, as always, returns instance of the class, not class. An instance cannot be sub-classes, "further" or not. Again, there is a base class or interface which is used as a return type of the factory method. Implementation of the factory class can have one or more type based on this interface or a base class, implementing the interface or inheriting the base class. The call returns the instance of one of those derived types. The interface of the type implementing the factory method does not specify what runtime type will be returned; this is decided by its implementation. In other words, the caller "does not know" the concrete runtime type returned; and this knowledge is not needed. This is the essence of OOP: the runtime type "knows what to do", not its user.
  4. Yes, it is of course possible. This way, you can have double hierarchy of the types: the hierarchy of the types returned by the factory method, and the hierarchy of the factories.

    However, this is not the essence of the pattern and not mandatory. Moreover, should you try to create parallel hierarchy, you would defeat the purpose of the factory. Say, if you plan to use ConcreteCreatorA for creation of instances of type A, and ConcreteCreatorB for creation of instances of type B, it would totally defeat the purpose of the pattern, because its would be nothing better then creating instances of A and B directly.


Unfortunately, none of these questions helps to understand the essence and the use of the pattern, they all miss the goal. The whole approach to getting some help through such question is not very productive (but perhaps not completely useless): you are trying to guess something which you don't really understand and ask for confirmation. This is like using adjustment fire with the gun which is already well adjusted (documentation is available, like already adjusted iron sights, you just need to understand it / view through it).

The essence of the patten is well explained.
Wikipedia explains, through the words by the Gang Of Four:
The essence of this pattern is to "Define an interface for creating an object, but let the classes that implement the interface decide which class to instantiate. The Factory method lets a class defer instantiation to subclasses."
That's why the picture (first on on the page references above) shows just two members of the Creator hierarchy. The concrete implementation type ConcreteCreator creates instances of the Product type for all of the concrete types derived from Product. It allows the user of the Creator to remain agnostic of the concrete runtime types of the returned objects.

—SA
 
Share this answer
 
v5
Comments
girishmeena 29-Mar-14 10:27am    
Thanks for taking your time and explaining things here.
Sergey Alexandrovich Kryukov 29-Mar-14 21:41pm    
You are very welcome.
Good luck, call again.
—SA
girishmeena 29-Mar-14 10:31am    
But are you saying that the example provided in dofactory is incorrect. it is contradicting #4 though.
Sergey Alexandrovich Kryukov 29-Mar-14 21:44pm    
I don't know, failed to load the page. #4 in my description is correct; it's two-fold. You need to understand the purpose of the patter, then you can see what makes sense and what not. "Correct" is not necessary a valid criterion. Code can be correct in certain sense but make no practical sense. Patterns are oriented to practical applicability.
—SA
Joezer BH 30-Mar-14 5:58am    
Posh answer 5+

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900