Add AbstractFactory code and picture
parent
b1f071112f
commit
493c432949
Binary file not shown.
Binary file not shown.
After Width: | Height: | Size: 31 KiB |
Binary file not shown.
After Width: | Height: | Size: 34 KiB |
Binary file not shown.
After Width: | Height: | Size: 276 KiB |
|
@ -0,0 +1,122 @@
|
||||||
|
#ifndef __ABSTRACT_FACTORY__
|
||||||
|
#define __ABSTRACT_FACTORY__
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
#include <string.h>
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
//抽象产品类AbstractBall
|
||||||
|
class AbstractBall
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
AbstractBall(){
|
||||||
|
|
||||||
|
}
|
||||||
|
//抽象方法:
|
||||||
|
void play(){};
|
||||||
|
};
|
||||||
|
|
||||||
|
//具体产品类Basketball
|
||||||
|
class Basketball :public AbstractBall
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
Basketball(){
|
||||||
|
play();
|
||||||
|
}
|
||||||
|
//具体实现方法
|
||||||
|
void play(){
|
||||||
|
printf("Jungle play Basketball\n\n");
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
//具体产品类Football
|
||||||
|
class Football :public AbstractBall
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
Football(){
|
||||||
|
play();
|
||||||
|
}
|
||||||
|
//具体实现方法
|
||||||
|
void play(){
|
||||||
|
printf("Jungle play Football\n\n");
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
//抽象产品类AbstractShirt
|
||||||
|
class AbstractShirt
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
AbstractShirt(){}
|
||||||
|
//抽象方法:
|
||||||
|
void wearShirt(){};
|
||||||
|
};
|
||||||
|
|
||||||
|
//具体产品类BasketballShirt
|
||||||
|
class BasketballShirt :public AbstractShirt
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
BasketballShirt(){
|
||||||
|
wearShirt();
|
||||||
|
}
|
||||||
|
//具体实现方法
|
||||||
|
void wearShirt(){
|
||||||
|
printf("Jungle wear Basketball Shirt\n\n");
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
//具体产品类FootballShirt
|
||||||
|
class FootballShirt :public AbstractShirt
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
FootballShirt(){
|
||||||
|
wearShirt();
|
||||||
|
}
|
||||||
|
//具体实现方法
|
||||||
|
void wearShirt(){
|
||||||
|
printf("Jungle wear Football Shirt\n\n");
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
//抽象工厂类
|
||||||
|
class AbstractFactory
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
virtual AbstractBall *getBall() = 0;
|
||||||
|
virtual AbstractShirt *getShirt() = 0;
|
||||||
|
};
|
||||||
|
|
||||||
|
//具体工厂类BasketballFactory
|
||||||
|
class BasketballFactory :public AbstractFactory
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
BasketballFactory(){
|
||||||
|
printf("BasketballFactory\n");
|
||||||
|
}
|
||||||
|
AbstractBall *getBall(){
|
||||||
|
printf("Jungle get basketball\n");
|
||||||
|
return new Basketball();
|
||||||
|
}
|
||||||
|
AbstractShirt *getShirt(){
|
||||||
|
printf("Jungle get basketball shirt\n");
|
||||||
|
return new BasketballShirt();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
//具体工厂类BasketballFactory
|
||||||
|
class FootballFactory :public AbstractFactory
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
FootballFactory(){
|
||||||
|
printf("FootballFactory\n");
|
||||||
|
}
|
||||||
|
AbstractBall *getBall(){
|
||||||
|
printf("Jungle get football\n");
|
||||||
|
return new Football();
|
||||||
|
}
|
||||||
|
AbstractShirt *getShirt(){
|
||||||
|
printf("Jungle get football shirt\n");
|
||||||
|
return new FootballShirt();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif //__ABSTRACT_FACTORY__
|
|
@ -0,0 +1,23 @@
|
||||||
|
#include <iostream>
|
||||||
|
#include "AbstractFactory.h"
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
printf("工厂方法模式\n");
|
||||||
|
|
||||||
|
//定义工厂类对象和产品类对象
|
||||||
|
AbstractFactory *fac = NULL;
|
||||||
|
AbstractBall *ball = NULL;
|
||||||
|
AbstractShirt *shirt = NULL;
|
||||||
|
|
||||||
|
fac = new BasketballFactory();
|
||||||
|
ball = fac->getBall();
|
||||||
|
shirt = fac->getShirt();
|
||||||
|
|
||||||
|
fac = new FootballFactory();
|
||||||
|
ball = fac->getBall();
|
||||||
|
shirt = fac->getShirt();
|
||||||
|
|
||||||
|
system("pause");
|
||||||
|
return 0;
|
||||||
|
}
|
Loading…
Reference in New Issue