抽象工厂模式

原文链接:http://www.cnblogs.com/cxrs/archive/2011/11/22/2259056.html

// 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;
}

 

转载于:https://www.cnblogs.com/cxrs/archive/2011/11/22/2259056.html

上一篇:Java编程思想学习笔记


下一篇:Java编程思想白话一:对象导论