C++实现简单工厂和工厂方法以及抽象工厂

工厂类型设计模式

new给封装起来,即对象的创建封装起来

一种创建型设计模式

简单工厂

假设场景:用工厂来创建不同的汽车

优点:把对象的创建封装在一个接口函数里面,通过传入不同标识来返回不同的对象。客户不用自己负责创建对象(new),不用了解创建对象的详细过程

实现代码如下:

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

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

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


enum CarType
{
	BMW, AUDI
};
class SimpleFactory
{
public:
	Car* createCar(CarType ct)
	{
		switch (ct)
		{
		case BMW:
			return new Bmw("X1");
		case AUDI:
			return new Audi("Q7");
		default:
			cerr << "传入工厂的参数不正确:" << ct << endl;
			break;
		}
		return nullptr;
	}
};

int main()
{
	// 不好,暴露了对象的创建
	/*Car* p1 = new Bmw("X1");
	Car* p2 = new Audi("Q7");*/

	unique_ptr<SimpleFactory> factory(new SimpleFactory()); // 创建工厂
	unique_ptr<Car> p1(factory->createCar(BMW));
	unique_ptr<Car> p2(factory->createCar(AUDI));
	p1->show();
	p2->show();
	return 0;
}

缺点:所有的产品都在一个工厂创建,提供创建对象实例的接口函数不闭合,不满足开闭原则中对修改封闭

工厂方法

优点:设计一个Factory基类,提供了一个纯虚函数(创建产品),定义派生类(具体产品的工厂)负责创建对应的产品,可以做到不同产品在不同的工厂里创建,能够对现有的工厂以及产品的修改关闭。实现代码如下

// 工厂方法
class Car
{
public:
	Car(string name) :_name(name) {}
	virtual void show() = 0;
protected:
	string _name;
};

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

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

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

class BmwFactory : public Factory
{
public:
	Car* createCar(string name)
	{
		return new Bmw(name);
	}
};
class AudiFactory : public Factory
{
public:
	Car* createCar(string name)
	{
		return new Audi(name);
	}
};

int main()
{
	unique_ptr<Factory> bmwfty(new BmwFactory()); // 创建工厂
	unique_ptr<Factory> audifty(new AudiFactory()); // 创建工厂
	unique_ptr<Car> p1(bmwfty->createCar("X6"));
	unique_ptr<Car> p2(audifty->createCar("A6"));
	p1->show();
	p2->show();
	return 0;
}

工厂方法缺点:实际上,很多产品时有关联关系的,属于一个产品族,不应该放在不同的工厂中创建(如苹果手机和耳机应该在同一个工厂生产)。这样一是不符合实际产品对象的创建逻辑,二是会造成类的数量的膨胀

抽象工厂

优点:将有关联关系的属于一个产品族的所有产品创建接口函数放在一个抽象工厂类AbstractFactory中,派生类(具体产品的工厂)应该负责创建该产品族的所有产品.

即宝马工厂也生产宝马车灯和宝马座椅,奥迪工厂也生产奥迪车灯和奥迪座椅

实现代码:

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

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

class Audi : public Car
{
public:
	Audi(string name) :Car(name) {}
	void show()
	{
		cout << "获取了一辆奥迪汽车:" << _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* createCarLight() = 0;
};

class BmwFactory : public AbstractFactory
{
public:
	Car* createCar(string name)
	{
		return new Bmw(name);
	}
	Light* createCarLight()
	{
		return new BmwLight();
	}
};
class AudiFactory : public AbstractFactory
{
public:
	Car* createCar(string name)
	{
		return new Audi(name);
	}
	Light* createCarLight()
	{
		return new AudiLight();
	}
};

int main()
{

	unique_ptr<AbstractFactory> bmwfty(new BmwFactory()); // 创建工厂
	unique_ptr<AbstractFactory> audifty(new AudiFactory()); // 创建工厂
	unique_ptr<Car> p1(bmwfty->createCar("X6"));
	unique_ptr<Car> p2(audifty->createCar("A6"));
	unique_ptr<Light> l1(bmwfty->createCarLight());
	unique_ptr<Light> l2(audifty->createCarLight());

	p1->show();
	l1->show();

	p2->show();
	l2->show();
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
简单工厂模式(Simple Factory Pattern)是一种创建型设计模式,它提供了一种创建对象的最佳方式。在简单工厂模式中,我们创建了一个工厂类,该工厂类负责创建其他类的对象,而不是使用 new 操作符直接实例化对象。这种方法将类的实例化过程从客户端代码中分离出来,使客户端代码与实例化过程解耦。下面是一个简单的 C++ 实现示例: ```cpp #include <iostream> using namespace std; // 抽象产品类 class Product { public: virtual void use() = 0; }; // 具体产品类 A class ProductA : public Product { public: void use() { cout << "Product A used." << endl; } }; // 具体产品类 B class ProductB : public Product { public: void use() { cout << "Product B used." << endl; } }; // 工厂类 class Factory { public: static Product* createProduct(int type) { switch (type) { case 1: return new ProductA(); case 2: return new ProductB(); default: return NULL; } } }; int main() { Product* productA = Factory::createProduct(1); Product* productB = Factory::createProduct(2); productA->use(); // Product A used. productB->use(); // Product B used. delete productA; delete productB; return 0; } ``` 抽象工厂模式(Abstract Factory Pattern)是一种创建型设计模式,它提供了一种创建相关或依赖对象族的最佳方式,而无需指定它们的具体类。在抽象工厂模式中,我们创建了一个抽象工厂类,该工厂类负责创建一组相关对象,例如各种形状的按钮和文本框,而不是使用 new 操作符直接实例化对象。这种方法将类的实例化过程从客户端代码中分离出来,使客户端代码与实例化过程解耦。下面是一个简单的 C++ 实现示例: ```cpp #include <iostream> using namespace std; // 抽象产品类 A class ProductA { public: virtual void use() = 0; }; // 具体产品类 A1 class ProductA1 : public ProductA { public: void use() { cout << "Product A1 used." << endl; } }; // 具体产品类 A2 class ProductA2 : public ProductA { public: void use() { cout << "Product A2 used." << endl; } }; // 抽象产品类 B class ProductB { public: virtual void use() = 0; }; // 具体产品类 B1 class ProductB1 : public ProductB { public: void use() { cout << "Product B1 used." << endl; } }; // 具体产品类 B2 class ProductB2 : public ProductB { public: void use() { cout << "Product B2 used." << endl; } }; // 抽象工厂类 class Factory { public: virtual ProductA* createProductA() = 0; virtual ProductB* createProductB() = 0; }; // 具体工厂类 1 class Factory1 : public Factory { public: ProductA* createProductA() { return new ProductA1(); } ProductB* createProductB() { return new ProductB1(); } }; // 具体工厂类 2 class Factory2 : public Factory { public: ProductA* createProductA() { return new ProductA2(); } ProductB* createProductB() { return new ProductB2(); } }; int main() { Factory* factory1 = new Factory1(); Factory* factory2 = new Factory2(); ProductA* productA1 = factory1->createProductA(); ProductB* productB1 = factory1->createProductB(); ProductA* productA2 = factory2->createProductA(); ProductB* productB2 = factory2->createProductB(); productA1->use(); // Product A1 used. productB1->use(); // Product B1 used. productA2->use(); // Product A2 used. productB2->use(); // Product B2 used. delete productA1; delete productB1; delete productA2; delete productB2; delete factory1; delete factory2; return 0; } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值