C++ 设计模式--工厂模式

工厂模式:利用C++多态的特性,对对象的创建进行封装。

定义一个创建对象的接口,让其子类自己决定实力化哪一个工厂类,工厂模式使其创建过程延迟到子类进行

参考链接

  1. 简单工厂
cpp 复制代码
#include <iostream>
#include <string>
using namespace std;


class Car
{
public:
	Car(string name) : m_name(name) {}
	~Car() {}
	virtual void show() = 0;

protected:
	string m_name;
};

class Bmw : public Car
{
public:
	Bmw(string name) : Car(name){}
	void show()
	{
		cout << "获取了一辆宝马汽车: " << m_name << endl;
	}

};

class Audi : public Car
{
public:
	Audi(string name) : Car(name) {}
	void show()
	{
		cout << "获取了一辆奥迪汽车: " << m_name << endl;
	}

};

// 简单工厂
enum carType
{
	BMW,
	AUDI,
};

class simpleFactory
{
public:
	Car* createCar(carType ct)
	{
		switch (ct)
		{
		case BMW:
			return new Bmw("x6");
		case AUDI:
			return new Audi("a8");
		default:
			cerr << "传入的参数错误" << ct << endl;
		}
		return nullptr;
	}

};
int main()
{
	// 智能指针 在使用了new以后不需要delete
	/*unique_ptr<simpleFactory> fac(new simpleFactory());
	unique_ptr<Car> p1(fac->createCar(BMW));
	unique_ptr<Car> p2(fac->createCar(AUDI));*/

	simpleFactory* fac = new simpleFactory();
	Car* p1 = fac->createCar(BMW);
	Car* p2 = fac->createCar(AUDI);

	p1->show();
	p2->show();

	return 0;
}
  1. 工厂方法
cpp 复制代码
#include <iostream>
#include <string>
using namespace std;
class Car
{
public:
	Car(string name) : m_name(name) {}
	~Car() {}
	virtual void show() = 0;

protected:
	string m_name;
};

class Bmw : public Car
{
public:
	Bmw(string name) : Car(name){}
	void show()
	{
		cout << "获取了一辆宝马汽车: " << m_name << endl;
	}

};

class Audi : public Car
{
public:
	Audi(string name) : Car(name) {}
	void show()
	{
		cout << "获取了一辆奥迪汽车: " << m_name << endl;
	}

};

// 工厂方法
// 基类
class factory
{
public:
	virtual Car* createCar(string name) = 0;

};

// 子类1
class BmwFac : public factory
{
public:
	Car* createCar(string name)
	{
		return new Bmw(name);
	}
};
// 子类2
class AudiFac : public factory
{
public:
	Car* createCar(string name)
	{
		return new Audi(name);
	}
};

int main()
{
	unique_ptr<factory> bmwFty(new BmwFac());
	unique_ptr<factory> audiFty(new AudiFac());
	unique_ptr<Car> p3(bmwFty->createCar("x7"));
	unique_ptr<Car> p4(audiFty->createCar("a9"));

	p3->show();
	p4->show();

	return 0;
}
  1. 抽象工厂
cpp 复制代码
#include <iostream>
#include <string>
using namespace std;
class Car
{
public:
	Car(string name) : m_name(name) {}
	~Car() {}
	virtual void show() = 0;

protected:
	string m_name;
};

class Bmw : public Car
{
public:
	Bmw(string name) : Car(name){}
	void show()
	{
		cout << "获取了一辆宝马汽车: " << m_name << endl;
	}

};

class Audi : public Car
{
public:
	Audi(string name) : Car(name) {}
	void show()
	{
		cout << "获取了一辆奥迪汽车: " << m_name << endl;
	}

};

// 抽象工厂 
// 增加另外一个类
class Light
{
public:
	virtual void show() = 0;

};

class BmwLight : public Light
{
public:
	void show()
	{
		cout << "BMW Light" << endl;
	}

};

class AudiLight : public Light
{
public:
	void show()
	{
		cout << "AUDI Light" << endl;
	}
};

class AbstractFactory
{
public:
	virtual Car* createCar(string name) = 0;
	virtual Light* createLight() = 0;

};

class BMWFactory : public AbstractFactory
{
public:
	Car* createCar(string name)
	{
		return new Bmw(name);
	}

	Light* createLight()
	{
		return new BmwLight();
	}

};

class AUDIFactory : public AbstractFactory
{
public:
	Car* createCar(string name)
	{
		return new Audi(name);
	}

	Light* createLight()
	{
		return new AudiLight();
	}

};

int main()
{
	unique_ptr<AbstractFactory> bmwfty(new BMWFactory());
	unique_ptr<AbstractFactory> audifty(new AUDIFactory());
	unique_ptr<Car> p5(bmwfty->createCar("x8"));
	unique_ptr<Car> p6(audifty->createCar("a10"));
	unique_ptr<Light> p7(bmwfty->createLight());
	unique_ptr<Light> p8(audifty->createLight());

	p5->show();
	p6->show();
	p7->show();
	p8->show();
	return 0;
}

其实感觉对于工厂模式的实现,看到第二种方式就差不多了,第三种只是在第二种基础上再增加一个类而已。之前在前司的时候项目中的代码有看到过这种设计模式的,当时还不了解设计模式,当时调代码的时候一步一步的走到了父类,父类还是一个模版,所以印象深刻。

相关推荐
A阳俊yi1 分钟前
设计模式——结构型模式
设计模式
混分巨兽龙某某31 分钟前
基于Qt Creator的Serial Port串口调试助手项目(代码开源)
c++·qt creator·串口助手·serial port
小冯记录编程42 分钟前
C++指针陷阱:高效背后的致命危险
开发语言·c++·visual studio
C_Liu_1 小时前
C++:类和对象(下)
开发语言·c++
coderxiaohan2 小时前
【C++】类和对象1
java·开发语言·c++
阿昭L2 小时前
MFC仿真
c++·mfc
老赵的博客4 小时前
c++ unqiue指针
java·jvm·c++
程序猿编码5 小时前
基于 Linux 内核模块的字符设备 FIFO 驱动设计与实现解析(C/C++代码实现)
linux·c语言·c++·内核模块·fifo·字符设备
怎么没有名字注册了啊5 小时前
MFC_Install_Create
c++·mfc
Wadli6 小时前
C++语法 | static静态|单例模式
开发语言·c++·单例模式