Understanding the factory method template

I am reading about the Factory method template.

I can understand when there is one Factory class, i.e. StoreFactory#getStore() , which returns a Store implementation based on some runtime or other state.

But starting from reading (for example, this link ), there seems to be a general model of people creating an abstract Factory class so that other Factory classes extend:

  public abstract class AbstractFactory { public abstract Store getStore(int store); } public class StoreFactoryA extends AbstractFactory { public Store getStore(int Store) { if(Store == 1) { return new MyStoreImplA(); } if(Store == 2) { return new MyStoreImplB(); } } } public class StoreFactoryB extends AbstractFactory { public Store getStore(int Store) { if(Store == 1) { return new MyStoreImplC(); } if(Store == 2) { return new MyStoreImplD(); } } } public class Runner { public static void main(String[] args) { AbstractFactory storeFactory = new StoreFactoryA(); Store myStore = storeFactory.getStore(1); } } 

My example is contrived, but it models the above link.

This implementation seems to me a chicken egg. Use the Factory method template to eliminate the need for client code to indicate the type of class, but now client code must selectively select the correct Factory to use, i.e. StoreFactoryA , StoreFactoryB ?

What is the reason for using an abstract class here?

+4
source share
4 answers

The link you are reading, unfortunately, does not provide a realistic example of a template. In fact, according to the original GoF design patterns, this pattern is called the Factory Summary (the Factory method is another pattern).

The Factory abstract pattern is used when you have factories that can create a family of objects . for example, you can have AbstractGUIFactory , which can have the createButton(), createWindow(), createTitleBar , etc. And then you will have specific plants such as WindowsGUIFactory, MacGUIFactory, MotifGUIFactory , etc., Each of which will create Button, Window, TitleBar in its own way.

Factory will be installed on one implementation at some point in the application (possibly using the configuration), and then Factory will be used wherever objects need to be created.

If you study design patterns, it is best to start with the classic GoF book.

+4
source

The pattern becomes interesting, especially when it comes to testing. Now you can introduce AbstractFactory into the class and select different types for different environments or tests without having to change the classes (if the factory creates types that have common interfaces).

The classes used should not depend on the concrete implementation of the factory and should not depend on real implementations of the created types.

0
source

When using the Factory method, the method is still in the object, and you need to instantiate the correct object to access the method. The only abstraction you achieve is because of the abstract class.

The factory method template is designed this way. You might have expected this to be something like an abstract Factory pattern that has a better abstraction than the Factory method.

http://c2.com/cgi/wiki?AbstractFactoryVsFactoryMethod

0
source

You get extensibility and decoupling through the inversion of the control, but at the same time allow this object to be a field of control over the actual process and time.

0
source

Source: https://habr.com/ru/post/1432628/


All Articles