SummaryWe have discussed the Template method mode in one of the behavior modes before, while the Factory method mode is actually an Application Form of the Template method mode in the Factory scenario (create object. The Template method mode is defined for the algorithm framework in the base class. The method is used to call the specific implementation of each step of the algorithm encapsulated in the subclass. What about the Factory method mode? Actually, it is a specific application. My definition of it is to define the overall framework logic for object creation in the base class factory, and the steps for creating a specific object in the subclass factory, then, use the base class method to call the actual creation steps encapsulated in the subclass.
PurposeYou can ignore the logical process of Object Instantiation and call a unified interface to instantiate the object. At the same time, the coupling between the caller and the specific object is removed, so that the caller only relies on abstraction. (The purpose of all Factory modes should be the same)
InstanceThe most basic implementation of the Factory method mode is as follows:
class AbstractObject {};class ConcreteObjectA : public AbstractObject {};class ConcreteObjectB : public AbstractObject {};class ConcreteObjectNULL : public AbstractObject {}; class Factory {public: virtual AbstractObject* do_create() = 0; AbstractObject* create_object() { return this->do_create(); }};class FactoryA :public Factory{public: virtual AbstractObject* do_create() { return new ConcreteObjectA; } };class FactoryB :public Factory{public: virtual AbstractObject* do_create() { return new ConcreteObjectB; } };
Each subclass overrides the do_create method to create a specific object. The base class provides the do_create method as the unified object creation interface. The specific usage is as follows:
Factory * factory = new FactoryA; AbstractObject * obj = factory-> create_object (); // obtain the ConcreteObjectA object
Some may ask, what is the actual significance of this? Didn't it complicate the simple question? If the code is really just like this, it is indeed too complicated. However, the actual problems are often not so simple and there will be many additional requirements. For example, if all objects have the initialize method, you must ensure that the initialize method is called for normal initialization during object creation, for example, we may need to add additional logic judgment when creating ConcreteObjectA, etc. Different object creation and initialization processes will follow. Based on the above implementation, we can simply expand to meet the requirements:
class AbstractObject {public: virtual void initialize() {} };class Factory {public: virtual AbstractObject* do_create() = 0; AbstractObject* create_object() { AbstractObject* obj = this->do_create(); obj->initialize(); return obj; }};class FactoryA :public Factory{public: virtual AbstractObject* do_create() { bool isOk = true; // isOk = ...any judgement if (isOk) return new ConcreteObjectA; return new ConcreteObjectNULL; } };
As you can imagine, if we couple all the creation logic into a Factory class, it will cause a lot of trouble for future extension.
ApplicationWe can see that the Factory method is a special case of the Template method mode or an application method. For more information about the Template method mode, see [design patterns Guide] Template Method mode of the behavior mode.