Creation Model-Simple Factory Model

The simple factory model does not belong to GoF's 23 design patterns.
So why should I use the factory model? Please see the following program.

#include<iostream>
using namespace std;

//水果类
class Fruit 
{
    
    
public:
	Fruit(string name) 
	{
    
    
		m_name = name;
	}
	void showFruitName() 
	{
    
    
		if (m_name.compare("apple") == 0) 
		{
    
    
			cout << "我是苹果" << endl;
		}
		else if (m_name.compare("banana") == 0) 
		{
    
    
			cout << "我是香蕉" << endl;
		}
		else if (m_name.compare("pear") == 0) 
		{
    
    
			cout << "我是鸭梨" << endl;
		}
	}
private:
	string m_name;
};

int main() 
{
    
    

	Fruit* apple = new Fruit("apple");
	Fruit* banana = new Fruit("banana");
	Fruit* pear = new Fruit("pear");

	apple->showFruitName();
	banana->showFruitName();
	pear->showFruitName();

	system("pause");
	return EXIT_SUCCESS;
}

It is not difficult to see that the Fruit class is a "huge" class. There are several problems in the design of this class:
(1) There are many "if...else..." code blocks in the Fruit class, and the code for the entire class is quite lengthy. , The longer the code, the more difficult it is to read, maintain, and test; and the existence of a large number of conditional statements will also affect the performance of the system, and a large number of conditional judgments need to be made during the execution of the program.
(2) The responsibility of the Fruit class is too heavy. It is responsible for initializing and displaying all fruit objects. The initialization code and display code of various fruit objects are implemented in one class, which violates the "single responsibility principle" and is not conducive to the class. Reuse and maintain;
(3) When a new type of fruit needs to be added, the source code of the Fruit class must be modified, which violates the "open and close principle".

Roles and responsibilities in the model

Insert picture description here
Factory (Factory) role: the core of the simple factory model, which is responsible for implementing the internal logic of creating all instances. The factory class can be directly called by the outside world to create the required product objects.
AbstractProduct (AbstractProduct) role: the parent class of all objects created by the simple factory pattern, which is responsible for describing the common interface shared by all instances.
Concrete Product (Concrete Product) role: the concrete instance object created by the simple factory pattern.

Simple factory pattern case

Insert picture description here

#include<iostream>
#include<string>
using namespace std;


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

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

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

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

//水果工厂
class FruitFactory 
{
    
    
public:
	static Fruit* CreateFruit(string name) 
	{
    
    
		if (name.compare("apple") == 0) 
		{
    
    
			return new Apple;
		}
		else if (name.compare("banana") == 0) 
		{
    
    
			return new Banana;
		}
		else if (name.compare("pear") == 0) 
		{
    
    
			return new Pear;
		}
	}
};

//测试
void test01() 
{
    
    

	Fruit* fruit = NULL;
	fruit = FruitFactory::CreateFruit("apple");  //工厂生产苹果
	fruit->shoName();
	delete fruit;

	fruit = FruitFactory::CreateFruit("banana"); //工厂生产香蕉
	fruit->shoName();
	delete fruit;

	fruit = FruitFactory::CreateFruit("pear"); //工厂生产鸭梨
	fruit->shoName();
	delete fruit;

}

int main() {
    
    

	test01();

	system("pause");
	return EXIT_SUCCESS;
}

The advantages and disadvantages of the simple factory model

Advantages:
(1) The separation of object creation and use is realized.
(2) There is no need to remember the specific class name, just remember the parameters, which reduces the amount of user memory.
Disadvantages:
(1) Excessive responsibilities for factories, once they cannot work, the system will be affected.
(2) Increasing the number of classes in the system increases the complexity and understanding.
(3) In violation of the "opening and closing principle", adding new products requires modifying the factory logic, and the factory is becoming more and more complex.

Applicable scene

  1. The factory class is responsible for creating fewer objects. Since there are fewer objects created, the business logic in the factory method will not be too complicated.
  2. The client only knows the parameters passed into the factory class, and does not care about how to create the object.

Guess you like

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