The factory method is an object creation mode. It aims to define an interface for creating objects, so that the subclass determines which class to instantiate and delays the instantiation of a class to the subclass.
Applicable scenarios
The factory method is applicable to the following situations:
1) when a class does not know the class of the object it must create;
2) When a class wants its subclass to specify the object it creates;
3) when the class delegates the responsibility of creating an object to one of multiple help sub-classes;
Mode Structure
Let's take a look at the schema structure of the factory method to give it a global understanding ,:
Participants
Obviously, from the above model structure, we can see that there are four main participants in the factory method.
1) Product
Defines the interface of the object created by the factory method.
2) concreteProduct
Interface for object creation.
3) Creator
Declare the factory method to return the Product object. You can also define the default implementation to return a default ConcreteProduct object. In addition, you can also call the factory method in the Creator to create a Product object, it will be reflected in the Code later. The GetProduct () method and CreateProduct () method are the main methods.
4) ConcreteCreator
The CreateProduct () method is redefined to return a specific ConcreteProduct instance.
In addition, if you want to create a specific ConcreteProduct instance, Creator depends on its subclass, such as ConcreteCreator, this creates a potential problem-a subclass of the Creator class may be forced to create only appropriate Product objects. One solution in C ++ is to use a template.
Implementation
The following code implements the above factory method. The specific operations and discrepancies in the class mainly reflect this idea-delayed creation.
The first is various declarations and inheritance systems:
[Cpp] // factory. h
# Ifndef FACTORY_H
# Define FACTORY_H
Class Product
{
};
Class Creator
{
Public:
Creator (): m_pProduct (nullptr ){};
Product * GetProduct ();
Virtual Product * CreateProduct () = 0;
Private:
Product * m_pProduct;
};
/* A solution is provided for the above potential problems.
* 1) provides a template subclass of the Creator class, using the Product class as the template parameter
* 2) This can reduce the creation of classes.
*/
Template <class TheProduct>
Class StandardCreator: public Creator
{
Public:
Virtual Product * CreateProduct ();
};
Template <typename TheProduct>
Product * StandardCreator <TheProduct>: CreateProduct ()
{
Return new TheProduct ();
}
# Endif
// Factory. h
# Ifndef FACTORY_H
# Define FACTORY_H
Class Product
{
};
Class Creator
{
Public:
Creator (): m_pProduct (nullptr ){};
Product * GetProduct ();
Virtual Product * CreateProduct () = 0;
Private:
Product * m_pProduct;
};
/* A solution is provided for the above potential problems.
* 1) provides a template subclass of the Creator class, using the Product class as the template parameter
* 2) This can reduce the creation of classes.
*/
Template <class TheProduct>
Class StandardCreator: public Creator
{
Public:
Virtual Product * CreateProduct ();
};
Template <typename TheProduct>
Product * StandardCreator <TheProduct>: CreateProduct ()
{
Return new TheProduct ();
}
# Endif
The following is a simple implementation:
[Cpp] // factory. cpp
# Include "factory. h"
# Include <iostream>
Using namespace std;
Product * Creator: GetProduct ()
{
If (nullptr = m_pProduct)
{
M_pProduct = CreateProduct ();
}
Return m_pProduct;
}
// Factory. cpp
# Include "factory. h"
# Include <iostream>
Using namespace std;
Product * Creator: GetProduct ()
{
If (nullptr = m_pProduct)
{
M_pProduct = CreateProduct ();
}
Return m_pProduct;
}
Finally, the test code is as follows:
[Cpp] // test. cpp
# Include "factory. h"
# Include <iostream>
Using namespace std;
// You can use a unified method to create a product by providing different product categories.
Class MyProduct: public Product
{
Public:
MyProduct ()
{
Cout <"MyProduct has been constructed! "<Endl;
}
};
Class YourProduct: public Product
{
Public:
YourProduct ()
{
Cout <"YourProduct has been constructed! "<Endl;
}
};
Class OurProduct: public Product
{
Public:
OurProduct ()
{
Cout <"OurProduct has been constructed! "<Endl;
}
};
Int main ()
{
StandardCreator <OurProduct> ourCreator;
Product * pProduct = ourCreator. GetProduct ();
StandardCreator <MyProduct> myCreator;
PProduct = myCreator. GetProduct ();
StandardCreator <YourProduct> yourCreator;
PProduct = yourCreator. GetProduct ();
Char ch = getchar ();
Return 1;
}
// Test. cpp
# Include "factory. h"
# Include <iostream>
Using namespace std;
// You can use a unified method to create a product by providing different product categories.
Class MyProduct: public Product
{
Public:
MyProduct ()
{
Cout <"MyProduct has been constructed! "<Endl;
}
};
Class YourProduct: public Product
{
Public:
YourProduct ()
{
Cout <"YourProduct has been constructed! "<Endl;
}
};
Class OurProduct: public Product
{
Public:
OurProduct ()
{
Cout <"OurProduct has been constructed! "<Endl;
}
};
Int main ()
{
StandardCreator <OurProduct> ourCreator;
Product * pProduct = ourCreator. GetProduct ();
StandardCreator <MyProduct> myCreator;
PProduct = myCreator. GetProduct ();
StandardCreator <YourProduct> yourCreator;
PProduct = yourCreator. GetProduct ();
Char ch = getchar ();
Return 1;
}
The following is the test result: www.2cto.com
From Arvon Zhang's column