Simple factory model in software

        In software, sometimes it is necessary to separate the creation and use of objects, and then a simple factory pattern is needed.
        1. Three roles of the simple factory pattern
        1) Factory role: The core of the simple factory pattern, 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.
        2) Abstract Product (AbstractProduct) role: the parent class of all objects created by the simple factory pattern, which is responsible for describing the common interfaces shared by all instances.
        3) Concrete Product role: the concrete instance object created by the simple factory model.
Insert picture description here

Figure (1) UML diagram of simple factory pattern

        2. Applicable scenarios
        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.
        3.
        Advantages and disadvantages of the factory model 1) Advantages
        1.1) Realize the separation of object creation and use;
        1.2) No need to remember the specific class name, just remember the parameters, reducing the user's memory burden.
        2) Disadvantages:
        2.1) Excessive responsibilities for the factory category, once it fails to work, the system will be affected;
        2.2) Increase the number of categories in the system, increase the complexity and understanding;
        2.3) Violate the "open and close principle", add new products The factory logic needs to be modified, and the factory is becoming more and more complex.
        Four, case
Insert picture description here

Figure (2) UML diagram of simple factory pattern for fruits

        4.1 Original design ver1.1

#define _CRT_SECURE_NO_WARNINGS

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

class Fruit
{
    
    
public:
	Fruit(string kind){
    
    
		if (this->kind == "apple")
		{
    
    

		}
		else if (this->kind == "banana")
		{
    
    

		}
	}

	void getName(){
    
    
		if (this->kind == "apple")
		{
    
    
			cout << "I am apple" << endl;
		}
		else if (this->kind == "banana")
		{
    
    
			cout << "I am banana" << endl;
		}
	}

private:
	string kind;  //水果的种类
};

//工厂模式的目的,就是让业务层和类的构造函数之间解耦合,
//尽量降低一个类的复杂度

int main(void)
{
    
    
	//要一个苹果
	Fruit apple("apple");
	apple.getName();

	Fruit banana("banana");
	banana.getName();

	return 0;
}

4.2) Design ver1.2 with simple factory mode added

#define _CRT_SECURE_NO_WARNINGS

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


//水果类
class Fruit
{
    
    
public:
	virtual void getName() = 0;
};

class Apple :public Fruit
{
    
    
public:
	virtual void getName(){
    
    
		cout << "apple..." << endl;
	}
};

class Banana :public Fruit
{
    
    
public:
	virtual void getName(){
    
    
		cout << "banana..." << endl;
	}
};

//一个工厂
class Factory
{
    
    
public:
	//水果的生产器
	Fruit* createFruit(string kind){
    
    
		if (kind == "apple")
		{
    
    
			return new Apple;
		}
		else if (kind == "banana")
		{
    
    
			return new Banana;
		}
	}

};

int main(void)
{
    
    
	Factory *factory = new Factory;

	//要一个苹果
	Fruit *apple = factory->createFruit("apple");
	apple->getName();

	//要香蕉
	Fruit *banana = factory->createFruit("banana");
	banana->getName();

	return 0;
}

        Version ver1.2 has better maintainability and scalability than ver1.1 code.

Guess you like

Origin blog.csdn.net/sanqima/article/details/105329092