Design pattern (3) Factory method pattern

Provide the original link https://blog.csdn.net/sinat_21107433/article/details/102616501

Factory method pattern ideas

The idea of ​​the simple factory design pattern determines that every time a specific product is added, the internal product composition of the factory needs to be changed. Because of the opening and closing principle, the idea of ​​opening to the outside world, closing to the inside, and the factory method design mode is not used to create all specific products in a unified manner. Different factories need to add new products for different products. When adding new products, you need to add corresponding ones. Factory.

Factory method pattern: Define an interface for creating objects, but let the subclass decide which class to instantiate. The factory method pattern delays the instantiation of a class to its subclasses.

Factory method pattern structure

From the introduction of the above-mentioned factory method pattern, it can be seen that this pattern is composed of the following main members:

  • Abstract Factory (AbstractFactory): the base class of all factory classes that produce specific products, providing public methods of the factory class;
  • Concrete Factory (ConcreteFactory): produce specific products
  • Abstract Product (AbstractProduct): the base class of all products, providing public methods of the product class
  • Concrete Product (ConcreteProduct): specific product category

Insert picture description here

Factory method pattern example

需求
需求
需求
客户
篮球工厂
篮球
足球工厂
足球
排球工厂
排球

The corresponding UML diagram is as follows:
Insert picture description here
factorymethod.h

#include <iostream>
#include <string.h>

using namespace std;

//抽象产品类AbstractProduct
class AbstractSportProduct
{
    
    
public:
  AbstractSportProduct() {
    
    }
  //抽象方法:
  void printName(){
    
    };
  void play(){
    
    };
};

//具体产品类Basketball
class Basketball : public AbstractSportProduct
{
    
    
public:
  Basketball()
  {
    
    
    printName();
    play();
  }
  //具体实现方法
  void printName()
  {
    
    
    cout << "Get Basketball\n";
  }
  void play()
  {
    
    
    cout << "Play Basketball\n\n";
  }
};

//具体产品类Football
class Football : public AbstractSportProduct
{
    
    
public:
  Football()
  {
    
    
    printName();
    play();
  }
  //具体实现方法
  void printName()
  {
    
    
    cout << "Get Football\n";
  }
  void play()
  {
    
    
    cout << "Play Football\n\n";
  }
};

//具体产品类Volleyball
class Volleyball : public AbstractSportProduct
{
    
    
public:
  Volleyball()
  {
    
    
    printName();
    play();
  }
  //具体实现方法
  void printName()
  {
    
    
    cout << "Get Volleyball\n";
  }
  void play()
  {
    
    
    cout << "Play Volleyball\n\n";
  }
};

//抽象工厂类
class AbstractFactory
{
    
    
public:
  virtual AbstractSportProduct *getSportProduct() = 0;
};

//具体工厂类BasketballFactory
class BasketballFactory : public AbstractFactory
{
    
    
public:
  BasketballFactory()
  {
    
    
    cout << "BasketballFactory\n";
  }
  AbstractSportProduct *getSportProduct()
  {
    
    
    cout << "Basketball";
    return new Basketball();
  }
};

//具体工厂类FootballFactory
class FootballFactory : public AbstractFactory
{
    
    
public:
  FootballFactory()
  {
    
    
    cout << "FootballFactory\n";
  }
  AbstractSportProduct *getSportProduct()
  {
    
    
    cout << "Football";
    return new Football();
  }
};

//具体工厂类VolleyballFactory
class VolleyballFactory : public AbstractFactory
{
    
    
public:
  VolleyballFactory()
  {
    
    
    cout <<  "VolleyballFactory\n";
  }
  AbstractSportProduct *getSportProduct()
  {
    
    
    cout << "Valleyball";
    return new Volleyball();
  }
};

factorymethod.cpp

#include <iostream>
#include "factorymethod.h"

using namespace std;

int main()
{
    
    
	cout << "工厂方法模式\n\n";
	
	//定义工厂类对象和产品类对象
	AbstractFactory *fac = NULL;
	AbstractSportProduct *product = NULL;

	fac = new BasketballFactory();
	product = fac->getSportProduct();
	if (fac)
	{
    
    
		delete fac;
	}
	if (product) {
    
    
		delete product;
	}

	fac = new FootballFactory();
	product = fac->getSportProduct();
	if (fac)
	{
    
    
		delete fac;
	}
	if (product) {
    
    
		delete product;
	}

	fac = new VolleyballFactory();
	product = fac->getSportProduct();	
	if (fac)
	{
    
    
		delete fac;
	}
	if (product) {
    
    
		delete product;
	}

	return 0;
}

Summary of Factory Method Pattern

In summary, it can be seen that if you want to play a sports, you need to add a factory accordingly. Therefore, compared with the simple factory model, the factory method model is more in line with the principle of opening and closing.

advantage:

  1. The factory method is used to create the products required by the customer, and at the same time hide the details of a specific product category to be instantiated from the customer, and the user only needs to care about the factory corresponding to the required product;
  2. The factory independently decides which products to create, and the creation process is encapsulated in specific factory objects. Polymorphic design is the key to the factory method pattern; when
    new products are added, there is no need to modify the original code, which enhances the scalability of the system and conforms to the development. Close principle.

Disadvantages:

  1. When adding new products, you need to add new product factories at the same time. The number of classes in the system increases in pairs, which increases the complexity of the system. More classes need to be compiled and run, which increases the extra overhead of the system;
  2. Both factories and products introduce abstraction layers. The abstraction layers (AbstractFactory and AbstractSportProduct) used in the client code increase the abstraction level of the system and the difficulty of understanding.

Applicable environment:

  • The client does not need to know the class of the object it needs to create;
  • The abstract factory class specifies which object to create through its subclasses (using polymorphic design and the principle of Richter substitution)

Guess you like

Origin blog.csdn.net/qq_24649627/article/details/115008349