DesignPattern/02.FactoryMethod/2.Code/FactoryMethod.h

116 lines
1.8 KiB
C
Raw Normal View History

2019-10-17 23:32:47 +00:00
#ifndef __FACTORY_METHOD__
#define __FACTORY_METHOD__
#include <iostream>
#include <string.h>
using namespace std;
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ʒ<EFBFBD><C6B7>AbstractProduct
class AbstractSportProduct
{
public:
AbstractSportProduct(){
}
//<2F><><EFBFBD>󷽷<EFBFBD><F3B7BDB7><EFBFBD>
void printName(){};
void play(){};
};
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ʒ<EFBFBD><C6B7>Basketball
class Basketball :public AbstractSportProduct
{
public:
Basketball(){
printName();
play();
}
//<2F><><EFBFBD><EFBFBD>ʵ<EFBFBD>ַ<EFBFBD><D6B7><EFBFBD>
void printName(){
printf("Jungle get Basketball\n");
}
void play(){
printf("Jungle play Basketball\n\n");
}
};
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ʒ<EFBFBD><C6B7>Football
class Football :public AbstractSportProduct
{
public:
Football(){
printName();
play();
}
//<2F><><EFBFBD><EFBFBD>ʵ<EFBFBD>ַ<EFBFBD><D6B7><EFBFBD>
void printName(){
printf("Jungle get Football\n");
}
void play(){
printf("Jungle play Football\n\n");
}
};
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ʒ<EFBFBD><C6B7>Volleyball
class Volleyball :public AbstractSportProduct
{
public:
Volleyball(){
printName();
play();
}
//<2F><><EFBFBD><EFBFBD>ʵ<EFBFBD>ַ<EFBFBD><D6B7><EFBFBD>
void printName(){
printf("Jungle get Volleyball\n");
}
void play(){
printf("Jungle play Volleyball\n\n");
}
};
//<2F><><EFBFBD>󹤳<EFBFBD><F3B9A4B3><EFBFBD>
class AbstractFactory
{
public:
virtual AbstractSportProduct *getSportProduct() = 0;
};
//<2F><><EFBFBD><EFBFBD><E5B9A4><EFBFBD><EFBFBD>BasketballFactory
class BasketballFactory :public AbstractFactory
{
public:
BasketballFactory(){
printf("BasketballFactory\n");
}
AbstractSportProduct *getSportProduct(){
printf("basketball");
return new Basketball();
}
};
//<2F><><EFBFBD><EFBFBD><E5B9A4><EFBFBD><EFBFBD>FootballFactory
class FootballFactory :public AbstractFactory
{
public:
FootballFactory(){
printf("FootballFactory\n");
}
AbstractSportProduct *getSportProduct(){
return new Football();
}
};
//<2F><><EFBFBD><EFBFBD><E5B9A4><EFBFBD><EFBFBD>VolleyballFactory
class VolleyballFactory :public AbstractFactory
{
public:
VolleyballFactory(){
printf("VolleyballFactory\n");
}
AbstractSportProduct *getSportProduct(){
return new Volleyball();
}
};
#endif //__FACTORY_METHOD__