Questions about Factory Pattern

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?

+7
source share
3 answers

Your understanding of Factory patterns and Abstract Factory patterns is correct.

When people create classes whose sole responsibility is to create other objects, they naturally call them Factory, of course. This in itself is not unreasonable. The problem is that there is no Factory pattern .

Confusion arises here for two reasons:

  • Some developers simply want to add another template and claim to use the Factory template, referring to an object that others create.

  • Developers who study design patterns see that the class is called Factory, regardless of whether the pattern is implemented, and suppose it should be a Factory method or an abstract Factory. This is confusing because you are trying to figure out what it is by questioning your own understanding of real models.

Remember that not only solutions to design patterns are common, but also serve to create a language for discussing design. In this case, the design language you expect is not the one that the developer actually used. What they do is wrong if they say they use a specific design template.

+7
source

what do they mean by a family of dependent or related objects

Using an example from Design Patterns from Gang Four:

  • AbstractFactory ( WidgetFactory )
  • ConcreteFactory ( MotifWidgetFactory , PMWidgetFactory )
  • AbstractProduct ( Window , ScrollBar )
  • ConcreteProduct ( MotifWindow , MotifScrollBar , PMWindow , PMScrollBar )

public abstract class WidgetFactory {...}

public class MotifWidgetFactory extends WidgetFactory {...}

public class PMWidgetFactory extends WidgetFactory {...}

Let's start with MotifWidgetFactory . It will produce a family of specific products that expand or market abstract products. Since they are all built by the same factory, they play well together. You cannot make PMScrollBar work with MotifWindow .

What a person does by the name of the factory template below ... he looks like a utility class in which we pass a class and get an instance of the object.

Your example is a factory in which it creates an object. In this case, extract the singlet from Map . It does not match the "Factory Method" or "Abstract Factory" patterns and therefore is only the factory in the name.

+2
source

just watching the java tag related to this question. Although this book is related to Java, it is โ€œloosely coupled,โ€ like GOF design patterns, to most OO languages.

Thus, design may be relevant to other languages.

0
source

All Articles