设计模式:工厂方法模式(讲故事图文易懂)

目录

  • 简单工厂
  • 工厂方法模式


简单工厂

定义:简单工厂由一个工厂根据参数类型决定创建哪种产品的实例。
简单工厂不包含在23种设计模式之内(简单工厂不满足开闭原则,后面会详细讲)
举例:张三去4S店买了车,显而易见,车不是张三造出来的,车是工厂造出来的,张三获得了该车并能够使用该车,但是造车的细节张三不用知道。

铁蛋在一家汽车工厂上班,这家工厂承接了”小米汽车“和”华为汽车“的生产,一天铁蛋接到了要求,要新开一个”长安汽车“生产线生产长安汽车,铁蛋要怎么做呢?

先写一个简单工厂的例子,再看看铁蛋要怎么做?
简单工厂模式有三个角色:抽象基类(Car)
                                            实现类(XiaoMiCar、HuaWeiCar)
                                            简单工厂类(SimpleFactory)

#include 
#include 
using namespace std;

//抽象基类
class Car
{
public:
	Car(string name) : name_(name) {}
	virtual void Show() = 0;

protected:
	string name_;
};

//实现类
class XiaomiCar : public Car {
public:
	XiaomiCar(string name) : Car(name) {}
	void Show() {
		cout << "获得一辆小米轿车: " << name_ << endl;
	}
};
//实现类
class HuaweiCar : public Car {
public:
	HuaweiCar(string name) : Car(name) {}
	void Show() {
		cout << "获得一辆华为轿车: " << name_ << endl;
	}
};

enum CarType
{
	XIAOMI, HUAWEI
};

//简单工厂类
class SimpleFactory {
public:
	Car* CreateCar(CarType type) {
		switch (type) {
		case XIAOMI:
			return new XiaomiCar("SU7");
		case HUAWEI:
			return new HuaweiCar("问界");
		default:
			return nullptr;
		}
	}
};

int main()
{
	SimpleFactory* factory = new SimpleFactory();
	Car* c1 = factory->CreateCar(XIAOMI);
	Car* c2 = factory->CreateCar(HUAWEI);
	c1->Show();
	c2->Show();

	delete c1;
	delete c2;
	delete factory;
}

运行结果:
在这里插入图片描述
上面小米汽车XiaomiCar 和华为汽车HuaweiCar 继承了汽车基类Car。
简单工厂SimpleFactory根据传入不同类型CarType来生产不同类型的车。

你是否想到了铁蛋要怎么做,铁蛋是这样做的:
1.增加长安汽车类(继承了汽车基类Car)
2.修改简单工厂类SimpleFactory(增加”CHANGAN“类型车的创建)

#include 
#include 
using namespace std;

class Car
{
public:
	Car(string name) : name_(name) {}
	virtual void Show() = 0;

protected:
	string name_;
};

class XiaomiCar : public Car {
public:
	XiaomiCar(string name) : Car(name) {}
	void Show() {
		cout << "获得一辆小米轿车: " << name_ << endl;
	}
};

class HuaweiCar : public Car {
public:
	HuaweiCar(string name) : Car(name) {}
	void Show() {
		cout << "获得一辆华为轿车: " << name_ << endl;
	}
};

class ChanganCar : public Car {
public:
	ChanganCar(string name) : Car(name) {}
	void Show() {
		cout << "获得一辆长安轿车: " << name_ << endl;
	}
};

enum CarType
{
	XIAOMI, HUAWEI, CHANGAN
};

class SimpleFactory {
public:
	Car* CreateCar(CarType type) {
		switch (type) {
		case XIAOMI:
			return new XiaomiCar("SU7");
		case HUAWEI:
			return new HuaweiCar("问界");
		case CHANGAN:
			return new ChanganCar("深蓝《偷偷藏不住》");
		default:
			return nullptr;
		}
	}
};

int main()
{
	cout << "------简单工厂------" << endl;
	SimpleFactory* factory = new SimpleFactory();
	Car* c1 = factory->CreateCar(XIAOMI);
	Car* c2 = factory->CreateCar(HUAWEI);
	Car* c3 = factory->CreateCar(CHANGAN);
	c1->Show();
	c2->Show();
	c3->Show();

	delete c1;
	delete c2;
	delete c3;
	delete factory;
}

运行结果:
设计模式:工厂方法模式(讲故事图文易懂)_第1张图片
成功生产了长安轿车!


但是从逻辑上来讲,同一个工厂不会生产小米汽车又生产华为汽车,又或者这个工厂还将生产长安汽车

因为:当简单工厂想增加生产长安汽车时,会修改简单工厂类SimpleFactory(增加”CHANGAN“类型车的创建),违反了开闭原则(对修改关闭,对扩展开放)。每当我们增加一种产品的时候就要去修改工厂方法,这样会破坏其内聚性,给维护带来额外开支。

所以,有了工厂方法模式来解决该问题。

工厂方法模式

定义:工厂基类负责定义创建对象的公共接口,子类工厂负责创建出具体的对象,来实现不同产品的工厂化创建。
设计模式有三大分类:创建型模式、结构型模式、行为型模式
工厂方法模式属于创建型模式
设计模式:工厂方法模式(讲故事图文易懂)_第2张图片
工厂方法模式不只有一个工厂每个工厂只生产一种特定的产品。这样做的好处是当以后需要增加新的产品时,直接新增加一个对应的工厂就可以,而不是去修改原有的工厂,符合编程原则的开闭原则

#include 
#include 
using namespace std;

class Car
{
public:
	Car(string name) : name_(name) {}
	virtual void Show() = 0;

protected:
	string name_;
};

class XiaomiCar : public Car {
public:
	XiaomiCar(string name) : Car(name) {}
	void Show() {
		cout << "获得一辆小米轿车: " << name_ << endl;
	}
};

class HuaweiCar : public Car {
public:
	HuaweiCar(string name) : Car(name) {}
	void Show() {
		cout << "获得一辆华为轿车: " << name_ << endl;
	}
};

class Factory {
public:
	virtual Car* CreateCar(string name) = 0;
};

class XiaomiFactory : public Factory
{
public:
	Car* CreateCar(string name)
	{
		return new XiaomiCar(name);
	}
};

class HuaweiFactory : public Factory
{
public:
	Car* CreateCar(string name)
	{
		return new HuaweiCar(name);
	}
};

int main()
{
	cout << "------工厂方法模式------" << endl;
	Factory* xiaomiFactory = new XiaomiFactory();
	Factory* huaweiFactory = new HuaweiFactory();
	Car* xiaomiCar = xiaomiFactory->CreateCar("SU7");
	Car* huaweiCar = huaweiFactory->CreateCar("问界");

	xiaomiCar->Show();
	huaweiCar->Show();

	delete xiaomiCar;
	delete huaweiCar;
	delete xiaomiFactory;
	delete huaweiFactory;
}

运行结果:
设计模式:工厂方法模式(讲故事图文易懂)_第3张图片

同样上面小米汽车XiaomiCar 和华为汽车HuaweiCar 继承了汽车基类Car。
小米工厂XiaomiFactory和华为工厂HuaweiFactory继承了工厂基类Factory。
小米工厂生产小米汽车,华为工厂生产华为汽车。

这样,铁蛋想生产”长安汽车“,就直接新建长安汽车工厂,由长安工厂来生产长安汽车,这样就不用修改小米工厂和华为工厂,满足程序设计的开闭原则

铁蛋是这样做的
1.增加了长安汽车类ChanganCar (继承了汽车基类Car)
2.增加了长安汽车工厂ChanganFactory (继承了工厂基类Factory)

#include 
#include 
using namespace std;

class Car
{
public:
	Car(string name) : name_(name) {}
	virtual void Show() = 0;

protected:
	string name_;
};

class XiaomiCar : public Car {
public:
	XiaomiCar(string name) : Car(name) {}
	void Show() {
		cout << "获得一辆小米轿车: " << name_ << endl;
	}
};

class HuaweiCar : public Car {
public:
	HuaweiCar(string name) : Car(name) {}
	void Show() {
		cout << "获得一辆华为轿车: " << name_ << endl;
	}
};

class Factory {
public:
	virtual Car* CreateCar(string name) = 0;
};

class XiaomiFactory : public Factory
{
public:
	Car* CreateCar(string name)
	{
		return new XiaomiCar(name);
	}
};

class HuaweiFactory : public Factory
{
public:
	Car* CreateCar(string name)
	{
		return new HuaweiCar(name);
	}
};

class ChanganCar : public Car {
public:
	ChanganCar(string name) : Car(name) {}
	void Show() {
		cout << "获得一辆长安轿车: " << name_ << endl;
	}
};

class ChanganFactory : public Factory
{
public:
	Car* CreateCar(string name)
	{
		return new ChanganCar(name);
	}
};

int main()
{
	cout << "------工厂方法模式------" << endl;
	Factory* xiaomiFactory = new XiaomiFactory();
	Factory* huaweiFactory = new HuaweiFactory();
	Factory* changanFactory = new ChanganFactory();
	Car* xiaomiCar = xiaomiFactory->CreateCar("SU7");
	Car* huaweiCar = huaweiFactory->CreateCar("问界");
	Car* changanCar = changanFactory->CreateCar("深蓝《偷偷藏不住》");

	xiaomiCar->Show();
	huaweiCar->Show();
	changanCar->Show();

	delete xiaomiCar;
	delete huaweiCar;
	delete changanCar;
	delete xiaomiFactory;
	delete huaweiFactory;
	delete changanFactory;
}

运行结果:
设计模式:工厂方法模式(讲故事图文易懂)_第4张图片


无广告,以汽车为例故事形式介绍了简单工厂和工厂方法模式。

谢谢观看,祝顺利!

你可能感兴趣的:(C++,设计模式,工厂方法模式,c++,简单工厂模式)