Creation Model-Factory Method Pattern

  The meaning of the Factory Method pattern is to define a factory interface for creating product objects and postpone the actual creation work to subclasses. The core factory class is no longer responsible for the creation of the product, so the core class becomes an abstract factory role, only responsible for the interface that the concrete factory subclass must implement. The advantage of this further abstraction is that the factory method pattern can make the system not modify the concrete factory role In the case of introducing new products.
  The factory method pattern is a derivative of the simple factory pattern, which solves many problems of the simple factory pattern. First of all, the "open-close principle" is fully realized, and scalability is realized.

Roles and responsibilities in the factory method pattern

Insert picture description here
Abstract Factory (Abstract Factory) role: the core of the factory method pattern, any factory class must implement this interface.
The role of the factory (Concrete Factory): The concrete factory class is an implementation of the abstract factory and is responsible for instantiating product objects.
Abstract Product (Abstract Product) role: the parent class of all objects created by the factory method pattern, which is responsible for describing the common interfaces shared by all instances.
Concrete Product (Concrete Product) role: the concrete instance object created by the factory method pattern.

	   简单工厂模式  + “开闭原则” =    工厂方法模式

Factory method pattern case

Insert picture description here

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;


//抽象水果
class AbstractFruit 
{
    
    
public:
	virtual void showName() = 0;
};

/* 具体水果 start  */

//苹果
class Apple : public AbstractFruit 
{
    
    
public:
	virtual void showName() 
	{
    
    
		cout << "我是苹果" << endl;
	}
};

//香蕉
class Banana : public AbstractFruit 
{
    
    
public:
	virtual void showName() 
	{
    
    
		cout << "我是香蕉" << endl;
	}
};

//鸭梨
class Pear : public AbstractFruit 
{
    
    
public:
	virtual void showName() 
	{
    
    
		cout << "我是鸭梨" << endl;
	}
};

/* 具体水果 end  */

//抽象工厂
class AbstractFactory 
{
    
    
public:
	virtual AbstractFruit* CreateFruit() = 0;
};

/* 具体工厂类 start */

//苹果工厂
class AppleFactory : public AbstractFactory 
{
    
    
public:
	virtual AbstractFruit* CreateFruit() {
    
    
		return new Apple;
	}
};

//香蕉工厂
class BananaFactory : public AbstractFactory 
{
    
    
public:
	virtual AbstractFruit* CreateFruit()
	{
    
    
		return new Banana;
	}
};

//鸭梨工厂
class PearFactory : public AbstractFactory
{
    
    
public:
	virtual AbstractFruit* CreateFruit() 
	{
    
    
		return new Pear;
	}
};

/* 具体工厂类 end */


//测试
void test01() 
{
    
    

	AbstractFactory* factory = NULL;
	AbstractFruit* fruit = NULL;

	factory = new AppleFactory; //创建苹果工厂
	fruit = factory->CreateFruit(); //苹果工厂生产苹果
	fruit->showName();

	factory = new BananaFactory; //创建香蕉工厂
	fruit = factory->CreateFruit(); //香蕉工厂生产苹果
	fruit->showName();

	factory = new PearFactory; //创建鸭梨工厂
	fruit = factory->CreateFruit(); //鸭梨工厂生产苹果
	fruit->showName();

}

int main() 
{
    
    

	test01();

	system("pause");
	return EXIT_SUCCESS;
}

Advantages and disadvantages of the factory method pattern

Advantages:
(1) No need to remember specific class names, even specific parameters.
(2) The separation of object creation and use is realized.
(3) The scalability of the system becomes very good, without modifying the interface and the original class.
Disadvantages:
(1) Increasing the number of classes in the system increases the complexity and understanding.
(2) Increase the abstraction and difficulty of understanding the system.

Guess you like

Origin blog.csdn.net/bureau123/article/details/112966631