2019-10-19 01:32:12 +00:00
|
|
|
#ifndef __ABSTRACT_FACTORY__
|
|
|
|
#define __ABSTRACT_FACTORY__
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
#include <string.h>
|
|
|
|
using namespace std;
|
|
|
|
|
2021-10-28 15:15:51 +00:00
|
|
|
// 抽象产品类AbstractBall
|
2019-10-19 01:32:12 +00:00
|
|
|
class AbstractBall
|
|
|
|
{
|
|
|
|
public:
|
2021-10-28 15:15:51 +00:00
|
|
|
AbstractBall()
|
|
|
|
{
|
2019-10-19 01:32:12 +00:00
|
|
|
}
|
2021-10-28 15:15:51 +00:00
|
|
|
virtual ~AbstractBall() {}
|
|
|
|
// 抽象方法:
|
2021-04-04 13:09:08 +00:00
|
|
|
virtual void play(){};
|
2019-10-19 01:32:12 +00:00
|
|
|
};
|
|
|
|
|
2021-10-28 15:15:51 +00:00
|
|
|
// 具体产品类Basketball
|
|
|
|
class Basketball : public AbstractBall
|
2019-10-19 01:32:12 +00:00
|
|
|
{
|
|
|
|
public:
|
2021-10-28 15:15:51 +00:00
|
|
|
Basketball()
|
|
|
|
{
|
2019-10-19 01:32:12 +00:00
|
|
|
play();
|
|
|
|
}
|
2021-10-28 15:15:51 +00:00
|
|
|
// 具体实现方法
|
|
|
|
void play()
|
|
|
|
{
|
2019-10-19 01:32:12 +00:00
|
|
|
printf("Jungle play Basketball\n\n");
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-10-28 15:15:51 +00:00
|
|
|
// 具体产品类Football
|
|
|
|
class Football : public AbstractBall
|
2019-10-19 01:32:12 +00:00
|
|
|
{
|
|
|
|
public:
|
2021-10-28 15:15:51 +00:00
|
|
|
Football()
|
|
|
|
{
|
2019-10-19 01:32:12 +00:00
|
|
|
play();
|
|
|
|
}
|
2021-10-28 15:15:51 +00:00
|
|
|
// 具体实现方法
|
|
|
|
void play()
|
|
|
|
{
|
2019-10-19 01:32:12 +00:00
|
|
|
printf("Jungle play Football\n\n");
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-10-28 15:15:51 +00:00
|
|
|
// 抽象产品类AbstractShirt
|
2019-10-19 01:32:12 +00:00
|
|
|
class AbstractShirt
|
|
|
|
{
|
|
|
|
public:
|
2021-10-28 15:15:51 +00:00
|
|
|
AbstractShirt() {}
|
|
|
|
virtual ~AbstractShirt() {}
|
|
|
|
// 抽象方法:
|
2021-04-04 13:09:08 +00:00
|
|
|
virtual void wearShirt(){};
|
2019-10-19 01:32:12 +00:00
|
|
|
};
|
|
|
|
|
2021-10-28 15:15:51 +00:00
|
|
|
// 具体产品类BasketballShirt
|
|
|
|
class BasketballShirt : public AbstractShirt
|
2019-10-19 01:32:12 +00:00
|
|
|
{
|
|
|
|
public:
|
2021-10-28 15:15:51 +00:00
|
|
|
BasketballShirt()
|
|
|
|
{
|
2019-10-19 01:32:12 +00:00
|
|
|
wearShirt();
|
|
|
|
}
|
2021-10-28 15:15:51 +00:00
|
|
|
// 具体实现方法
|
|
|
|
void wearShirt()
|
|
|
|
{
|
2019-10-19 01:32:12 +00:00
|
|
|
printf("Jungle wear Basketball Shirt\n\n");
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-10-28 15:15:51 +00:00
|
|
|
// 具体产品类FootballShirt
|
|
|
|
class FootballShirt : public AbstractShirt
|
2019-10-19 01:32:12 +00:00
|
|
|
{
|
|
|
|
public:
|
2021-10-28 15:15:51 +00:00
|
|
|
FootballShirt()
|
|
|
|
{
|
2019-10-19 01:32:12 +00:00
|
|
|
wearShirt();
|
|
|
|
}
|
2021-10-28 15:15:51 +00:00
|
|
|
// 具体实现方法
|
|
|
|
void wearShirt()
|
|
|
|
{
|
2019-10-19 01:32:12 +00:00
|
|
|
printf("Jungle wear Football Shirt\n\n");
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-10-28 15:15:51 +00:00
|
|
|
// 抽象工厂类
|
2019-10-19 01:32:12 +00:00
|
|
|
class AbstractFactory
|
|
|
|
{
|
|
|
|
public:
|
2021-04-04 13:09:08 +00:00
|
|
|
virtual ~AbstractFactory() {}
|
2019-10-19 01:32:12 +00:00
|
|
|
virtual AbstractBall *getBall() = 0;
|
|
|
|
virtual AbstractShirt *getShirt() = 0;
|
|
|
|
};
|
|
|
|
|
2021-10-28 15:15:51 +00:00
|
|
|
// 具体工厂类BasketballFactory
|
|
|
|
class BasketballFactory : public AbstractFactory
|
2019-10-19 01:32:12 +00:00
|
|
|
{
|
|
|
|
public:
|
2021-10-28 15:15:51 +00:00
|
|
|
BasketballFactory()
|
|
|
|
{
|
2019-10-19 01:32:12 +00:00
|
|
|
printf("BasketballFactory\n");
|
|
|
|
}
|
2021-10-28 15:15:51 +00:00
|
|
|
AbstractBall *getBall()
|
|
|
|
{
|
2019-10-19 01:32:12 +00:00
|
|
|
printf("Jungle get basketball\n");
|
|
|
|
return new Basketball();
|
|
|
|
}
|
2021-10-28 15:15:51 +00:00
|
|
|
AbstractShirt *getShirt()
|
|
|
|
{
|
2019-10-19 01:32:12 +00:00
|
|
|
printf("Jungle get basketball shirt\n");
|
|
|
|
return new BasketballShirt();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-10-28 15:15:51 +00:00
|
|
|
// 具体工厂类BasketballFactory
|
|
|
|
class FootballFactory : public AbstractFactory
|
2019-10-19 01:32:12 +00:00
|
|
|
{
|
|
|
|
public:
|
2021-10-28 15:15:51 +00:00
|
|
|
FootballFactory()
|
|
|
|
{
|
2019-10-19 01:32:12 +00:00
|
|
|
printf("FootballFactory\n");
|
|
|
|
}
|
2021-10-28 15:15:51 +00:00
|
|
|
AbstractBall *getBall()
|
|
|
|
{
|
2019-10-19 01:32:12 +00:00
|
|
|
printf("Jungle get football\n");
|
|
|
|
return new Football();
|
|
|
|
}
|
2021-10-28 15:15:51 +00:00
|
|
|
AbstractShirt *getShirt()
|
|
|
|
{
|
2019-10-19 01:32:12 +00:00
|
|
|
printf("Jungle get football shirt\n");
|
|
|
|
return new FootballShirt();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif //__ABSTRACT_FACTORY__
|