Many say that they use the factory pattern in their project. But when I really see their implementation, it looks completely different from the definition that I read in the first book. The book describes two types of factory ie
Factory Method : - the class indicates its subclasses to indicate which objects to create based on some parameter. Therefore, we expect here some abstract method in the base class that will be implemented by the child class and the navel of this will be to create some object
Abstract Factory : - Provides a factory (in the form of an interface or abstract factory) for creating families of related or dependent objects without specifying their specific classes.
I have a question about what they mean by a family of dependent or related objects. Let's look at http://www.apwebco.com/gofpatterns/creational/AbstractFactory.html . In my understanding, this means that in FinancialToolsFactory (by reference) you can create a TaxProcessor , which is a family of products in which CanadaTaxProcessor and EuropeTaxProcessor specify products. So, here we will have n number of specific plants (in this case CanadaFinancialToolsFactory and EuropeFinancialToolsFactory ), which will expand / implement abstract factories in this case FinancialToolsFactory .
Please let me know if the correct understanding is correct, as I consider it the essence of the factory pattern.
Second question:
What a person does by the name of the factory template below:
public class MyFactory { public static <T> T getObject(Class<T> cls) { if (cls == null) { throw new IllegalArgumentException("Invalid className"); } T daoObject = (T)map.get(cls); if (daoObject == null) { daoObject = loadObject(cls); } return daoObject; } }
They simply pass a class, such as Example.class, from the main method and get an instance of the object for this particular class. Now, if we follow the actual concept of the factory template, which is described at the beginning (from the first chapter of the book) and other websites, it does not follow either of the two factory templates. For me, it looks like a utility class in which we pass the class and get an instance of the object. Please let me know if you agree with this person?
M sach
source share