// Factory.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
using namespace std;

class IProduct
{
public:
 virtual void doSomething() = 0;
};

class ProductA: public IProduct
{
public:
 void doSomething()
 {
  std::cout<<"Product A doSomething"<<endl;
 }
};

class ProductB: public IProduct
{
public:
 void doSomething()
 {
  std::cout<<"Product B doSomething"<<endl;
 }
};

class IFactory
{
public:
 virtual IProduct * createProduct() = 0;
};

class FactoryA: public IFactory
{
public:
 IProduct * createProduct()
 {
  return new ProductA();
 }
};

class FactoryB: public IFactory
{
public:
 IProduct *createProduct()
 {
  return new ProductB();
 }
};

int _tmain(int argc, _TCHAR* argv[])
{
 
 // Create productA
 //
 FactoryA factoryA;
 IProduct *product = factoryA.createProduct();
 product->doSomething();
 delete product;

 FactoryB factoryB;
 product = factoryB.createProduct();
 product->doSomething();
 delete product;

 system("pause");
 return 0;
}

 

posted on 2011-11-22 17:01  Just a Programer  阅读(326)  评论(0编辑  收藏  举报