C + + Factory mode

Source: Internet
Author: User
here is a summary of my own study of design patterns. Simple Factory mode

The simple factory model is the simplest one in the factory model, he can hide the details of creating objects in a simpler way, usually by telling the type that the factory class needs, and the factory class will return the desired product class, but the client sees only the abstract object of the product, regardless of which subclass it returns. The only specific subclass that the client needs to know is the factory subclass. In addition to this, the basic principle of reliance on reversal is met.

If we don't use the factory class, using only Abstractproduct and its subclasses, the client needs to know exactly which subclass to use each time a different subclass is used, but when the analogy is less, there is no problem, but when the analogy is more, the management is very troublesome, You have to do a lot of substitution, and an error occurs when you're not careful.

And after using the factory class, there will be no such problem, no matter how many classes inside, I just need to know the class model can be. However, there is also the question that if I create a different type each time I use the factory class, there will be problems when I modify it, or I need a lot of replacements. So the simple factory model should generally be used in most parts of the program only one of the products, factory classes do not have to frequently create product classes. This modification only needs to modify a limited number of places.

Customers only need to know simplefactory on it, use the time is also used abstractfactory, so that the client only when the first time to create a factory is aware of specific details, other times it only know Abstractfactory, This is the perfect way to achieve the principle of relying on the reverse. Common Scenarios

For example, if you are deploying multiple databases, you may want to use different databases in different places, and you only need to set the type of the database in the configuration file, and then generate an instance based on the type each time, so no matter how the following database type changes, the client appears to have only one abstractproduct, There is no need to modify the code when used. The supplied type can also be in a more easily recognizable string, so that you do not have to remember a long class name, or you can save as a configuration file.

This way, you only need to modify the configuration file and add new product subclasses at a time.

Therefore, the simple factory model is generally applied to a variety of similar types of cases, these classes are hidden, and then provide a unified interface, easy to maintain and modify. Advantages

1. Hides the details of object creation and defers the instantiation of the product to the subclass.

2. The client basically does not care which product is used, just need to know which factory is the line, the provided type can also use a more easily recognizable string.

3. Easy to add new product subclass, each time only need to modify the factory class pass the type value of the line.

4. The principle of dependency reversal was followed. Disadvantage

1. The type of product subclass is required, the same method names are used, if there is more analogy, and all the classes must add a method, it will be very troublesome things. or a class. Another class has several different methods, and the client cannot know which product subclass it is and cannot invoke these different methods.

2. Each addition of a product subclass must add a judgment branch to the factory class, which violates the open-closed principle. C + + implementation code

1 #ifndef _abstractproduct_h_
 2 #define _ABSTRACTPRODUCT_H_
 3 
 4 
 5 #include <stdio.h>
 6 
 7 
 8 class abstractproduct{
 9 public 
: one     abstractproduct ();     virtual ~abstractproduct ();
Public     
:     virtual void operation () = 0;
; 
class Producta:public abstractproduct{public 
:     producta ();     virtual ~producta ();
Public     
:     void operation ();
}; 
class Productb:public abstractproduct{public 
:     productb ();     ~PRODUCTB ();
Public     
:     operation Void ();
};
#endif of Panax Notoginseng 
AbstractProduct.h
1 #include "AbstractProduct.h"
 2 
 3 
 4 
 5 abstractproduct::abstractproduct () {
 6}
 7 
 8 
 9 abstractproduct::~abstractproduct () 
producta::P roducta () {
}
The 
producta::~producta () { 
22} producta::operation ()     fprintf (stderr, "ProductA operation!\n"); 
PRODUCTB::P roductb () 
PRODUCTB::~PRODUCTB () ()
{ 
the Void Productb::operation () {     fprintf (stderr, "PRODUCTB operation!\n");
36}
AbstractProduct.cpp

1 #ifndef _simplefactory_h_
 2 #define _SIMPLEFACTROY_H_
 3 
 4 #include <stdio.h>
 5 #include " AbstractProduct.h "
 6 
 7 
 8 class abstractfactory{
 9 public 
: one     abstractfactory ();     virtual ~abstractfactory ();
Public     
:     virtual abstractproduct* createproduct (int type) = 0;    
; 
class simplefactory:public abstractfactory{public 
:     simplefactory ( );     ~simplefactory ();
Public     
:     abstractproduct* createproduct (int type);
}; 
#endif
SimpleFactory.h
1 #include "SimpleFactory.h"
 2 
 3 
 4 abstractfactory::abstractfactory () {
 5}
 6 
 7 
 8 Abstractfactory::~abstractfactory () {
 9} 
simplefactory::simplefactory () {
13} 
simplefactory::~simplefactory () ( 
abstractproduct*) simplefactory::createproduct (int type) {     abstractproduct* temp = NULL;     switch (type)
{case     1:         temp = new ProductA ();
-Break         ;
Case     2:         temp = new PRODUCTB ();
Break         ;     default: Break         ;
The return of the     temp;
34}
SimpleFactory.cpp
1 #include "SimpleFactory.h"
 2 
 3 
 4 int main () {
 5     abstractfactory* factory = new Simplefactory ();
 6     abstractproduct* Product = factory->createproduct (1);
 7     product->operation ();
 8     Delete product;
 9     product = NULL;     Product = factory->createproduct (2);     product->operation ();
The     delete product;     product = NULL;
return     0;
16}
Client.cpp
1 g++-o client client.cpp SimpleFactory.cpp AbstractProduct.cpp

Results

Factory mode

The factory model is basically the same as the simple factory model, which also says that every time you add a product subclass, you have to add a judgment branch to the factory class, which violates the open-closed principle, so the factory pattern is created to solve the problem.

Since every time I have to judge, then I will make these judgments to generate a factory subclass, so that each time you add a product subclass, just add a factory subclass. This is the perfect adherence to the open-closed principle. But that's actually a problem, if the number of products is sufficient, the amount to be maintained will increase, fortunately, the General factory subclass is only used to generate product classes, as long as the product subclass name does not change, then the basic factory subclass does not need to modify, each time only need to modify the product subclass.

The same factory model should generally be used in most parts of the program only one of the products, factory classes do not have to frequently create product classes. This modification only needs to modify a limited number of places.

Common Scenarios

Basically consistent with the simple factory model, it is simply a flaw in the open-closed principle in the simple factory model, making the model more resilient. Defers the instantiation process to the subclass, which determines which of the instantiations to instantiate. Advantages

Basically consistent with the simple factory model, a lot of advantage is to follow the open-closed principle, make the model more flexible. Disadvantage

Similar to the simple factory model. C + + implementation code

1 #ifndef _abstractproduct_h_
 2 #define _ABSTRACTPRODUCT_H_
 3 
 4 
 5 #include <stdio.h>
 6 
 7 
 8 class abstractproduct{
 9 public 
: one     abstractproduct ();     virtual ~abstractproduct ();
Public     
:     virtual void operation () = 0;
; 
class Producta:public abstractproduct{public 
:     producta ();     virtual ~producta ();
Public     
:     void operation ();
}; 
class Productb:public abstractproduct{public 
:     productb ();     ~PRODUCTB ();
Public     
:     operation Void ();
};
#endif of Panax Notoginseng 
AbstractProduct.h
1 #include "AbstractProduct.h"
 2 
 3 
 4 
 5 abstractproduct::abstractproduct () {
 6}
 7 
 8 
 9 abstractproduct::~abstractproduct () 
producta::P roducta () {
}
The 
producta::~producta () { 
22} producta::operation ()     fprintf (stderr, "ProductA operation!\n"); 
PRODUCTB::P roductb () 
PRODUCTB::~PRODUCTB () ()
{ 
the Void Productb::operation () {     fprintf (stderr, "PRODUCTB operation!\n");
36}
AbstractProduct.cpp
1 #ifndef _simplefactory_h_
 2 #define _SIMPLEFACTROY_H_
 3 
 4 #include <stdio.h>
 5 #include " AbstractProduct.h "
 6 
 7 
 8 class abstractfactory{
 9 public 
: one     abstractfactory ();     virtual ~abstractfactory ();
Public     
:     virtual abstractproduct* createproduct () = 0;    
; 
class factorya:public abstractfactory{public 
:     Factorya ();     ~factorya ();
Public     
:     abstractproduct* createproduct ();
}; 
class factoryb:public abstractfactory{public 
:     factoryb ();     ~factoryb ();
Public     
:
Panax notoginseng     abstractproduct* createproduct ();
/};
#endif
AbstractFactory.h
1 #include "AbstractFactory.h"
 2 
 3 
 4 abstractfactory::abstractfactory () {
 5}
 6 
 7 
 8 Abstractfactory::~abstractfactory () {
 9} 
Factorya::factorya () 
abstractproduct* factorya::createproduct () Factorya::~factorya () {
-     abstractproduct* temp = NULL;     temp = new ProductA ();
return to     temp; 
factoryb::factoryb () 
Factoryb::~factoryb () () () 
abstractproduct* factoryb::createproduct () {     abstractproduct* temp = NULL;
Panax Notoginseng     temp = new PRODUCTB ();
return to     temp;
39}
AbstractFactory.cpp
1 #include "AbstractFactory.h"
 2 
 3 
 4 int main () {
 5     abstractfactory* factory = new Factorya ();
 6     abstractproduct* Product = Factory->createproduct ();
 7     product->operation ();
 8     Delete product;
 9     product = NULL;
A     delete factory;
One     factory = NULL;     factory = new Factoryb ();     Product = Factory->createproduct ();     product->operation ();     Delete product;     product = NULL;     Factory Delete;     factory = NULL;     0;
21}
Client.cpp
1 g++-o client client.cpp AbstractFactory.cpp AbstractProduct.cpp

Results

Abstract Factory Pattern

Abstract factory patterns become more complex than factory patterns, as mentioned above, and factory and simple factory patterns require that product subclasses must be of the same type and have common methods, which limits the extension of product subclasses. So in order to be more convenient to expand, the abstract factory model will be the same class of product subclasses into a class, let them inherit the same abstract subclass, we can see them together as a group, and then several groups of products constitute a family.

At this point, the client must know which factory it is and which group of product abstract classes to use. Each factory subclass is responsible for producing a family of products, whereas a subclass of a method produces a type of product. In the client's view only Abstractproducta and ABSTRACTPRODUCTB two kinds of products, the use of the direct use of both products. And through the factory to identify which family products belong to.

Products Producta_1 and productb_1 constitute a family of products, corresponding to the creation of Factory1, which means Factory1 always create producta_1 and productb_1 products, In the client's view it is only necessary to know what kind of factory and product group is available. Generally speaking, both producta_1 and Productb_1 are adapted to the same environment, so they are classified as a family. Common Scenarios

For example, under Linux and Windows two operating systems, there are 2 pendants A and B, they are implemented in different ways under Linux and Windows, and Factory1 is responsible for creating pendants A and B that can run under Linux. Factory2 is responsible for creating pendants A and B that can run under windows so that if the system environment changes, we just need to modify the factory. Advantages

1. Encapsulation of the creation of the product, so that do not need to know what kind of products, just need to know which factory on the line.

2. Can support different types of products, making the mode more flexible.

3. It is very convenient to use different types of products in the middle of a family. Disadvantage

1. The structure is too bloated, if the product type is more, or product family analogy more, it will be very difficult to manage.

2. Each time a group of products is added, all factory classes must add a method that violates the open-closed principle. Therefore, the general application of product mix product family changes in a small situation. C + + implementation code

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.