Note that there are some explanatory texts on larger screens.

plurals
  1. POQuestions regarding the Factory pattern
    text
    copied!<p>Many people say they are using factory pattern in their project. But when i actually see their implementation it looks totally different from definition what i have read in head first book. In the book they have described two kind of factory pattern i.e</p> <blockquote> <p><strong>Factory Method</strong>:- A class specifies its sub-classes to specify which objects to create based on some parameter. So we expect here some abstract method in base class whihich will be implemented by child class and pupose of that will be to create some object</p> <p><strong>Abstract Factory</strong>:- Provides an factory (in form of interface or abstract factory)for creating families of related or dependent objects without specifying their concrete classes.</p> </blockquote> <p><strong>I have a question here on what do they mean by family of dependent or related objects.</strong> Lets refer to <a href="http://www.apwebco.com/gofpatterns/creational/AbstractFactory.html" rel="nofollow">http://www.apwebco.com/gofpatterns/creational/AbstractFactory.html</a>. As per my understanding it means that in <code>FinancialToolsFactory</code> (in the link) is able to create <code>TaxProcessor</code> which is a family of products where actuall concreate products are <code>CanadaTaxProcessor</code> and <code>EuropeTaxProcessor</code> . So here we will have <code>n</code> number of concrete factories(in this case <code>CanadaFinancialToolsFactory</code> and <code>EuropeFinancialToolsFactory</code>) which will be extending/implementing abstract factory in this case <code>FinancialToolsFactory</code>.</p> <p><strong>Please let me know if above understanding is correct as i think its the crux of Factory pattern.</strong></p> <p>Second question:</p> <p>What people are doing on the name of factory pattern is below:</p> <pre><code>public class MyFactory { public static &lt;T&gt; T getObject(Class&lt;T&gt; cls) { if (cls == null) { throw new IllegalArgumentException("Invalid className"); } T daoObject = (T)map.get(cls); if (daoObject == null) { daoObject = loadObject(cls); } return daoObject; } } </code></pre> <p>They are just passing class like Example.class from main method and getting the object instance for that specific class. Now if we go by actual concept of factory pattern which is described in beginning(from head first book) and other websites it does not follow any of the two factory patterns. To me it looks like a utility class where we are passing the class and getting the object instance. <strong>Please let me know if you folks agree with this?</strong></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload