代码片断1:Product.h //Product.h
#ifndef _PRODUCT_H_
#define _PRODUCT_H_
class Product
{
public:
virtual ~Product() =0;
protected: Product();
};
class ConcreteProduct: public Product
{
public:
ConcreteProduct();
~ConcreteProduct();
};
#endif //~_PRODUCT_H_
//==============================
代码片断2:Product.cpp //Product.cpp
#include "Product.h"
#include
using namespace std;
Product::Product()
{
}
Product::~Product()
{
}
ConcreteProduct::ConcreteProduct()
{
cout<<"ConcreteProduct...."<}
ConcreteProduct::~ConcreteProduct()
{
}
//=============================
代码片断3:Factory.h //Factory.h
#ifndef _FACTORY_H_
#define _FACTORY_H_
class Product;
class Factory
{
public:
virtual ~Factory() = 0;
virtual Product* CreateProduct() = 0;
protected:
Factory();
private:
};
class ConcreteFactory:public Factory
{
public:
~ConcreteFactory();
ConcreteFactory();
Product* CreateProduct();
protected:
private:
};
#endif //~_FACTORY_H_
//================================
代码片断4:Factory.cpp //Factory.cpp
#include "Factory.h"
#include "Product.h"
#include
using namespace std;
Factory::Factory()
{
}
Factory::~Factory()
{
}
ConcreteFactory::ConcreteFactory()
{
cout << "ConcreteFactory....." << endl;
}
ConcreteFactory::~ConcreteFactory()
{
}
Product* ConcreteFactory::CreateProduct()
{
return new ConcreteProduct();
}
//===============================
代码片断5:main.cpp //main.cpp
#include "Factory.h"
#include "Product.h"
#include
using namespace std;
int main(int argc,char* argv[])
{
Factory *fac = new ConcreteFactory();
Product *p = fac->CreateProduct();
return 0;
}
阅读(1336) | 评论(0) | 转发(0) |