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

//产品父类声明
#pragma once
#include "iostream"
using namespace std;
class Product
{
public:
virtual void PrintfName();
Product(void);
~Product(void);
};
//产品A类
#pragma once
#include "product.h"
class ProductA :
public Product
{
public:
virtual void PrintfName();
ProductA(void);
~ProductA(void);
}; #include "ProductA.h" ProductA::ProductA(void)
{
} ProductA::~ProductA(void)
{
}
void ProductA::PrintfName()
{ cout<<"This is ProductA"<<endl;
}
//产品B类
#pragma once
#include "Product.h"
class ProductB :
public Product
{
public:
virtual void PrintfName();
ProductB(void);
~ProductB(void);
}; #include "ProductB.h" ProductB::ProductB(void)
{
}
ProductB::~ProductB(void)
{
}
void ProductB::PrintfName()
{
cout<<"This is ProductB"<<endl; }
//创建父类声明
#pragma once
#include "Product.h"
class Creator
{
public:
virtual Product* GetProduct()=;
Creator(void);
~Creator(void);
};
////产品A的创建
#pragma once
#include "ProductA.h"
#include "creator.h"
class CreatorA:
public Creator
{
public:
virtual ProductA* GetProduct();
CreatorA(void);
~CreatorA(void);
}; #include "CreatorA.h" CreatorA::CreatorA(void)
{
} ProductA* CreatorA::GetProduct()
{
return (new ProductA()); }
CreatorA::~CreatorA(void)
{
}
//产品B的创建
#pragma once
#include "creator.h"
#include "ProductB.h"
class CreatorB :
public Creator
{
public:
virtual ProductB* GetProduct();
CreatorB(void);
~CreatorB(void);
}; #include "CreatorB.h" CreatorB::CreatorB(void)
{
} CreatorB::~CreatorB(void)
{
}
ProductB *CreatorB::GetProduct()
{ return (new ProductB());
}
// Factory.cpp : 定义控制台应用程序的入口点。
//工厂模式调用 #include "stdafx.h"
#include "Creator.h"
#include "CreatorA.h"
#include "CreatorB.h"
#include "ProductA.h"
#include "ProductB.h"
int _tmain(int argc, _TCHAR* argv[])
{
Creator* ca=new CreatorA();
Creator* cb=new CreatorB();
Product* pa=ca->GetProduct();
Product* pb=cb->GetProduct();
pa->PrintfName();
pb->PrintfName();
getchar();
delete ca;
delete cb;
delete pa;
delete pb;
return ;
}

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAJoAAAAuCAIAAABfxpcjAAACBElEQVR4nO2ZW3KDMAxF2W531W20edRdQp6LABbRD810GCzJRrInQXPP6IMQfJF1aD7K8HUZ/+sT7JxhpfMD7BlG5wD2C3SGAjpDAZ2hyHWmDLpy9TFH/7YeZ47Uvz/QmaOEN4tjdS5vs7zZLnSuEhoadSZIsS2NSj+27D36PaRteTedxWE2G6xN5+pb6Snb+vS1zWF7NvfPjr4mJ2UUc+wYdLITkfZmaNefw45vqFDb41hq2zwfDfOPbVGnuUt/jnQ9a7GTwmI/KaN+gyJddernJfw5mxJeq7O4dhv9dJrb9ee8XKe0hcrr7bA604JVT0o3+So2R6FJjnSxElJcIu1XmYN0i9WZ/NgF/isUCugMBXSGAjpDAZ2hgM5QQGco8L6zPtCZI8W2zP++jFR439m7JT22zfRynUp6j4e0B++mU1reQed1pML7Tv2+7NxrclKG3qcLg052ItLeDL36c4rjkzbS41hpW+rTju2vk72g5kwN/pzi+IZMbb7QqbC+n+LyDXTVaWvUn7MpIZTOw3WkwvvOTsfLj61GJHK4TlR436kskfarzEG6RXGzLlidYK8cbhMVdEbgeJuO0BkG0nmEzhhAZyhOt4kKOiNwuk9U0BkB6AzF+T5RQWcEzveZCjojcH7MVNAZgZ/HTAWdEUiPmQo6I5CeMxV0RuD3OVNBZwD+AH+je5Y0sZi6AAAAAElFTkSuQmCC" alt="" />

上一篇:Virut样本取证特征


下一篇:Wall Painting HDU - 4810(按位算贡献)