抽象工厂模式:
提供了一个创建一系列相关的或相互依赖的对象的接口而不需要具体指定它们的类型。
Provide an interface for creating families of related or dependent objects without specifying their concrete classes.
UML类图:
主要包括:
基本的抽象工厂模式C++代码如下:
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
class AbstractProductA
{
public:
virtual void show()=0;
};
class ProductA1:public AbstractProductA
{
public:
void show()
{
std::cout<<"ProductA1::show()"<<std::endl;
}
};
class ProductA2:public AbstractProductA
{
public:
void show()
{
std::cout<<"ProductA2::show()"<<std::endl;
}
};
class AbstractProductB
{
public :
virtual void display()=0;
};
class ProductB1:public AbstractProductB
{
public:
void display()
{
std::cout<<"ProductB1::display()"<<std::endl;
}
};
class ProductB2:public AbstractProductB
{
public:
void display()
{
std::cout<<"ProductB2::display()"<<std::endl;
}
};
class AbstractFactory
{
public:
virtual AbstractProductA* createProductA()=0;
virtual AbstractProductB* createProductB()=0;
};
class ConcreteFactory1:public AbstractFactory
{
public:
AbstractProductA *createProductA()
{
return new ProductA1();
}
AbstractProductB *createProductB()
{
return new ProductB1();
}
};
class ConcreteFactory2:public AbstractFactory
{
public:
AbstractProductA *createProductA()
{
return new ProductA2();
}
AbstractProductB *createProductB()
{
return new ProductB2();
}
};
class Client
{
public:
Client()
{
}
Client(AbstractFactory *af)
{
abstractFactory=af;
}
AbstractProductA * getProductA()
{
return abstractProductA;
}
AbstractProductB* getProductB()
{
return abstractProductB;
}
void run()
{
abstractProductA=abstractFactory->createProductA();
abstractProductB=abstractFactory->createProductB();
}
~Client()
{
if(!abstractProductA)
delete abstractProductA;
if(!abstractProductB)
delete abstractProductB;
}
private:
AbstractFactory *abstractFactory;
AbstractProductA * abstractProductA;
AbstractProductB * abstractProductB;
};
int main()
{
std::cout<<"抽象工厂模式测试"<<std::endl;
AbstractFactory *af=new ConcreteFactory1();
Client* client=new Client(af);
client->run();
client->getProductA()->show();
client->getProductB()->display();
delete af;
delete client;
return 0;
}
执行结果:
再看一个具体的例子:
大陆上有食肉动物和食草动物,但是不同的大陆比如说非洲大陆的食肉动物和食草动物和美洲大陆的食肉动物和食草动物是不同的。抽象工厂工厂模式和工厂方法模式的不同点就在于抽象工厂模式有两处变化的地方,除了工厂类可以变化外,抽象的产品类有多个而不是一个。
UML图如下:
C++代码实现如下:
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
//食草动物
class Herbivore
{
public:
virtual void drink()=0;
};
//羚羊的一种,产于非洲
class Wildebeest:public Herbivore
{
public:
void drink()
{
std::cout<<"wildbeest drink"<<std::endl;
}
};
//美洲野牛
class Bison:public Herbivore
{
public:
void drink()
{
std::cout<<"bison drink"<<std::endl;
}
};
//食肉动物
class Carnivore
{
public:
virtual void eat()=0;
};
//非洲狮子
class Lion:public Carnivore
{
public:
void eat()
{
std::cout<<"lion eat"<<std::endl;
}
};
//狼
class Wolf:public Carnivore
{
public:
void eat()
{
std::cout<<"wolf eat"<<std::endl;
}
};
//大陆工厂
class ContinentFactory
{
public:
virtual Herbivore * createHerbivore()=0;
virtual Carnivore * createCarnivore()=0;
};
class AfricaFactory :public ContinentFactory
{
public:
virtual Herbivore * createHerbivore()
{
return new Wildebeest();
}
virtual Carnivore * createCarnivore()
{
return new Lion();
}
};
class AmericaFactory :public ContinentFactory
{
public:
virtual Herbivore * createHerbivore()
{
return new Bison();
}
virtual Carnivore * createCarnivore()
{
return new Wolf();
}
};
class AnimalWorld
{
public:
AnimalWorld()
{
}
AnimalWorld(ContinentFactory *cf)
{
herbivore=cf->createHerbivore();
carnivore=cf->createCarnivore();
}
void action()
{
herbivore->drink();
carnivore->eat();
}
~AnimalWorld()
{
if(!herbivore)
delete herbivore;
if(!carnivore)
delete carnivore;
}
private:
Herbivore * herbivore;
Carnivore * carnivore;
};
int main()
{
std::cout<<"抽象工厂模式具体的例子"<<std::endl;
ContinentFactory * cf=new AmericaFactory;
AnimalWorld *an=new AnimalWorld(cf);
an->action();
delete cf;
delete an;
return 0;
}
执行结果:
设计模式之十一:抽象工厂模式(Abstract Factory)
原文地址:http://blog.csdn.net/u012501459/article/details/46392975