设计模式--Factory

简单工厂模式

实现代码:

#include <iostream>
using namespace std;

enum ProductType {PA, PB};

class Product
{
public:
	Product()
	{
		cout << "Product ..." << endl;		
	}
	virtual void show() = 0;
};

class ProductA : public Product
{
public:
	ProductA()
	{
		cout << "ProductA ..." << endl;
	}
	void show()
	{
		cout << "show A ..." << endl;
	}
}; 

class ProductB : public Product
{
public:
	ProductB()
	{
		cout << "ProductB ..." << endl;
	}
	void show()
	{
		cout << "show B ..." << endl;
	}
};

class Factory
{
public:
	Factory()
	{
		cout << "Factory ..." << endl;
	}
	
	// 
	Product *createProduct(enum ProductType type)
	{
		if (type == PA)
		{
			return new ProductA();
		}
		else if (type == PB)
		{
			return new ProductB();
		}
		else
		{
			return NULL;
		}
	}
};

int main()
{
	Factory *factory = new Factory();
	Product *productA = factory->createProduct(PA);
	productA->show();
	Product *productB = factory->createProduct(PB);
	productB->show();
	return 0;
}

UML类图:



工厂方法模式

实现代码:

#include <iostream>
using namespace std;

enum ProductType {PA, PB};

class Product
{
public:
	Product()
	{
		cout << "Product ..." << endl;		
	}
	virtual void show(void) = 0;
};

class ProductA : public Product
{
public:
	ProductA()
	{
		cout << "ProductA ..." << endl;
	}
	void show(void)
	{
		cout << "show A ..." << endl;
	}
}; 

class ProductB : public Product
{
public:
	ProductB()
	{
		cout << "ProductB ..." << endl;
	}
	void show(void)
	{
		cout << "show B ..." << endl;
	}
};

class Factory
{
public:
	Factory()
	{
		cout << "Factory ..." << endl;
	}
	
	virtual Product *createProduct(void) = 0;
};

class FactoryA : public Factory
{
public:
	FactoryA()
	{
		cout << "FactoryA ..." << endl;
	}
	Product *createProduct(void)
	{
		return new ProductA();
	}
}; 

class FactoryB : public Factory
{
public:
	FactoryB()
	{
		cout << "FactoryB ..." << endl;
	}
	Product *createProduct(void)
	{
		return new ProductB();
	}
};

int main()
{
	Factory *factoryA = new FactoryA();
	Product *productA = factoryA->createProduct();
	productA->show();
	
	Factory *factoryB = new FactoryB();
	Product *productB = factoryB->createProduct();
	productB->show();
	return 0;
}

UML类图:


抽象工厂模式

实现代码:

#include <iostream>
using namespace std;

enum ProductType {PAX, PAY, PBX, PBY};

class ProductA
{
public:
	ProductA()
	{
		cout << "ProductA ..." << endl;
	}
	virtual void show(void) = 0;
}; 

class ProductAX : public ProductA
{
public:
	ProductAX()
	{
		cout << "ProductAX ..." << endl;
	}
	void show(void)
	{
		cout << "show AX ..." << endl;
	}
};

class ProductAY : public ProductA
{
public:
	ProductAY()
	{
		cout << "ProductAY ..." << endl;
	}
	void show(void)
	{
		cout << "show AY ..." << endl;
	}
};

class ProductB
{
public:
	ProductB()
	{
		cout << "ProductB ..." << endl;
	}
	virtual void show(void) = 0;
};

class ProductBX : public ProductB
{
public:
	ProductBX()
	{
		cout << "ProductBX ..." << endl;
	}
	void show(void)
	{
		cout << "show BX ..." << endl;
	}
};

class ProductBY : public ProductB
{
public:
	ProductBY()
	{
		cout << "ProductBY ..." << endl;
	}
	void show(void)
	{
		cout << "show BY ..." << endl;
	}
};

class Factory
{
public:
	Factory()
	{
		cout << "Factory ..." << endl;
	}
	
	virtual ProductA *createProductA(void) = 0;
	virtual ProductB *createProductB(void) = 0;

};

class FactoryX : public Factory
{
public:
	FactoryX()
	{
		cout << "FactoryX ..." << endl;
	}
	ProductA *createProductA(void)
	{
		return new ProductAX();
	}
	ProductB *createProductB(void)
	{
		return new ProductBX();
	}
}; 

class FactoryY : public Factory
{
public:
	FactoryY()
	{
		cout << "FactoryY ..." << endl;
	}
	ProductA *createProductA(void)
	{
		return new ProductAY();
	}
	ProductB *createProductB(void)
	{
		return new ProductBY();
	}
}; 

int main()
{
	Factory *factoryX = new FactoryX();
	ProductA *productAX = factoryX->createProductA();
	productAX->show();
	ProductB *productBX = factoryX->createProductB();
	productBX->show();
	
	Factory *factoryY = new FactoryY();
	ProductA *productAY = factoryY->createProductA();
	productAY->show();
	ProductB *productBY = factoryY->createProductB();
	productBY->show();
	
	return 0;
}

UML类图:



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值