设计模式一:工厂模式

  1. 简单工厂模式
#include
#include
#include

using namespace std;
typedef enum TypeProduct
{
	enumProductA,
	enumProductB,
	enumProductC
}PRODUCTTYPE;


class Product
{
public:

	Product()
	{
		
	
	}

	virtual ~Product()
	{
		
	
	}

	virtual void Show() 
	{
		std::cout << "product base" << std::endl;
	
	}
};


class ProductA : public Product
{
public:
	void Show()
	{
		cout << "I'm ProductA" << endl;
	}
};

class ProductB : public Product
{
public:
	void Show()
	{
		cout << "I'm ProductB" << endl;
	}
};

class ProductC : public Product
{
public:
	void Show()
	{
		cout << "I'm ProductC" << endl;
	}
};

class factory
{
public:
	std::shared_ptr createProduct(PRODUCTTYPE type)
	{
		switch (type)
		{
		case enumProductA:
		{
			std::shared_ptrpProductA;
			pProductA = std::make_shared();
			return pProductA;
		
		}
			
		
		case enumProductB:
		{
		
			std::shared_ptrpProductB;
			pProductB = std::make_shared();
			return pProductB;
		}
			

		case enumProductC:
		{
			std::shared_ptrpProductC;
			pProductC = std::make_shared();
			return pProductC;
		}

		default:
			return NULL;
		
			

		}
		
			
		
		
	}

};



int main()
{
	std::unique_ptrfac;
	fac= std::make_unique();
	std::shared_ptrproA = fac->createProduct(enumProductA);
	if (proA != NULL)
	{
		proA->Show();
	}

	std::shared_ptrproB = fac->createProduct(enumProductB);
	if (proB != NULL)
	{
		proB->Show();
	}
	

	std::shared_ptrproC = fac->createProduct(enumProductC);
	if (proC != NULL)
	{
		proC->Show();
	}
	



	return 0;
}
  1. 工厂方法模式
#include
#include

class productA
{
public:
	productA()
	{
	}

	virtual ~productA()
	{
	}

	virtual void show() = 0;

};

class productA1:public productA
{
public:
	void show()
	{

		std::cout << " i am productA1" << std::endl;
	}

};

class productA2 :public productA
{
public:
	void show()
	{
		std::cout << " i am productA2" << std::endl;
	}

};

class productB
{
public:
	productB()
	{

	}
	virtual ~productB()
	{
	}
	virtual void show() = 0;
};

class productB1 :public productB
{
public:
	void show()
	{
		std::cout << "i am productB1" << std::endl;

	}

};

class productB2 :public productB
{
public:
	void show()
	{
		std::cout << "i am productB2" << std::endl;

	}

};


class factory
{
public:
	factory()
	{
	}
	virtual ~factory()
	{
	}

	virtual std::shared_ptr createProductA() = 0;
	virtual std::shared_ptr createProductB() = 0;

};

class factory1 :public factory
{
public:
	std::shared_ptr createProductA()
	{
		std::shared_ptrpProA1;
		pProA1 = std::make_shared();
		return pProA1;
	
	}

	std::shared_ptr createProductB()
	{
		std::shared_ptrpProB2;
		pProB2 = std::make_shared();
		return pProB2;

	}


};
int main()
{
	std::shared_ptrfac = std::make_shared();
	std::shared_ptr < productA >pProA= fac->createProductA();
	std::shared_ptrpProB = fac->createProductB();
	pProA->show();
	pProB->show();


	return 0;
}

3.抽象工厂

#include
#include
#include

class product
{
public:

	product()
	{
	
	}

	virtual ~product()
	{
	

	}
	virtual void show()
	{
		std::cout << "base product" << std::endl;
	
	}


};

class productA :public product
{
public:
	void show()
	{
		std::cout << "I am productA" << std::endl;
	
	}

};

class productB :public product
{

public:
	void show()
	{
	
		std::cout << "I am productB" << std::endl;

	}

};

class factory
{
	virtual std::shared_ptr createProduct() = 0;

};

class factoryA :public factory
{
public:
	std::shared_ptr createProduct()
	{
		std::shared_ptrpProductA;
		pProductA = std::make_shared();
		return pProductA;

	
	}

};

class factoryB :public factory
{
public:

	std::shared_ptrcreateProduct()
	{
		std::shared_ptrpProductB;
		pProductB = std::make_shared();
		return pProductB;

	}

};

int main()
{
	std::unique_ptrpfacA = std::make_unique();
	std::unique_ptrpfacB = std::make_unique();

	std::shared_ptr pProA= pfacA->createProduct();
	std::shared_ptrpProB = pfacB->createProduct();
	pProA->show();
	pProB->show();


	return 0;
}

你可能感兴趣的:(c++,设计模式,C++)