#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();
}


// Factory.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <stdlib.h>
#include "factory_impl.h"
#include <memory>
using namespace std;
//定义一个用于创建对象的接口,让子类决定实例化哪一个类。Factory Method使一个类的实例化延迟到其子类。
int _tmain(int argc, _TCHAR* argv[])
{
//udp_factory,tcp_factory,rtsp_factory

auto_ptr<Factory> fy(new ConcreateFactory());

auto_ptr<Product> pt(fy->CreateProduct()); //决定实例化哪一个类,使一个类的实例化延迟到其子类

system("pause");

return 0;
}