设计模式-创建型-工厂方法
原创
©著作权归作者所有:来自51CTO博客作者shixin_0125的原创作品,请联系作者获取转载授权,否则将追究法律责任
#pragma once
#ifndef FACTORY_H
#define FACTORY_H
class Product
{
public:
Product(){}
virtual ~Product(){}
};
class ConcreateProduct : public Product
{
public:
ConcreateProduct();
virtual ~ConcreateProduct();
};
class Factory
{
public:
Factory(){}
virtual ~Factory(){}
Product* CreateProduct();
protected:
virtual Product* FactoryMethod() = 0;
};
class ConcreateFactory : public Factory
{
public:
ConcreateFactory();
virtual ~ConcreateFactory();
protected:
virtual Product* FactoryMethod();
};
#endif
#include "StdAfx.h"
#include "factory_impl.h"
#include <iostream>
using namespace std;
ConcreateProduct::ConcreateProduct()
{
std::cout << "construction of ConcreateProduct\n";
}
ConcreateProduct::~ConcreateProduct()
{
std::cout << "destruction of ConcreateProduct\n";
}
Product* Factory::CreateProduct()
{
std::cout << "an operation of product\n";
Product* p = FactoryMethod();
return p;
}
ConcreateFactory::ConcreateFactory()
{
std::cout << "construction of ConcreateFactory\n";
}
ConcreateFactory::~ConcreateFactory()
{
std::cout << "destruction of ConcreateFactory\n";
}
Product* ConcreateFactory::FactoryMethod()
{
return new ConcreateProduct();
}
#include "stdafx.h"
#include <stdlib.h>
#include "factory_impl.h"
#include <memory>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
auto_ptr<Factory> fy(new ConcreateFactory());
auto_ptr<Product> pt(fy->CreateProduct());
system("pause");
return 0;
}