Design Pattern C++ Simple Factory Pattern

When we want to use an object, we must instantiate the object through the class, that is, we need a new object. The process of new is very complicated, and it has to go through the process of reading files -> parsing text -> creating objects -> setting values ​​​​for attributes . The introduction of the factory mode means that the step of creating classes will be completed by the factory for us. We only need to use the classes created in the factory. When using a factory, we need to let the factory know an object we want, so we can tell the factory our needs by passing parameters

Definition idea : In the simple factory pattern, instances of different classes can be returned according to different parameters. The simple factory pattern specifically defines a class to be responsible for creating instances of other classes, and the created instances usually have a common parent class

Advantages :

  1. Realized the decoupling of the client and the concrete implementation class
  2. Clients are relieved of the responsibility to create product objects directly and instead simply "consume" product products
  3. The client does not need to know the class name of the specific product category created, but only needs to know the parameters corresponding to the specific product category

Disadvantages :

  1. When you want to add a new product category, you need to modify the source code, which does not conform to the principle of opening and closing
  2. The creation logic of all product classes is concentrated in the factory class, if there is a problem with this class. The whole system will be affected

Applicable scenarios :

  1. The factory class is responsible for creating fewer objects
  2. The client only knows the parameters passed into the factory class, and doesn't care about how to create the object:

Since the simple factory pattern does not conform to the principle of opening and closing, the boss removed this pattern from the 24 design patterns

Code implementation (mobile phone products)

insert image description here

First create an abstract mobile phone class, and specify that the specific product mobile phone must have the specified function, that is, the product class must override the method in this class

//抽象手机
class AbstractPhone
{
    
    
public:
	virtual void ShowName() = 0;
};

The mobile phone of the specific product inherits the abstract mobile phone class and rewrites its methods

//华为
class Huawei : public AbstractPhone
{
    
    
public:
	virtual void ShowName()
	{
    
    
		cout << "Huawe Phone" << endl;
	}
};

//小米
class Xiaomi : public AbstractPhone
{
    
    
public:
	virtual void ShowName()
	{
    
    
		cout << "Xiaomi Phone" << endl;
	}
};

//苹果
class Apple : public AbstractPhone
{
    
    
public:
	virtual void ShowName()
	{
    
    
		cout << "Apple Phone" << endl;
	}
};

Determine which phone needs to be generated by passing in parameters

//手机工厂
class PhoneFactory
{
    
    
public:
	static AbstractPhone* CreatePhone(string name)
	{
    
    
		if (name == "huawei")
			//...生产细节
			return new Huawei;
		else if (name == "xiaomi")
			//...生产细节
			return new Xiaomi;
		else if (name == "apple")
			//...生产细节
			return new Apple;
		else
			return nullptr;
	}
};

test:

void test()
{
    
    
	//创建工厂
	PhoneFactory* factor = new PhoneFactory;
	//创建手机
	AbstractPhone* phone;
	//指定工厂需要创建的手机
	phone = factor->CreatePhone("huawei");
	phone->ShowName();
	delete phone;

	phone = factor->CreatePhone("xiaomi");
	phone->ShowName();
	delete phone;

	phone = factor->CreatePhone("apple");
	phone->ShowName();
	delete phone;

	delete factor;
}

Running screenshot :
insert image description here
Recommended reading design pattern C++ factory method pattern

Guess you like

Origin blog.csdn.net/qq_44443986/article/details/117517685