factory pattern, abstract factory, simple factory

The factory method pattern defines an interface for creating objects, but it is up to the subclass to decide which class to instantiate, which means that the factory method pattern defers instantiation to the subclass.

The factory method model is very consistent with the "opening and closing principle". When a new product needs to be added, we only need to add a specific product category and the corresponding specific factory without modifying the original system. At the same time, in the factory method mode, the user only needs to know the specific factory that produces the product, and does not need to be concerned with the creation process of the product, or even the specific product class name. Although he complies with the "opening and closing principle" very well, since every time a new product is added, two classes need to be added, which will inevitably lead to an increase in the complexity of the system.

The specific code is as follows

#include <iostream>
using namespace std;
class SingleCore
{
    
    
public:
	virtual void Show() = 0;
};
class SingleCoreA:public SingleCore
{
    
    
public:
  void Show()
  {
    
    
      cout << "Single Core A" << endl;
  }
};
class SingleCoreB:public SingleCore
{
    
    
public:
  void Show()
  {
    
    
      cout << "Single Core B" << endl;
  }
};
class Factory
{
    
    
public:
  virtual SingleCore* CreateSingleCore() = 0;
};
class FactoryA:public Factory
{
    
    
public:
  SingleCore* CreateSingleCore()
  {
    
    
      cout << "Create SingleCore A" << endl;
      return new SingleCoreA();
  }
};
class FactoryB:public Factory
{
    
    
public:
  SingleCore* CreateSingleCore()
  {
    
    
      cout << "Create SingleCore B" << endl;
      return new SingleCoreB();
  }
};
int main()
{
    
    
  FactoryA m;
  SingleCore* p = m.CreateSingleCore();
  p->Show();
  FactoryB n;
  p = n.CreateSingleCore();
  p->Show();
}

Disadvantages: Every time a product is added, it is necessary to add a specific class and object implementation factory, which doubles the number of classes in the system, increases the
complexity of the system to a certain extent, and also increases the number of specific classes in the system. rely. it's not a good thing

abstract factory pattern

The abstract factory pattern refers to defining an interface that creates a series of related or interdependent objects without specifying their specific classes.

For example: The technology of this company continues to develop, and now it can produce multi-core processors. The specific method: it is still necessary to open two factories to produce two types of processors, A and B.

The specific code is as follows:

  #include<iostream>
  using namespace std;
            class SingleCore
            {
    
    
            public:
                virtual void Show() = 0;
            };
            class SingleCoreA:public SingleCore
            {
    
    
            public:
                void Show()
                {
    
    
                    cout << " SingleCoreA"<<endl;
                }
            };
            class SingleCoreB:public SingleCore

            {
    
    
            public:
                void Show()
                {
    
    
                    cout << " SingleCoreA"<<endl;
                }
            };
            class SingleCoreB:public SingleCore
            {
    
    
            public:
                void Show()
                {
    
    
                    cout << "SingleCoreB" << endl;
                }
            };

            class MultiCore
            {
    
    
            public:
                virtual void Show() = 0;
            };
            class MultiCoreA:public MultiCore
            {
    
    
            public:
                void Show()
                {
    
    
                    cout << "MultiCoreA" << endl;
                }
            };
            class MultiCoreB:public MultiCore
            {
    
    
            public:
                void Show()
                {
    
    
                    cout << "MultiCoreB" << endl;
                }
            };
            class CoreFactory
            {
    
    
            public:
                virtual SingleCore* createSingleCore() = 0;
                virtual MultiCore* createMultiCore() = 0;
            };

            class CoreFactoryA:public CoreFactory
            {
    
    
            public:
                SingleCore* createSingleCore()
                {
    
    
                    cout << "create SingleCoreA" << endl;
                    return new SingleCoreA();
                }
                MultiCore* createMultiCore()
                {
    
    
                    cout << "create MultiCoreA" << endl;
                    return new MultiCoreA();

                }
            };
            class CoreFactoryB:public CoreFactory
            {
    
    
            public:
                SingleCore* createSingleCore()
                {
    
    
                    cout << "create SingleCoreB" << endl;
                    return new SingleCoreB();
                }
                MultiCore* createMultiCore()
                {
    
    
                    cout << "create MultiCoreB" << endl;
                    return new MultiCoreB();
                }
            };

            int main()
            {
    
    
                CoreFactoryB Fcb;
                SingleCore* core = Fcb.createSingleCore();
                MultiCore* Mcore = Fcb.createMultiCore();
                core->Show();
                Mcore->Show();
                CoreFactoryA Fca;
                core = Fca.createSingleCore();
                Mcore = Fca.createMultiCore();
                core->Show();
                Mcore->Show();
            }

Simple Factory Pattern

The simple factory pattern is to determine which instance of a product class to create by a factory object.
For example: There is a manufacturer of processing cores, which has only one factory and can produce two types of processor cores. What kind of processor the customer needs must be clearly told to the factory.

Disadvantage: Every time a new product is added, an object factory needs to be added

The specific code is as follows:

 #include<iostream>
 using namespace std;
            class SingleCore
            {
    
    
            public:
                virtual void Show() = 0;
            };
            class SingleCoreA:public SingleCore
            {
    
    
            public:
                void Show()
                {
    
    
                    cout << "Single Core A" << endl;
                }
            };
            class SingleCoreB:public SingleCore
            {
    
    
            public:
                void Show()
                {
    
    
                    cout << "Single Core B" << endl;
                }
            };
            class Factory
            {
    
    
            public:
                virtual SingleCore* CreateSingleCore() = 0;
            };
            class FactoryA:public Factory
            {
    
    
            public:
                SingleCore* CreateSingleCore()
                {
    
    
                    cout << "Create SingleCore A" << endl;
                    return new SingleCoreA();
                }
            };
            class FactoryB:public Factory
            {
    
    
            public:
                SingleCore* CreateSingleCore()
                {
    
    
                    cout << "Create SingleCore B" << endl;
                    return new SingleCoreB();
                }
            };
            int main()
            {
    
    
                FactoryA m;
                SingleCore* p = m.CreateSingleCore();
                p->Show();
                FactoryB n;
                p = n.CreateSingleCore();
                p->Show();
            }

Guess you like

Origin blog.csdn.net/lml_w/article/details/125766122