一、定义抽象产品接口
//+------------------------------------------------------------------+
//| participants |
//+------------------------------------------------------------------+
// abstract product > declares an interface for a type of products
//+------------------------------------------------------------------+
//| participants > abstract product |
//+------------------------------------------------------------------+
interface AbstractProductA
{
};
//+------------------------------------------------------------------+
//| participants > abstract product |
//+------------------------------------------------------------------+
interface AbstractProductB
{
void Interact(AbstractProductA*);
};
二、定义抽象工厂接口
//+------------------------------------------------------------------+
//| participants |
//+------------------------------------------------------------------+
interface AbstractFactory
// declares an interface for operations that create abstract products
{
AbstractProductA* CreateProductA(void);
AbstractProductB* CreateProductB(void);
};
三、定义具体产品
//+------------------------------------------------------------------+
//| participants |
//+------------------------------------------------------------------+
// concrete product
// defines > product object to be created > by concrete factory
// implements > abstract product interface
//+------------------------------------------------------------------+
//| participants > concrete product |
//+------------------------------------------------------------------+
class ProductA1:public AbstractProductA
{
public:
ProductA1(void);
};
void ProductA1::ProductA1(void) {Print("product a1 constructed");}
//+------------------------------------------------------------------+
//| participants > concrete product |
//+------------------------------------------------------------------+
class ProductA2:public AbstractProductA
{
public:
ProductA2(void);
};
void ProductA2::ProductA2(void) {Print("product a2 constructed");}
//+------------------------------------------------------------------+
//| participants > concrete product |
//+------------------------------------------------------------------+
class ProductB1:public AbstractProductB
{
public:
ProductB1(void);
void Interact(AbstractProductA*);
};
void ProductB1::ProductB1(void) {Print("product b1 constructed");}
void ProductB1::Interact(AbstractProductA*src)
{
Print("product b1: ",&this," is interacting with product a: ",src);
}
//+------------------------------------------------------------------+
//| participants > concrete product |
//+------------------------------------------------------------------+
class ProductB2:public AbstractProductB
{
public:
ProductB2(void);
void Interact(AbstractProductA*);
};
void ProductB2::ProductB2(void) {Print("product b2 constructed");}
void ProductB2::Interact(AbstractProductA*src)
{
Print("product b2: ",&this," is interacting with product a: ",src);
}
//
//
四、定义具体工厂
//+------------------------------------------------------------------+
//| participants |
//+------------------------------------------------------------------+
// concrete factory > implements operations > create concrete products
//+------------------------------------------------------------------+
//| participants > concrete factory |
//+------------------------------------------------------------------+
class Factory1:public AbstractFactory
{
public:
Factory1(void);
AbstractProductA* CreateProductA(void);
AbstractProductB* CreateProductB(void);
};
//+------------------------------------------------------------------+
//| participants > concrete factory > factory 1 |
//+------------------------------------------------------------------+
void Factory1::Factory1(void)
{
Print("factory 1: ",&this," constructed");
}
//+------------------------------------------------------------------+
//| participants > concrete factory > factory 1 |
//+------------------------------------------------------------------+
AbstractProductA* Factory1::CreateProductA(void)
{
printf("factory 1 is creating and returning product a1");
return new ProductA1;
}
//+------------------------------------------------------------------+
//| participants > concrete factory > factory 1 |
//+------------------------------------------------------------------+
AbstractProductB* Factory1::CreateProductB(void)
{
printf("factory 1 is creating and returning product b1");
return new ProductB1;
}
//+------------------------------------------------------------------+
//| participants > concrete factory |
//+------------------------------------------------------------------+
class Factory2:public AbstractFactory
{
public:
Factory2(void);
AbstractProductA* CreateProductA(void);
AbstractProductB* CreateProductB(void);
};
//+------------------------------------------------------------------+
//| participants > concrete factory > factory 2 |
//+------------------------------------------------------------------+
void Factory2::Factory2(void)
{
Print("factory 2: ",&this," constructed");
}
//+------------------------------------------------------------------+
//| participants > concrete factory > factory 2 |
//+------------------------------------------------------------------+
AbstractProductA* Factory2::CreateProductA(void)
{
printf("factory 2 is creating and returning product a2");
return new ProductA2;
}
//+------------------------------------------------------------------+
//| participants > concrete factory > factory 2 |
//+------------------------------------------------------------------+
AbstractProductB* Factory2::CreateProductB(void)
{
printf("factory 2 is creating and returning product b2");
return new ProductB2;
}
五、定义工厂客户端
//+------------------------------------------------------------------+
//| participants |
//+------------------------------------------------------------------+
class FactoryClient
// uses interfaces > declared by > abstract factory, abstract product
{
public:
void Run(void);
void Switch(AbstractFactory*);
FactoryClient(AbstractFactory*);
~FactoryClient(void);
protected:
AbstractProductA* apa;
AbstractProductB* apb;
AbstractFactory* factory;
void Delete(void);
};
//+------------------------------------------------------------------+
//| participants > factory client |
//+------------------------------------------------------------------+
void FactoryClient::FactoryClient(AbstractFactory* af)
{
Print("factory client created and received abstract factory ",af);
Print("factory client is requesting to accept/switch the factories");
Switch(af);
}
//+------------------------------------------------------------------+
//| participants > factory client |
//+------------------------------------------------------------------+
void FactoryClient::~FactoryClient(void)
{
Delete();
}
//+------------------------------------------------------------------+
//| participants > factory client |
//+------------------------------------------------------------------+
void FactoryClient::Run(void)
{
Print("factory client is running abstract product b");
apb.Interact(apa);
}
//+------------------------------------------------------------------+
//| participants > factory client |
//+------------------------------------------------------------------+
void FactoryClient::Delete(void)
{
delete apa;
delete apb;
delete factory;
}
//+------------------------------------------------------------------+
//| participants > factory client |
//+------------------------------------------------------------------+
void FactoryClient::Switch(AbstractFactory *af)
{
string sFactory;
StringConcatenate(sFactory,sFactory,factory);
int iFactory=(int)StringToInteger(sFactory);
if(iFactory>0)
{
Print("factory client is switching old factory ",factory," to new factory ",af);
}
else
{
Print("factory client is accepting new factory ",af);
}
Delete();
factory=af;
Print("factory client saved the new factory");
Print("factory client is requesting its new factory to create product a");
apa=factory.CreateProductA();
Print("factory client is requesting its new factory to create product b");
apb=factory.CreateProductB();
}
六、客户端调用工厂客户端
//+------------------------------------------------------------------+
//| interface for patterns |
//+------------------------------------------------------------------+
interface ClientExample //pattern client
{
string Output(void); //returns header
void Run(void); //execute the pattern client
};
//+------------------------------------------------------------------+
//| participants |
//+------------------------------------------------------------------+
class Client:public ClientExample
{
public:
string Output(void);
void Run(void);
};
string Client::Output(void) {return __FUNCTION__;}
//+------------------------------------------------------------------+
//| collaborations |
//+------------------------------------------------------------------+
void Client::Run(void)
// concrete factory
// a single instance > normally created at run-time
// creates products > with particular implementation
// client uses other factory > for different product objects
// abstract factory
// defers creation > product objects > concrete factory subclass
{
Print("client is requesting to create factory 1");
Print("client is requesting to create the factory client");
Print("client is requesting the factory client to manage factory 1");
FactoryClient client(new Factory1);
Print("client is requesting the factory client to operate");
client.Run();
Print("client is requesting to create new factory 2 and asking factory client to switch factories");
client.Switch(new Factory2);
Print("client is requesting the factory client to run again");
client.Run();
}
}
七、抽象工厂模式的结构
//+------------------------------------------------------------------+
//| structure |
//+------------------------------------------------------------------+
//
// | AbstractFactory|<-----------------------------------------|Client|
// |----------------| |
// |CreateProductA()| |
// |CreateProductA()| |AbstractProductA|<-------+
// ^ ^ |
// | | |
// +-----------+----------+ +-----+-----+ |
// | | | | |
//|ConcreteFactory1|- + |ConcreteFactory2|- + ->|ProductA2| |ProductA1|<- + |
//|----------------| | |----------------| | | |
//|CreateProductA()| |CreateProductA()| |
//|CreateProductB()| | |CreateProductB()| | | |
// |AbstractProductB|<----+--+
// | | ^ |
// |
// | | +-----+-----+ |
// | |
// | + ->|ProductB2| |ProductB1|<- +
// |
// + - - - - - - - - - - - - - - - - - - - - - - - - - +