Design Pattern C++ Factory Method Pattern

Recommended reading design pattern C++ simple factory pattern

The factory method pattern is optimized based on the shortcomings of the simple factory pattern. We all know that in the simple factory pattern, to increase or decrease a product type, it is necessary to modify the if-else judgment in the factory. This obviously does not conform to the opening and closing principle in our code development, and it is very troublesome to expand

Factory method pattern = simple factory pattern + "opening and closing principle"

Definition idea : The factory parent class is responsible for defining the public interface for creating product objects, while the factory subclass is responsible for generating specific product objects. The purpose of this is to delay the instantiation of the product class to the factory subclass, that is, through the factory subclass to determine which concrete product class should be instantiated

Advantages :

  1. No need to remember specific class names, not even specific parameters
  2. Achieved separation of object creation and use
  3. The scalability of the system becomes better and complies with the principle of opening and closing

Disadvantages :

  1. The classes in the system are added in pairs, increasing the complexity and understanding of the system

Applicable scenarios :

  1. The client does not need to know the name of the specific product category, but only needs to know the corresponding factory
  2. Don't care about the details of class creation and implementation

Code implementation : (mobile phone product)
insert image description here

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

//华为
class Huawei : public AbstractPhone
{
    
    
public:
	virtual void ShowName()
	{
    
    
		cout << "Huawei 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;
	}
};

//抽象工厂
class AbstractPhoneFactory
{
    
    
public:
	virtual AbstractPhone* CreatePhone() = 0;
};

//华为工厂
class HuaweiFactory : public AbstractPhoneFactory
{
    
    
public:
	virtual AbstractPhone* CreatePhone()
	{
    
    
		return new Huawei();
	}
};

//小米工厂
class XiaomiFactory : public AbstractPhoneFactory
{
    
    
public:
	virtual AbstractPhone* CreatePhone()
	{
    
    
		return new Xiaomi();
	}
};

//苹果工厂
class AppleFactory : public AbstractPhoneFactory
{
    
    
public:
	virtual AbstractPhone* CreatePhone()
	{
    
    
		return new Apple();
	}
};

test:

void test()
{
    
    
	//创建一个工厂
	AbstractPhoneFactory* factory = nullptr;
	//创建一个手机
	AbstractPhone* phone = nullptr;

	//指定工厂是华为工厂
	factory = new HuaweiFactory;
	//通过华为工厂创建一个手机
	phone = factory->CreatePhone();
	phone->ShowName();

	delete phone;
	delete factory;

	factory = new XiaomiFactory;
	phone = factory->CreatePhone();
	phone->ShowName();

	delete phone;
	delete factory;

	factory = new AppleFactory;
	phone = factory->CreatePhone();
	phone->ShowName();

	delete phone;
	delete factory;
}

Running results:
insert image description here
recommended reading design pattern C++ abstract factory pattern

Guess you like

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