AbstractFactory 模式-LMLPHP

 ///////////////////////Product.h//////////////
#ifndef _PRODUCT_H_
#define _PRODUCT_H_
class AbstractProductA
{
public:
virtual ~AbstractProductA();
protected:
AbstractProductA();
private:
}; class AbstractProductB
{
public:
virtual ~AbstractProductB();
protected:
AbstractProductB();
private:
}; class ProductA1 : public AbstractProductA
{
public:
~ProductA1();
ProductA1();
protected:
private:
}; class ProductA2 : public AbstractProductA
{
public:
~ProductA2();
ProductA2();
protected:
private:
}; class ProductB1 : public AbstractProductB
{
public:
~ProductB1();
ProductB1();
protected:
private:
}; class ProductB2 : public AbstractProductB
{
public:
~ProductB2();
ProductB2();
protected:
private:
}; #endif
 ///////////////////////Product.cpp//////////////
#include "Product.h"
#include <iostream>
using namespace std; AbstractProductA::~AbstractProductA()
{ }
AbstractProductA::AbstractProductA()
{ }
AbstractProductB::~AbstractProductB()
{ }
AbstractProductB::AbstractProductB()
{ } ProductA1::~ProductA1()
{ }
ProductA1::ProductA1()
{
cout<<"ProductA1"<<endl;
} ProductA2::~ProductA2()
{ }
ProductA2::ProductA2()
{
cout<<"ProductA2"<<endl;
} ProductB1::~ProductB1()
{ }
ProductB1::ProductB1()
{
cout<<"ProductB1"<<endl;
}
ProductB2::~ProductB2()
{ }
ProductB2::ProductB2()
{
cout<<"ProductB2"<<endl;
}
 ///////////////////////Factory.h//////////////
#ifndef _FACTORY_H_
#define _FACTORY_H_ class AbstractProductA;
class AbstractProductB; class AbstractFactory
{
public:
virtual ~AbstractFactory();
virtual AbstractProductA* CreatProductA()=;
virtual AbstractProductB* CreatProductB()=;
protected:
AbstractFactory();
private:
}; class ConcreteFactory1 : public AbstractFactory
{
public:
~ConcreteFactory1();
ConcreteFactory1();
AbstractProductA* CreatProductA();
AbstractProductB* CreatProductB();
protected:
private:
}; class ConcreteFactory2 : public AbstractFactory
{
public:
~ConcreteFactory2();
ConcreteFactory2();
AbstractProductA* CreatProductA();
AbstractProductB* CreatProductB();
protected:
private:
}; #endif
 ///////////////////////Factory.cpp//////////////
#include "Factory.h"
#include "Product.h" AbstractFactory::~AbstractFactory()
{ }
AbstractFactory::AbstractFactory()
{ } ConcreteFactory1::~ConcreteFactory1(){}
ConcreteFactory1::ConcreteFactory1(){}
AbstractProductA* ConcreteFactory1::CreatProductA()
{
return new ProductA1();
} AbstractProductB* ConcreteFactory1::CreatProductB()
{
return new ProductB1();
} ConcreteFactory2::~ConcreteFactory2(){}
ConcreteFactory2::ConcreteFactory2(){}
AbstractProductA* ConcreteFactory2::CreatProductA()
{
return new ProductA2();
} AbstractProductB* ConcreteFactory2::CreatProductB()
{
return new ProductB2();
}
 ///////////////////////main.cpp//////////////
#include "Factory.h"
#include <iostream> int main()
{
AbstractFactory* fac1 = new ConcreteFactory1();
fac1->CreatProductA();
fac1->CreatProductB(); AbstractFactory* fac2 = new ConcreteFactory2();
fac2->CreatProductA();
fac2->CreatProductB(); getchar(); return ;
}
05-11 13:37