bugfix: change character code : utf-8
parent
4d8d1ca606
commit
6ca04a5438
|
@ -0,0 +1,5 @@
|
||||||
|
{
|
||||||
|
"files.associations": {
|
||||||
|
"list": "cpp"
|
||||||
|
}
|
||||||
|
}
|
|
@ -5,7 +5,7 @@
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
//抽象产品类AbstractProduct
|
// 抽象产品类AbstractProduct
|
||||||
class AbstractSportProduct
|
class AbstractSportProduct
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
@ -13,12 +13,12 @@ public:
|
||||||
|
|
||||||
}
|
}
|
||||||
virtual ~AbstractSportProduct(){}
|
virtual ~AbstractSportProduct(){}
|
||||||
//抽象方法:
|
// 抽象方法:
|
||||||
virtual void printName(){};
|
virtual void printName(){};
|
||||||
virtual void play(){};
|
virtual void play(){};
|
||||||
};
|
};
|
||||||
|
|
||||||
//具体产品类Basketball
|
// 具体产品类Basketball
|
||||||
class Basketball :public AbstractSportProduct
|
class Basketball :public AbstractSportProduct
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
@ -26,7 +26,7 @@ public:
|
||||||
printName();
|
printName();
|
||||||
play();
|
play();
|
||||||
}
|
}
|
||||||
//具体实现方法
|
// 具体实现方法
|
||||||
void printName(){
|
void printName(){
|
||||||
printf("Jungle get Basketball\n");
|
printf("Jungle get Basketball\n");
|
||||||
}
|
}
|
||||||
|
@ -35,7 +35,7 @@ public:
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
//具体产品类Football
|
// 具体产品类Football
|
||||||
class Football :public AbstractSportProduct
|
class Football :public AbstractSportProduct
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
@ -43,7 +43,7 @@ public:
|
||||||
printName();
|
printName();
|
||||||
play();
|
play();
|
||||||
}
|
}
|
||||||
//具体实现方法
|
// 具体实现方法
|
||||||
void printName(){
|
void printName(){
|
||||||
printf("Jungle get Football\n");
|
printf("Jungle get Football\n");
|
||||||
}
|
}
|
||||||
|
@ -52,7 +52,7 @@ public:
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
//具体产品类Volleyball
|
// 具体产品类Volleyball
|
||||||
class Volleyball :public AbstractSportProduct
|
class Volleyball :public AbstractSportProduct
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
@ -60,7 +60,7 @@ public:
|
||||||
printName();
|
printName();
|
||||||
play();
|
play();
|
||||||
}
|
}
|
||||||
//具体实现方法
|
// 具体实现方法
|
||||||
void printName(){
|
void printName(){
|
||||||
printf("Jungle get Volleyball\n");
|
printf("Jungle get Volleyball\n");
|
||||||
}
|
}
|
||||||
|
@ -69,7 +69,7 @@ public:
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
//抽象工厂类
|
// 抽象工厂类
|
||||||
class AbstractFactory
|
class AbstractFactory
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
@ -77,7 +77,7 @@ public:
|
||||||
virtual AbstractSportProduct *getSportProduct() = 0;
|
virtual AbstractSportProduct *getSportProduct() = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
//具体工厂类BasketballFactory
|
// 具体工厂类BasketballFactory
|
||||||
class BasketballFactory :public AbstractFactory
|
class BasketballFactory :public AbstractFactory
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
@ -90,7 +90,7 @@ public:
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
//具体工厂类FootballFactory
|
// 具体工厂类FootballFactory
|
||||||
class FootballFactory :public AbstractFactory
|
class FootballFactory :public AbstractFactory
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
@ -102,7 +102,7 @@ public:
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
//具体工厂类VolleyballFactory
|
// 具体工厂类VolleyballFactory
|
||||||
class VolleyballFactory :public AbstractFactory
|
class VolleyballFactory :public AbstractFactory
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
|
|
@ -4,10 +4,10 @@
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
printf("工厂方法模式\n");
|
printf("工厂方法模式\n");
|
||||||
|
|
||||||
//定义工厂类对象和产品类对象
|
|
||||||
|
|
||||||
|
// 定义工厂类对象和产品类对象
|
||||||
|
|
||||||
std::shared_ptr<AbstractFactory> fac = make_shared<BasketballFactory>();
|
std::shared_ptr<AbstractFactory> fac = make_shared<BasketballFactory>();
|
||||||
std::shared_ptr<AbstractSportProduct> product = std::shared_ptr<AbstractSportProduct>(fac->getSportProduct());
|
std::shared_ptr<AbstractSportProduct> product = std::shared_ptr<AbstractSportProduct>(fac->getSportProduct());
|
||||||
|
|
||||||
|
|
|
@ -5,81 +5,89 @@
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
//抽象产品类AbstractBall
|
// 抽象产品类AbstractBall
|
||||||
class AbstractBall
|
class AbstractBall
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
AbstractBall(){
|
AbstractBall()
|
||||||
|
{
|
||||||
}
|
}
|
||||||
virtual ~AbstractBall(){}
|
virtual ~AbstractBall() {}
|
||||||
//抽象方法:
|
// 抽象方法:
|
||||||
virtual void play(){};
|
virtual void play(){};
|
||||||
};
|
};
|
||||||
|
|
||||||
//具体产品类Basketball
|
// 具体产品类Basketball
|
||||||
class Basketball :public AbstractBall
|
class Basketball : public AbstractBall
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
Basketball(){
|
Basketball()
|
||||||
|
{
|
||||||
play();
|
play();
|
||||||
}
|
}
|
||||||
//具体实现方法
|
// 具体实现方法
|
||||||
void play(){
|
void play()
|
||||||
|
{
|
||||||
printf("Jungle play Basketball\n\n");
|
printf("Jungle play Basketball\n\n");
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
//具体产品类Football
|
// 具体产品类Football
|
||||||
class Football :public AbstractBall
|
class Football : public AbstractBall
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
Football(){
|
Football()
|
||||||
|
{
|
||||||
play();
|
play();
|
||||||
}
|
}
|
||||||
//具体实现方法
|
// 具体实现方法
|
||||||
void play(){
|
void play()
|
||||||
|
{
|
||||||
printf("Jungle play Football\n\n");
|
printf("Jungle play Football\n\n");
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
//抽象产品类AbstractShirt
|
// 抽象产品类AbstractShirt
|
||||||
class AbstractShirt
|
class AbstractShirt
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
AbstractShirt(){}
|
AbstractShirt() {}
|
||||||
virtual ~AbstractShirt(){}
|
virtual ~AbstractShirt() {}
|
||||||
//抽象方法:
|
// 抽象方法:
|
||||||
virtual void wearShirt(){};
|
virtual void wearShirt(){};
|
||||||
};
|
};
|
||||||
|
|
||||||
//具体产品类BasketballShirt
|
// 具体产品类BasketballShirt
|
||||||
class BasketballShirt :public AbstractShirt
|
class BasketballShirt : public AbstractShirt
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
BasketballShirt(){
|
BasketballShirt()
|
||||||
|
{
|
||||||
wearShirt();
|
wearShirt();
|
||||||
}
|
}
|
||||||
//具体实现方法
|
// 具体实现方法
|
||||||
void wearShirt(){
|
void wearShirt()
|
||||||
|
{
|
||||||
printf("Jungle wear Basketball Shirt\n\n");
|
printf("Jungle wear Basketball Shirt\n\n");
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
//具体产品类FootballShirt
|
// 具体产品类FootballShirt
|
||||||
class FootballShirt :public AbstractShirt
|
class FootballShirt : public AbstractShirt
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
FootballShirt(){
|
FootballShirt()
|
||||||
|
{
|
||||||
wearShirt();
|
wearShirt();
|
||||||
}
|
}
|
||||||
//具体实现方法
|
// 具体实现方法
|
||||||
void wearShirt(){
|
void wearShirt()
|
||||||
|
{
|
||||||
printf("Jungle wear Football Shirt\n\n");
|
printf("Jungle wear Football Shirt\n\n");
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
//抽象工厂类
|
// 抽象工厂类
|
||||||
class AbstractFactory
|
class AbstractFactory
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
@ -88,35 +96,41 @@ public:
|
||||||
virtual AbstractShirt *getShirt() = 0;
|
virtual AbstractShirt *getShirt() = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
//具体工厂类BasketballFactory
|
// 具体工厂类BasketballFactory
|
||||||
class BasketballFactory :public AbstractFactory
|
class BasketballFactory : public AbstractFactory
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
BasketballFactory(){
|
BasketballFactory()
|
||||||
|
{
|
||||||
printf("BasketballFactory\n");
|
printf("BasketballFactory\n");
|
||||||
}
|
}
|
||||||
AbstractBall *getBall(){
|
AbstractBall *getBall()
|
||||||
|
{
|
||||||
printf("Jungle get basketball\n");
|
printf("Jungle get basketball\n");
|
||||||
return new Basketball();
|
return new Basketball();
|
||||||
}
|
}
|
||||||
AbstractShirt *getShirt(){
|
AbstractShirt *getShirt()
|
||||||
|
{
|
||||||
printf("Jungle get basketball shirt\n");
|
printf("Jungle get basketball shirt\n");
|
||||||
return new BasketballShirt();
|
return new BasketballShirt();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
//具体工厂类BasketballFactory
|
// 具体工厂类BasketballFactory
|
||||||
class FootballFactory :public AbstractFactory
|
class FootballFactory : public AbstractFactory
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
FootballFactory(){
|
FootballFactory()
|
||||||
|
{
|
||||||
printf("FootballFactory\n");
|
printf("FootballFactory\n");
|
||||||
}
|
}
|
||||||
AbstractBall *getBall(){
|
AbstractBall *getBall()
|
||||||
|
{
|
||||||
printf("Jungle get football\n");
|
printf("Jungle get football\n");
|
||||||
return new Football();
|
return new Football();
|
||||||
}
|
}
|
||||||
AbstractShirt *getShirt(){
|
AbstractShirt *getShirt()
|
||||||
|
{
|
||||||
printf("Jungle get football shirt\n");
|
printf("Jungle get football shirt\n");
|
||||||
return new FootballShirt();
|
return new FootballShirt();
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,7 +5,7 @@
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
//<EFBFBD><EFBFBD>Ʒ<EFBFBD><EFBFBD>House
|
// 产品类House
|
||||||
class House
|
class House
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
@ -19,7 +19,7 @@ public:
|
||||||
void setRoof(string iRoof) {
|
void setRoof(string iRoof) {
|
||||||
this->roof = iRoof;
|
this->roof = iRoof;
|
||||||
}
|
}
|
||||||
//<EFBFBD><EFBFBD>ӡHouse<EFBFBD><EFBFBD>Ϣ
|
// 打印House信息
|
||||||
void printfHouseInfo() {
|
void printfHouseInfo() {
|
||||||
printf("Floor:%s\t\n", this->floor.c_str());
|
printf("Floor:%s\t\n", this->floor.c_str());
|
||||||
printf("Wall:%s\t\n", this->wall.c_str());
|
printf("Wall:%s\t\n", this->wall.c_str());
|
||||||
|
@ -31,7 +31,7 @@ private:
|
||||||
string roof;
|
string roof;
|
||||||
};
|
};
|
||||||
|
|
||||||
//<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>AbstractBall
|
// 抽象建造者AbstractBall
|
||||||
class AbstractBuilder
|
class AbstractBuilder
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
@ -46,7 +46,7 @@ public:
|
||||||
house = nullptr;
|
house = nullptr;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
//<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
// 抽象方法:
|
||||||
virtual void buildFloor() = 0;
|
virtual void buildFloor() = 0;
|
||||||
virtual void buildWall() = 0;
|
virtual void buildWall() = 0;
|
||||||
virtual void buildRoof() = 0;
|
virtual void buildRoof() = 0;
|
||||||
|
@ -55,7 +55,7 @@ public:
|
||||||
House *house;
|
House *house;
|
||||||
};
|
};
|
||||||
|
|
||||||
//<EFBFBD><EFBFBD><EFBFBD>彨<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ConcreteBuilderA
|
// 具体建造者ConcreteBuilderA
|
||||||
class ConcreteBuilderA :public AbstractBuilder
|
class ConcreteBuilderA :public AbstractBuilder
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
@ -70,7 +70,7 @@ public:
|
||||||
house = nullptr;
|
house = nullptr;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
//<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʵ<EFBFBD>ַ<EFBFBD><EFBFBD><EFBFBD>
|
// 具体实现方法
|
||||||
void buildFloor() {
|
void buildFloor() {
|
||||||
this->house->setFloor("Floor_A");
|
this->house->setFloor("Floor_A");
|
||||||
}
|
}
|
||||||
|
@ -85,7 +85,7 @@ public:
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
//<EFBFBD><EFBFBD><EFBFBD>彨<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ConcreteBuilderB
|
// 具体建造者ConcreteBuilderB
|
||||||
class ConcreteBuilderB :public AbstractBuilder
|
class ConcreteBuilderB :public AbstractBuilder
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
@ -100,7 +100,7 @@ public:
|
||||||
house = nullptr;
|
house = nullptr;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
//<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʵ<EFBFBD>ַ<EFBFBD><EFBFBD><EFBFBD>
|
// 具体实现方法
|
||||||
void buildFloor() {
|
void buildFloor() {
|
||||||
this->house->setFloor("Floor_B");
|
this->house->setFloor("Floor_B");
|
||||||
}
|
}
|
||||||
|
@ -115,7 +115,7 @@ public:
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
//ָ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Director
|
// 指挥者Director
|
||||||
class Director
|
class Director
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
@ -128,10 +128,11 @@ public:
|
||||||
builder = nullptr;
|
builder = nullptr;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
//<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʵ<EFBFBD>ַ<EFBFBD><EFBFBD><EFBFBD>
|
// 具体实现方法
|
||||||
void setBuilder(AbstractBuilder *iBuilder) {
|
void setBuilder(AbstractBuilder *iBuilder) {
|
||||||
this->builder = iBuilder;
|
this->builder = iBuilder;
|
||||||
}
|
}
|
||||||
|
// 封装组装流程,返回建造结果
|
||||||
House *construct() {
|
House *construct() {
|
||||||
builder->buildFloor();
|
builder->buildFloor();
|
||||||
builder->buildWall();
|
builder->buildWall();
|
||||||
|
|
|
@ -2,21 +2,21 @@
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
//녜蹶쉔芚諒
|
// 抽象建造者
|
||||||
AbstractBuilder *builder;
|
AbstractBuilder *builder;
|
||||||
//寧뿐諒
|
// 指挥者
|
||||||
Director *director = new Director();
|
Director *director = new Director();
|
||||||
//끓틔:House
|
// 产品:House
|
||||||
House *house;
|
House *house;
|
||||||
|
|
||||||
//寧땍야竟쉔芚諒A
|
// 指定具体建造者A
|
||||||
builder = new ConcreteBuilderA();
|
builder = new ConcreteBuilderA();
|
||||||
director->setBuilder(builder);
|
director->setBuilder(builder);
|
||||||
house = director->construct();
|
house = director->construct();
|
||||||
house->printfHouseInfo();
|
house->printfHouseInfo();
|
||||||
delete builder;
|
delete builder;
|
||||||
|
|
||||||
//寧땍야竟쉔芚諒B
|
// 指定具体建造者A
|
||||||
builder = new ConcreteBuilderB();
|
builder = new ConcreteBuilderB();
|
||||||
director->setBuilder(builder);
|
director->setBuilder(builder);
|
||||||
house = director->construct();
|
house = director->construct();
|
||||||
|
|
|
@ -5,7 +5,7 @@
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
//work model类
|
// work model类
|
||||||
class WorkModel
|
class WorkModel
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
@ -15,7 +15,7 @@ public:
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
//抽象原型类PrototypeWork
|
// 抽象原型类PrototypeWork
|
||||||
class PrototypeWork
|
class PrototypeWork
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
@ -27,7 +27,7 @@ private:
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
//具体原型类ConcreteWork
|
// 抽象原型类PrototypeWork
|
||||||
class ConcreteWork :public PrototypeWork
|
class ConcreteWork :public PrototypeWork
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
@ -56,7 +56,7 @@ public:
|
||||||
void setModel(WorkModel *iWorkModel){
|
void setModel(WorkModel *iWorkModel){
|
||||||
this->workModel = iWorkModel;
|
this->workModel = iWorkModel;
|
||||||
}
|
}
|
||||||
//打印work信息
|
// 打印work信息
|
||||||
void printWorkInfo(){
|
void printWorkInfo(){
|
||||||
printf("name:%s\t\n", this->name);
|
printf("name:%s\t\n", this->name);
|
||||||
printf("idNum:%d\t\n", this->idNum);
|
printf("idNum:%d\t\n", this->idNum);
|
||||||
|
|
|
@ -4,47 +4,47 @@ int main()
|
||||||
{
|
{
|
||||||
#if 0
|
#if 0
|
||||||
ConcreteWork *singleWork = new ConcreteWork("Single",1001,"Single_Model");
|
ConcreteWork *singleWork = new ConcreteWork("Single",1001,"Single_Model");
|
||||||
printf("\nSingle的作业:\n");
|
printf("\nSingle的作业:\n");
|
||||||
singleWork->printWorkInfo();
|
singleWork->printWorkInfo();
|
||||||
|
|
||||||
printf("\njungle直接抄作业……\n");
|
printf("\njungle直接抄作业……\n");
|
||||||
ConcreteWork *jungleWork = singleWork;
|
ConcreteWork *jungleWork = singleWork;
|
||||||
printf("\nJungle的作业:\n");
|
printf("\nJungle的作业:\n");
|
||||||
jungleWork->printWorkInfo();
|
jungleWork->printWorkInfo();
|
||||||
|
|
||||||
//抄完改名字和学号,否则会被老师查出来
|
// 抄完改名字和学号,否则会被老师查出来
|
||||||
printf("\njungle抄完改名字和学号,否则会被老师查出来……\n");
|
printf("\njungle抄完改名字和学号,否则会被老师查出来……\n");
|
||||||
jungleWork->setName("jungle");
|
jungleWork->setName("jungle");
|
||||||
jungleWork->setIdNum(1002);
|
jungleWork->setIdNum(1002);
|
||||||
WorkModel *jungleModel = new WorkModel();
|
WorkModel *jungleModel = new WorkModel();
|
||||||
jungleModel->setWorkModelName("Jungle_Model");
|
jungleModel->setWorkModelName("Jungle_Model");
|
||||||
jungleWork->setModel(jungleModel);
|
jungleWork->setModel(jungleModel);
|
||||||
|
|
||||||
//检查下是否改对了
|
// 检查下是否改对了
|
||||||
printf("\nSingle的作业:\n");
|
printf("\nSingle的作业:\n");
|
||||||
singleWork->printWorkInfo();
|
singleWork->printWorkInfo();
|
||||||
printf("\nJungle的作业:\n");
|
printf("\nJungle的作业:\n");
|
||||||
jungleWork->printWorkInfo();
|
jungleWork->printWorkInfo();
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
ConcreteWork *singleWork = new ConcreteWork("Single", 1001, "Single_Model");
|
ConcreteWork *singleWork = new ConcreteWork("Single", 1001, "Single_Model");
|
||||||
printf("\nSingle的作业:\n");
|
printf("\nSingle的作业:\n");
|
||||||
|
|
||||||
ConcreteWork *jungleWork = singleWork->clone();
|
ConcreteWork *jungleWork = singleWork->clone();
|
||||||
printf("\nJungle的作业:\n");
|
printf("\njungle直接抄作业……\n");
|
||||||
|
|
||||||
//抄完改名字和学号,否则会被老师查出来
|
// 抄完改名字和学号,否则会被老师查出来
|
||||||
printf("\njungle抄完改名字和学号,否则会被老师查出来……\n");
|
printf("\njungle抄完改名字和学号,否则会被老师查出来……\n");
|
||||||
jungleWork->setName("jungle");
|
jungleWork->setName("jungle");
|
||||||
jungleWork->setIdNum(1002);
|
jungleWork->setIdNum(1002);
|
||||||
WorkModel *jungleModel = new WorkModel();
|
WorkModel *jungleModel = new WorkModel();
|
||||||
jungleModel->setWorkModelName("Jungle_Model");
|
jungleModel->setWorkModelName("Jungle_Model");
|
||||||
jungleWork->setModel(jungleModel);
|
jungleWork->setModel(jungleModel);
|
||||||
|
|
||||||
//检查下是否改对了
|
// 检查下是否改对了
|
||||||
printf("\nSingle的作业:\n");
|
printf("\nSingle的作业:\n");
|
||||||
singleWork->printWorkInfo();
|
singleWork->printWorkInfo();
|
||||||
printf("\nJungle的作业:\n");
|
printf("\nJungle的作业:\n");
|
||||||
jungleWork->printWorkInfo();
|
jungleWork->printWorkInfo();
|
||||||
|
|
||||||
system("pause");
|
system("pause");
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
#ifndef __DECORATOR_PATTERN_H__
|
#ifndef __DECORATOR_PATTERN_H__
|
||||||
#define __DECORATOR_PATTERN_H__
|
#define __DECORATOR_PATTERN_H__
|
||||||
|
|
||||||
//抽象构件
|
// 抽象构件
|
||||||
class Component
|
class Component
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
@ -10,17 +10,17 @@ public:
|
||||||
virtual void operation() = 0;
|
virtual void operation() = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
//具体构件类
|
// 具体构件
|
||||||
class Phone :public Component
|
class Phone :public Component
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
Phone(){}
|
Phone(){}
|
||||||
void operation(){
|
void operation(){
|
||||||
printf("手机\n");
|
printf("<EFBFBD>ֻ<EFBFBD>\n");
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
//抽象装饰类
|
// 抽象装饰类
|
||||||
class Decorator :public Component
|
class Decorator :public Component
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
@ -41,7 +41,7 @@ private:
|
||||||
Component *component;
|
Component *component;
|
||||||
};
|
};
|
||||||
|
|
||||||
//具体装饰类:手机壳
|
// 具体装饰类:手机壳
|
||||||
class DecoratorShell:public Decorator
|
class DecoratorShell:public Decorator
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
@ -54,11 +54,12 @@ public:
|
||||||
this->newBehavior();
|
this->newBehavior();
|
||||||
}
|
}
|
||||||
void newBehavior(){
|
void newBehavior(){
|
||||||
printf("装手机壳\n");
|
printf("安装手机壳\n");
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
//具体装饰类:手机贴纸
|
|
||||||
|
// 具体装饰类:手机贴纸
|
||||||
class DecoratorSticker :public Decorator
|
class DecoratorSticker :public Decorator
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
@ -71,11 +72,11 @@ public:
|
||||||
this->newBehavior();
|
this->newBehavior();
|
||||||
}
|
}
|
||||||
void newBehavior(){
|
void newBehavior(){
|
||||||
printf("贴卡通贴纸\n");
|
printf("贴卡通贴纸ֽ\n");
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
//具体装饰类:手机挂绳
|
// 具体装饰类:挂绳
|
||||||
class DecoratorRope :public Decorator
|
class DecoratorRope :public Decorator
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
@ -88,7 +89,7 @@ public:
|
||||||
this->newBehavior();
|
this->newBehavior();
|
||||||
}
|
}
|
||||||
void newBehavior(){
|
void newBehavior(){
|
||||||
printf("系手机挂绳\n");
|
printf("系手机挂绳\n");
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
#endif //__DECORATOR_PATTERN_H__
|
#endif //__DECORATOR_PATTERN_H__
|
|
@ -5,7 +5,7 @@
|
||||||
#include <vector>
|
#include <vector>
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
// 抽象享元类
|
// 抽象享元类
|
||||||
class NetDevice
|
class NetDevice
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
@ -21,27 +21,27 @@ public:
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// 具体享元类:集线器
|
// 具体享元类:集线器
|
||||||
class Hub :public NetDevice
|
class Hub :public NetDevice
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
Hub(){}
|
Hub(){}
|
||||||
string getName(){
|
string getName(){
|
||||||
return "集线器";
|
return "集线器";
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// 具体享元类:交换机
|
// 具体享元类:交换机
|
||||||
class Switch :public NetDevice
|
class Switch :public NetDevice
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
Switch(){}
|
Switch(){}
|
||||||
string getName(){
|
string getName(){
|
||||||
return "交换机";
|
return "交换机";
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// 享元工厂类
|
// 享元工厂类
|
||||||
class NetDeviceFactory
|
class NetDeviceFactory
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
@ -58,7 +58,7 @@ public:
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 单例模式:返回享元工厂类的唯一实例
|
// 单例模式:返回享元工厂类的唯一实例
|
||||||
static NetDeviceFactory* getFactory(){
|
static NetDeviceFactory* getFactory(){
|
||||||
if (instance == NULL){
|
if (instance == NULL){
|
||||||
m_mutex.lock();
|
m_mutex.lock();
|
||||||
|
@ -80,7 +80,7 @@ private:
|
||||||
static NetDeviceFactory* instance;
|
static NetDeviceFactory* instance;
|
||||||
static std::mutex m_mutex;
|
static std::mutex m_mutex;
|
||||||
|
|
||||||
// 共享池:用一个vector来表示
|
// 共享池:用一个vector来表示
|
||||||
vector<NetDevice*> devicePool;
|
vector<NetDevice*> devicePool;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -7,25 +7,25 @@ int main()
|
||||||
|
|
||||||
NetDevice *device1, *device2, *device3, *device4;
|
NetDevice *device1, *device2, *device3, *device4;
|
||||||
|
|
||||||
// 客户端2获取一个hub
|
// 客户端1获取一个hub
|
||||||
device1 = factory->getNetDevice('H');
|
device1 = factory->getNetDevice('H');
|
||||||
device1->print(1);
|
device1->print(1);
|
||||||
// 客户端2获取一个hub
|
// 客户端2获取一个hub
|
||||||
device2 = factory->getNetDevice('H');
|
device2 = factory->getNetDevice('H');
|
||||||
device2->print(2);
|
device2->print(2);
|
||||||
// 判断两个hub是否是同一个
|
// 判断两个hub是否是同一个
|
||||||
printf("判断两个hub是否是同一个:\n");
|
printf("判断两个hub是否是同一个:\n");
|
||||||
printf("device1:%p\ndevice2:%p\n", device1, device2);
|
printf("device1:%p\ndevice2:%p\n", device1, device2);
|
||||||
|
|
||||||
printf("\n\n\n\n");
|
printf("\n\n\n\n");
|
||||||
// 客户端3获取一个switch
|
// 客户端3获取一个switch
|
||||||
device3 = factory->getNetDevice('S');
|
device3 = factory->getNetDevice('S');
|
||||||
device3->print(1);
|
device3->print(1);
|
||||||
// 客户端4获取一个hub
|
// 客户端4获取一个switch
|
||||||
device4 = factory->getNetDevice('S');
|
device4 = factory->getNetDevice('S');
|
||||||
device4->print(2);
|
device4->print(2);
|
||||||
// 判断两个hub是否是同一个
|
// 判断两个switch是否是同一个
|
||||||
printf("判断两个switch是否是同一个:\n");
|
printf("判断两个switch是否是同一个:\n");
|
||||||
printf("device3:%p\ndevice4:%p\n", device3, device4);
|
printf("device3:%p\ndevice4:%p\n", device3, device4);
|
||||||
|
|
||||||
printf("\n\n");
|
printf("\n\n");
|
||||||
|
|
|
@ -5,7 +5,7 @@
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
// 请求:票据
|
// 请求:票据
|
||||||
class Bill
|
class Bill
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
@ -28,7 +28,8 @@ private:
|
||||||
string name;
|
string name;
|
||||||
double account;
|
double account;
|
||||||
};
|
};
|
||||||
// 抽象处理者
|
|
||||||
|
// 抽象处理者
|
||||||
class Approver
|
class Approver
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
@ -37,11 +38,11 @@ public:
|
||||||
setName(iName);
|
setName(iName);
|
||||||
}
|
}
|
||||||
virtual ~Approver(){}
|
virtual ~Approver(){}
|
||||||
// 添加上级
|
// 添加上级
|
||||||
void setSuperior(Approver *iSuperior){
|
void setSuperior(Approver *iSuperior){
|
||||||
this->superior = iSuperior;
|
this->superior = iSuperior;
|
||||||
}
|
}
|
||||||
// 处理请求
|
// 处理请求
|
||||||
virtual void handleRequest(Bill*) = 0;
|
virtual void handleRequest(Bill*) = 0;
|
||||||
string getName(){
|
string getName(){
|
||||||
return name;
|
return name;
|
||||||
|
@ -55,7 +56,7 @@ private:
|
||||||
string name;
|
string name;
|
||||||
};
|
};
|
||||||
|
|
||||||
// 具体处理者:组长
|
// 具体处理者:组长
|
||||||
class GroupLeader :public Approver
|
class GroupLeader :public Approver
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
@ -63,20 +64,20 @@ public:
|
||||||
GroupLeader(string iName){
|
GroupLeader(string iName){
|
||||||
setName(iName);
|
setName(iName);
|
||||||
}
|
}
|
||||||
// 处理请求
|
// 处理请求
|
||||||
void handleRequest(Bill *bill){
|
void handleRequest(Bill *bill){
|
||||||
if (bill->getAccount() < 10){
|
if (bill->getAccount() < 10){
|
||||||
printf("组长 %s 处理了该票据,票据信息:",this->getName().c_str());
|
printf("组长 %s 处理了该票据,票据信息:",this->getName().c_str());
|
||||||
bill->print();
|
bill->print();
|
||||||
}
|
}
|
||||||
else{
|
else{
|
||||||
printf("组长无权处理,转交上级……\n");
|
printf("组长无权处理,转交上级……\n");
|
||||||
this->superior->handleRequest(bill);
|
this->superior->handleRequest(bill);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// 具体处理者:主管
|
// 具体处理者:主管
|
||||||
class Head :public Approver
|
class Head :public Approver
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
@ -84,20 +85,20 @@ public:
|
||||||
Head(string iName){
|
Head(string iName){
|
||||||
setName(iName);
|
setName(iName);
|
||||||
}
|
}
|
||||||
// 处理请求
|
// 处理请求
|
||||||
void handleRequest(Bill *bill){
|
void handleRequest(Bill *bill){
|
||||||
if (bill->getAccount() >= 10 && bill->getAccount()<30){
|
if (bill->getAccount() >= 10 && bill->getAccount()<30){
|
||||||
printf("主管 %s 处理了该票据,票据信息:", this->getName().c_str());
|
printf("主管 %s 处理了该票据,票据信息:", this->getName().c_str());
|
||||||
bill->print();
|
bill->print();
|
||||||
}
|
}
|
||||||
else{
|
else{
|
||||||
printf("主管无权处理,转交上级……\n");
|
printf("主管无权处理,转交上级……\n");
|
||||||
this->superior->handleRequest(bill);
|
this->superior->handleRequest(bill);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// 具体处理者:经理
|
// 具体处理者:经理
|
||||||
class Manager :public Approver
|
class Manager :public Approver
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
@ -105,20 +106,20 @@ public:
|
||||||
Manager(string iName){
|
Manager(string iName){
|
||||||
setName(iName);
|
setName(iName);
|
||||||
}
|
}
|
||||||
// 处理请求
|
// 处理请求
|
||||||
void handleRequest(Bill *bill){
|
void handleRequest(Bill *bill){
|
||||||
if (bill->getAccount() >= 30 && bill->getAccount()<60){
|
if (bill->getAccount() >= 30 && bill->getAccount()<60){
|
||||||
printf("经理 %s 处理了该票据,票据信息:", this->getName().c_str());
|
printf("经理 %s 处理了该票据,票据信息:", this->getName().c_str());
|
||||||
bill->print();
|
bill->print();
|
||||||
}
|
}
|
||||||
else{
|
else{
|
||||||
printf("经理无权处理,转交上级……\n");
|
printf("经理无权处理,转交上级……\n");
|
||||||
this->superior->handleRequest(bill);
|
this->superior->handleRequest(bill);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// 具体处理者:老板
|
// 具体处理者:老板
|
||||||
class Boss :public Approver
|
class Boss :public Approver
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
@ -126,9 +127,9 @@ public:
|
||||||
Boss(string iName){
|
Boss(string iName){
|
||||||
setName(iName);
|
setName(iName);
|
||||||
}
|
}
|
||||||
// 处理请求
|
// 处理请求
|
||||||
void handleRequest(Bill *bill){
|
void handleRequest(Bill *bill){
|
||||||
printf("老板 %s 处理了该票据,票据信息:", this->getName().c_str());
|
printf("老板 %s 处理了该票据,票据信息:", this->getName().c_str());
|
||||||
bill->print();
|
bill->print();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
|
@ -3,25 +3,25 @@
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
// 请求处理者:组长,兵哥,春总,老板
|
// 请求处理者:组长,兵哥,春总,老板
|
||||||
Approver *zuzhang, *bingge, *chunzong, *laoban;
|
Approver *zuzhang, *bingge, *chunzong, *laoban;
|
||||||
|
|
||||||
zuzhang = new GroupLeader("孙大哥");
|
zuzhang = new GroupLeader("孙大哥");
|
||||||
bingge = new Head("兵哥");
|
bingge = new Head("兵哥");
|
||||||
chunzong = new Manager("春总");
|
chunzong = new Manager("春总");
|
||||||
laoban = new Boss("张老板");
|
laoban = new Boss("张老板");
|
||||||
|
|
||||||
zuzhang->setSuperior(bingge);
|
zuzhang->setSuperior(bingge);
|
||||||
bingge->setSuperior(chunzong);
|
bingge->setSuperior(chunzong);
|
||||||
chunzong->setSuperior(laoban);
|
chunzong->setSuperior(laoban);
|
||||||
|
|
||||||
// 创建报销单
|
// 创建报销单
|
||||||
Bill *bill1 = new Bill(1, "Jungle", 8);
|
Bill *bill1 = new Bill(1, "Jungle", 8);
|
||||||
Bill *bill2 = new Bill(2, "Lucy", 14.4);
|
Bill *bill2 = new Bill(2, "Lucy", 14.4);
|
||||||
Bill *bill3 = new Bill(3, "Jack", 32.9);
|
Bill *bill3 = new Bill(3, "Jack", 32.9);
|
||||||
Bill *bill4 = new Bill(4, "Tom", 89);
|
Bill *bill4 = new Bill(4, "Tom", 89);
|
||||||
|
|
||||||
// 全部先交给组长审批
|
// 全部先交给组长审批
|
||||||
zuzhang->handleRequest(bill1); printf("\n");
|
zuzhang->handleRequest(bill1); printf("\n");
|
||||||
zuzhang->handleRequest(bill2); printf("\n");
|
zuzhang->handleRequest(bill2); printf("\n");
|
||||||
zuzhang->handleRequest(bill3); printf("\n");
|
zuzhang->handleRequest(bill3); printf("\n");
|
||||||
|
|
|
@ -3,11 +3,11 @@
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
// 实例化调用者:按钮
|
// 实例化调用者:按钮
|
||||||
Button *button = new Button();
|
Button *button = new Button();
|
||||||
Command *lampCmd, *fanCmd;
|
Command *lampCmd, *fanCmd;
|
||||||
|
|
||||||
// 按钮控制电灯
|
// 按钮控制电灯
|
||||||
lampCmd = new LampCommand();
|
lampCmd = new LampCommand();
|
||||||
button->setCommand(lampCmd);
|
button->setCommand(lampCmd);
|
||||||
button->touch();
|
button->touch();
|
||||||
|
@ -16,7 +16,7 @@ int main()
|
||||||
|
|
||||||
printf("\n\n");
|
printf("\n\n");
|
||||||
|
|
||||||
// 按钮控制风扇
|
// 按钮控制风扇
|
||||||
fanCmd = new FanCommand();
|
fanCmd = new FanCommand();
|
||||||
button->setCommand(fanCmd);
|
button->setCommand(fanCmd);
|
||||||
button->touch();
|
button->touch();
|
||||||
|
@ -30,11 +30,9 @@ int main()
|
||||||
Command *lampCmd2, *fanCmd2;
|
Command *lampCmd2, *fanCmd2;
|
||||||
CommandQueue *cmdQueue = new CommandQueue();
|
CommandQueue *cmdQueue = new CommandQueue();
|
||||||
|
|
||||||
// 按钮控制电灯
|
|
||||||
lampCmd2 = new LampCommand();
|
lampCmd2 = new LampCommand();
|
||||||
cmdQueue->addCommand(lampCmd2);
|
cmdQueue->addCommand(lampCmd2);
|
||||||
|
|
||||||
// 按钮控制风扇
|
|
||||||
fanCmd2 = new FanCommand();
|
fanCmd2 = new FanCommand();
|
||||||
cmdQueue->addCommand(fanCmd2);
|
cmdQueue->addCommand(fanCmd2);
|
||||||
|
|
||||||
|
|
|
@ -7,17 +7,17 @@
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
// 抽象表达式类
|
// 抽象表达式类
|
||||||
class AbstractNode
|
class AbstractNode
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
AbstractNode(){}
|
AbstractNode(){}
|
||||||
virtual ~AbstractNode(){}
|
virtual ~AbstractNode(){}
|
||||||
// 声明抽象接口
|
// 声明抽象接口
|
||||||
virtual char interpret() = 0;
|
virtual char interpret() = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
// 终结符表达式:ValueNode
|
// 终结符表达式:ValueNode
|
||||||
class ValueNode :public AbstractNode
|
class ValueNode :public AbstractNode
|
||||||
{
|
{
|
||||||
public :
|
public :
|
||||||
|
@ -25,7 +25,7 @@ public :
|
||||||
ValueNode(int iValue){
|
ValueNode(int iValue){
|
||||||
this->value = iValue;
|
this->value = iValue;
|
||||||
}
|
}
|
||||||
// 实现解释操作
|
// 实现解释操作
|
||||||
char interpret(){
|
char interpret(){
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
@ -33,7 +33,7 @@ private:
|
||||||
int value;
|
int value;
|
||||||
};
|
};
|
||||||
|
|
||||||
// 终结符表达式:OperationNode
|
// 终结符表达式:OperationNode
|
||||||
class OperatorNode :public AbstractNode
|
class OperatorNode :public AbstractNode
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
@ -41,7 +41,7 @@ public:
|
||||||
OperatorNode(string iOp){
|
OperatorNode(string iOp){
|
||||||
this->op = iOp;
|
this->op = iOp;
|
||||||
}
|
}
|
||||||
// 实现解释操作
|
// 实现解释操作
|
||||||
char interpret(){
|
char interpret(){
|
||||||
if (op == "and"){
|
if (op == "and"){
|
||||||
return '&';
|
return '&';
|
||||||
|
@ -55,7 +55,7 @@ private:
|
||||||
string op;
|
string op;
|
||||||
};
|
};
|
||||||
|
|
||||||
// 非终结符表达式:SentenceNode
|
// 非终结符表达式:SentenceNode
|
||||||
class SentenceNode :public AbstractNode
|
class SentenceNode :public AbstractNode
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
@ -81,7 +81,7 @@ private:
|
||||||
AbstractNode *operatorNode;
|
AbstractNode *operatorNode;
|
||||||
};
|
};
|
||||||
|
|
||||||
// 处理者
|
// 处理者
|
||||||
class Handler
|
class Handler
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
|
|
@ -17,5 +17,5 @@ int Television::getTotalChannelNum(){
|
||||||
}
|
}
|
||||||
|
|
||||||
void Television::play(int i){
|
void Television::play(int i){
|
||||||
printf("<EFBFBD><EFBFBD><EFBFBD>ڲ<EFBFBD><EFBFBD>ţ<EFBFBD>%s<><73><EFBFBD><EFBFBD>\n", channelList[i].c_str());
|
printf("现在播放:%s……\n", channelList[i].c_str());
|
||||||
}
|
}
|
|
@ -2,13 +2,14 @@
|
||||||
#define __AGGREGATE_H__
|
#define __AGGREGATE_H__
|
||||||
|
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
#include <string>
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
// 前向声明,因为两个类互相引用
|
// 前向声明,因为两个类互相引用
|
||||||
class Iterator;
|
class Iterator;
|
||||||
class RemoteControl;
|
class RemoteControl;
|
||||||
|
|
||||||
// 抽象聚合类 Aggregate
|
// 抽象聚合类 Aggregate
|
||||||
class Aggregate
|
class Aggregate
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
@ -17,19 +18,19 @@ public:
|
||||||
virtual Iterator* createIterator() = 0;
|
virtual Iterator* createIterator() = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
// 具体聚合类 Television
|
// 具体聚合类 Television
|
||||||
class Television :public Aggregate
|
class Television :public Aggregate
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
Television();
|
Television();
|
||||||
Television(vector<string> iChannelList);
|
Television(vector<std::string> iChannelList);
|
||||||
// 实现创建迭代器
|
// 实现创建迭代器
|
||||||
Iterator* createIterator();
|
Iterator* createIterator();
|
||||||
// 获取总的频道数目
|
// 获取总的频道数目
|
||||||
int getTotalChannelNum();
|
int getTotalChannelNum();
|
||||||
void play(int i);
|
void play(int i);
|
||||||
private:
|
private:
|
||||||
vector<string> channelList;
|
vector<std::string> channelList;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif //__AGGREGATE_H__
|
#endif //__AGGREGATE_H__
|
|
@ -7,13 +7,13 @@
|
||||||
#include <vector>
|
#include <vector>
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
// 抽象迭代器
|
// 抽象迭代器
|
||||||
class Iterator
|
class Iterator
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
Iterator(){}
|
Iterator(){}
|
||||||
virtual ~Iterator(){}
|
virtual ~Iterator(){}
|
||||||
// 声明抽象遍历方法
|
// 声明抽象遍历方法
|
||||||
virtual void first() = 0;
|
virtual void first() = 0;
|
||||||
virtual void last() = 0;
|
virtual void last() = 0;
|
||||||
virtual void next() = 0;
|
virtual void next() = 0;
|
||||||
|
@ -25,7 +25,7 @@ private:
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// 遥控器:具体迭代器
|
// 遥控器:具体迭代器
|
||||||
class RemoteControl :public Iterator
|
class RemoteControl :public Iterator
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
@ -35,7 +35,7 @@ public:
|
||||||
cursor = -1;
|
cursor = -1;
|
||||||
totalNum = tv->getTotalChannelNum();
|
totalNum = tv->getTotalChannelNum();
|
||||||
}
|
}
|
||||||
// 实现各个遍历方法
|
// 实现各个遍历方法
|
||||||
void first(){
|
void first(){
|
||||||
cursor = 0;
|
cursor = 0;
|
||||||
}
|
}
|
||||||
|
@ -58,11 +58,11 @@ public:
|
||||||
tv->play(cursor);
|
tv->play(cursor);
|
||||||
}
|
}
|
||||||
private:
|
private:
|
||||||
// 游标
|
// 游标
|
||||||
int cursor;
|
int cursor;
|
||||||
// 总的频道数目
|
// 总的频道数目
|
||||||
int totalNum;
|
int totalNum;
|
||||||
// 电视
|
// 电视
|
||||||
Television* tv;
|
Television* tv;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -3,16 +3,16 @@
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
vector<string> channelList = { "新闻频道", "财经频道", "体育频道", "电影频道", "音乐频道", "农业频道", "四川卫视", "成都卫视" };
|
vector<string> channelList = { "新闻频道", "财经频道", "体育频道", "电影频道", "音乐频道", "农业频道", "四川卫视", "成都卫视" };
|
||||||
// 创建电视
|
// 创建电视
|
||||||
Television *tv = new Television(channelList);
|
Television *tv = new Television(channelList);
|
||||||
// 创建遥控器
|
// 创建遥控器
|
||||||
Iterator *remoteControl = tv->createIterator();
|
Iterator *remoteControl = tv->createIterator();
|
||||||
|
|
||||||
// 顺序遍历
|
// 顺序遍历
|
||||||
printf("顺序遍历:\n");
|
printf("顺序遍历:\n");
|
||||||
remoteControl->first();
|
remoteControl->first();
|
||||||
// 遍历电视所有频道
|
// 遍历电视所有频道
|
||||||
while (remoteControl->hasNext()){
|
while (remoteControl->hasNext()){
|
||||||
remoteControl->currentChannel();
|
remoteControl->currentChannel();
|
||||||
remoteControl->next();
|
remoteControl->next();
|
||||||
|
@ -20,10 +20,10 @@ int main()
|
||||||
|
|
||||||
printf("\n\n");
|
printf("\n\n");
|
||||||
|
|
||||||
// 逆序遍历
|
// 逆序遍历
|
||||||
printf("逆序遍历:\n");
|
printf("<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>:\n");
|
||||||
remoteControl->last();
|
remoteControl->last();
|
||||||
// 遍历电视所有频道
|
// 遍历电视所有频道
|
||||||
while (remoteControl->hasPrevious()){
|
while (remoteControl->hasPrevious()){
|
||||||
remoteControl->currentChannel();
|
remoteControl->currentChannel();
|
||||||
remoteControl->previous();
|
remoteControl->previous();
|
||||||
|
|
|
@ -4,11 +4,11 @@
|
||||||
#include "common.h"
|
#include "common.h"
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
// 前向声明
|
// 前向声明
|
||||||
class Mediator;
|
class Mediator;
|
||||||
class Agency;
|
class Agency;
|
||||||
|
|
||||||
// 抽象同事类
|
// 抽象同事类
|
||||||
class Colleague
|
class Colleague
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
@ -33,7 +33,7 @@ private:
|
||||||
Mediator* mediator;
|
Mediator* mediator;
|
||||||
};
|
};
|
||||||
|
|
||||||
// 具体同事类:房东
|
// 具体同事类:房东
|
||||||
class Landlord :public Colleague
|
class Landlord :public Colleague
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
@ -48,7 +48,7 @@ private:
|
||||||
string phoneNumber;
|
string phoneNumber;
|
||||||
};
|
};
|
||||||
|
|
||||||
// 具体同事类:租客
|
// 具体同事类:租客
|
||||||
class Tenant :public Colleague
|
class Tenant :public Colleague
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
|
|
@ -19,11 +19,11 @@ Landlord::Landlord(string iName, int iPrice,
|
||||||
}
|
}
|
||||||
|
|
||||||
void Landlord::answer(){
|
void Landlord::answer(){
|
||||||
printf("房东姓名:%s, 房租:%d, 地址:%s, 联系电话:%s\n",
|
printf("房东姓名:%s, 房租:%d, 地址:%s, 联系电话:%s\n",
|
||||||
name.c_str(), price, address.c_str(), phoneNumber.c_str());
|
name.c_str(), price, address.c_str(), phoneNumber.c_str());
|
||||||
}
|
}
|
||||||
|
|
||||||
void Landlord::ask(){
|
void Landlord::ask(){
|
||||||
printf("房东%s查看租客信息:\n",name.c_str());
|
printf("房东%s查看租客信息:\n",name.c_str());
|
||||||
(this->getMediator())->operation(this);
|
(this->getMediator())->operation(this);
|
||||||
}
|
}
|
|
@ -4,19 +4,19 @@
|
||||||
#include "common.h"
|
#include "common.h"
|
||||||
#include "Colleague.h"
|
#include "Colleague.h"
|
||||||
|
|
||||||
// 抽象中介者
|
// 抽象中介者
|
||||||
class Mediator
|
class Mediator
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
Mediator(){}
|
Mediator(){}
|
||||||
virtual ~Mediator(){}
|
virtual ~Mediator(){}
|
||||||
// 声明抽象方法
|
// 声明抽象方法
|
||||||
virtual void operation(Colleague*) = 0;
|
virtual void operation(Colleague*) = 0;
|
||||||
// 声明注册方法
|
// 声明注册方法
|
||||||
virtual void registerMethod(Colleague*) = 0;
|
virtual void registerMethod(Colleague*) = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
// 具体中介者
|
// 具体中介者
|
||||||
class Agency:public Mediator
|
class Agency:public Mediator
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
|
|
@ -1,21 +1,25 @@
|
||||||
#include "Colleague.h"
|
#include "Colleague.h"
|
||||||
#include "Mediator.h"
|
#include "Mediator.h"
|
||||||
|
|
||||||
Tenant::Tenant(){
|
Tenant::Tenant()
|
||||||
|
{
|
||||||
name = "none";
|
name = "none";
|
||||||
setPersonType(NONE_PERSON);
|
setPersonType(NONE_PERSON);
|
||||||
}
|
}
|
||||||
|
|
||||||
Tenant::Tenant(string iName){
|
Tenant::Tenant(string iName)
|
||||||
|
{
|
||||||
name = iName;
|
name = iName;
|
||||||
setPersonType(TENANT);
|
setPersonType(TENANT);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Tenant::ask(){
|
void Tenant::ask()
|
||||||
printf("租客%s询问房东信息\n", name.c_str());
|
{
|
||||||
|
printf("租客%s询问房东信息\n", name.c_str());
|
||||||
(this->getMediator())->operation(this);
|
(this->getMediator())->operation(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Tenant::answer(){
|
void Tenant::answer()
|
||||||
printf("租客姓名:%s\n", name.c_str());
|
{
|
||||||
|
printf("租客姓名:%s\n", name.c_str());
|
||||||
}
|
}
|
|
@ -1,7 +1,7 @@
|
||||||
#ifndef __COMMON_H__
|
#ifndef __COMMON_H__
|
||||||
#define __COMMON_H__
|
#define __COMMON_H__
|
||||||
|
|
||||||
// 公共头文件
|
// 公共头文件
|
||||||
|
|
||||||
#include <vector>
|
#include <vector>
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
|
@ -4,27 +4,27 @@
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
// 创建租房中介
|
// 创建租房中介
|
||||||
Agency *mediator = new Agency();
|
Agency *mediator = new Agency();
|
||||||
|
|
||||||
// 创建3位房东
|
// 创建3位房东
|
||||||
Landlord *fangdong1 = new Landlord("刘备", 1350, "成都市双流区", "1351025");
|
Landlord *fangdong1 = new Landlord("刘备", 1350, "成都市双流区", "1351025");
|
||||||
Landlord *fangdong2 = new Landlord("关羽", 1500, "成都市武侯区", "1378390");
|
Landlord *fangdong2 = new Landlord("关羽", 1500, "成都市武侯区", "1378390");
|
||||||
Landlord *fangdong3 = new Landlord("张飞", 1000, "成都市龙泉驿", "1881166");
|
Landlord *fangdong3 = new Landlord("张飞", 1000, "成都市龙泉驿", "1881166");
|
||||||
fangdong1->setMediator(mediator);
|
fangdong1->setMediator(mediator);
|
||||||
fangdong2->setMediator(mediator);
|
fangdong2->setMediator(mediator);
|
||||||
fangdong3->setMediator(mediator);
|
fangdong3->setMediator(mediator);
|
||||||
// 房东在中介处登记注册房源信息
|
// 房东在中介处登记注册房源信息
|
||||||
mediator->registerMethod(fangdong1);
|
mediator->registerMethod(fangdong1);
|
||||||
mediator->registerMethod(fangdong2);
|
mediator->registerMethod(fangdong2);
|
||||||
mediator->registerMethod(fangdong3);
|
mediator->registerMethod(fangdong3);
|
||||||
|
|
||||||
// 创建两位租客Jungle和贱萌兔
|
// 创建两位租客Jungle和贱萌兔
|
||||||
Tenant *jungle = new Tenant("Jungle");
|
Tenant *jungle = new Tenant("Jungle");
|
||||||
Tenant *jianmengtu = new Tenant("贱萌兔");
|
Tenant *jianmengtu = new Tenant("<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>");
|
||||||
jungle->setMediator(mediator);
|
jungle->setMediator(mediator);
|
||||||
jianmengtu->setMediator(mediator);
|
jianmengtu->setMediator(mediator);
|
||||||
// Jungle和贱萌兔在中介处登记求租信息
|
// Jungle和贱萌兔在中介处登记求租信息
|
||||||
mediator->registerMethod(jungle);
|
mediator->registerMethod(jungle);
|
||||||
mediator->registerMethod(jianmengtu);
|
mediator->registerMethod(jianmengtu);
|
||||||
|
|
||||||
|
|
|
@ -4,33 +4,34 @@
|
||||||
#include "common.h"
|
#include "common.h"
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
// 前向声明
|
// 前向声明
|
||||||
class Observer;
|
class Observer;
|
||||||
class Player;
|
class Player;
|
||||||
|
|
||||||
// 抽象目标:联盟中心
|
// 抽象目标:联盟中心
|
||||||
class AllyCenter
|
class AllyCenter
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
AllyCenter();
|
AllyCenter();
|
||||||
virtual ~AllyCenter(){}
|
virtual ~AllyCenter() {}
|
||||||
// 声明通知方法
|
// 声明通知方法
|
||||||
virtual void notify(INFO_TYPE infoType, std::string name) = 0;
|
virtual void notify(INFO_TYPE infoType, std::string name) = 0;
|
||||||
// 加入玩家
|
// 加入玩家
|
||||||
void join(Observer* player);
|
void join(Observer *player);
|
||||||
// 移除玩家
|
// 移除玩家
|
||||||
void remove(Observer* player);
|
void remove(Observer *player);
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
// 玩家列表
|
// 玩家列表
|
||||||
std::vector<Observer*>playerList;
|
std::vector<Observer *> playerList;
|
||||||
};
|
};
|
||||||
|
|
||||||
// 具体目标
|
// 具体目标
|
||||||
class AllyCenterController :public AllyCenter
|
class AllyCenterController : public AllyCenter
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
AllyCenterController();
|
AllyCenterController();
|
||||||
// 实现通知方法
|
// 实现通知方法
|
||||||
void notify(INFO_TYPE infoType, std::string name);
|
void notify(INFO_TYPE infoType, std::string name);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -4,63 +4,69 @@
|
||||||
using namespace std;
|
using namespace std;
|
||||||
#include <list>
|
#include <list>
|
||||||
|
|
||||||
// 抽象观察者
|
// 抽象观察者
|
||||||
class Observer
|
class Observer
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
virtual ~Observer() {}
|
virtual ~Observer() {}
|
||||||
// 声明响应更新方法
|
// 声明响应更新方法
|
||||||
virtual void update() = 0;
|
virtual void update() = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
// 具体观察者
|
// 具体观察者
|
||||||
class ConcreteObserver:public Observer
|
class ConcreteObserver : public Observer
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
// 实现响应更新方法
|
// 实现响应更新方法
|
||||||
void update(){
|
void update()
|
||||||
// 具体操作
|
{
|
||||||
|
// 具体操作
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// 抽象目标
|
// 抽象目标
|
||||||
class Subject
|
class Subject
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
virtual ~Subject() {}
|
virtual ~Subject() {}
|
||||||
// 添加观察者
|
// 添加观察者
|
||||||
void attach(Observer* obs){
|
void attach(Observer *obs)
|
||||||
|
{
|
||||||
obsList.push_back(obs);
|
obsList.push_back(obs);
|
||||||
}
|
}
|
||||||
// 移除观察者
|
// 移除观察者
|
||||||
void detach(Observer* obs){
|
void detach(Observer *obs)
|
||||||
|
{
|
||||||
obsList.remove(obs);
|
obsList.remove(obs);
|
||||||
}
|
}
|
||||||
// 声明通知方法
|
// 声明通知方法
|
||||||
virtual void notify() = 0;
|
virtual void notify() = 0;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
// 观察者列表
|
// 观察者列表
|
||||||
list<Observer*>obsList;
|
list<Observer *> obsList;
|
||||||
};
|
};
|
||||||
|
|
||||||
// 具体目标
|
// 具体目标
|
||||||
class ConcreteSubject :public Subject
|
class ConcreteSubject : public Subject
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
// 实现通知方法
|
// 实现通知方法
|
||||||
void notify(){
|
void notify()
|
||||||
// 具体操作
|
{
|
||||||
// 遍历通知观察者对象
|
// 具体操作
|
||||||
for (int i = 0; i < obsList.size(); i++){
|
// 遍历通知观察者对象
|
||||||
|
for (int i = 0; i < obsList.size(); i++)
|
||||||
|
{
|
||||||
obsList[i]->update();
|
obsList[i]->update();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// 客户端代码示例
|
// 客户端代码示例
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
Subject *sub = new ConcreteSubject();
|
Subject *sub = new ConcreteSubject();
|
||||||
Observer *obs = new ConcreteObserver();
|
Observer *obs = new ConcreteObserver();
|
||||||
sub->attach(obs);
|
sub->attach(obs);
|
||||||
sub->notify();
|
sub->notify();
|
||||||
|
|
|
@ -6,13 +6,13 @@ using namespace std;
|
||||||
#include "common.h"
|
#include "common.h"
|
||||||
#include "AllyCenter.h"
|
#include "AllyCenter.h"
|
||||||
|
|
||||||
// 抽象观察者 Observer
|
// 抽象观察者 Observer
|
||||||
class Observer
|
class Observer
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
virtual ~Observer(){}
|
virtual ~Observer(){}
|
||||||
Observer(){}
|
Observer(){}
|
||||||
// 声明抽象方法
|
// 声明抽象方法
|
||||||
virtual void call(INFO_TYPE infoType, AllyCenter* ac) = 0;
|
virtual void call(INFO_TYPE infoType, AllyCenter* ac) = 0;
|
||||||
string getName(){
|
string getName(){
|
||||||
return name;
|
return name;
|
||||||
|
@ -24,7 +24,7 @@ private:
|
||||||
string name;
|
string name;
|
||||||
};
|
};
|
||||||
|
|
||||||
// 具体观察者
|
// 具体观察者
|
||||||
class Player :public Observer
|
class Player :public Observer
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
@ -34,26 +34,26 @@ public:
|
||||||
Player(string iName){
|
Player(string iName){
|
||||||
setName(iName);
|
setName(iName);
|
||||||
}
|
}
|
||||||
// 实现
|
// 实现
|
||||||
void call(INFO_TYPE infoType, AllyCenter* ac){
|
void call(INFO_TYPE infoType, AllyCenter* ac){
|
||||||
switch (infoType){
|
switch (infoType){
|
||||||
case RESOURCE:
|
case RESOURCE:
|
||||||
printf("%s :我这里有物资\n", getName().c_str());
|
printf("%s :我这里有物资\n", getName().c_str());
|
||||||
break;
|
break;
|
||||||
case HELP:
|
case HELP:
|
||||||
printf("%s :救救我\n", getName().c_str());
|
printf("%s :救救我\n", getName().c_str());
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
printf("Nothing\n");
|
printf("Nothing\n");
|
||||||
}
|
}
|
||||||
ac->notify(infoType, getName());
|
ac->notify(infoType, getName());
|
||||||
}
|
}
|
||||||
// 实现具体方法
|
// 实现具体方法
|
||||||
void help(){
|
void help(){
|
||||||
printf("%s:坚持住,我来救你!\n", getName().c_str());
|
printf("%s:坚持住,我来救你!\n", getName().c_str());
|
||||||
}
|
}
|
||||||
void come(){
|
void come(){
|
||||||
printf("%s:好的,我来取物资\n", getName().c_str());
|
printf("%s:好的,我来取物资\n", getName().c_str());
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -3,14 +3,14 @@
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
// 创建一个战队
|
// 创建一个战队
|
||||||
AllyCenterController* controller = new AllyCenterController();
|
AllyCenterController *controller = new AllyCenterController();
|
||||||
|
|
||||||
// 创建4个玩家,并加入战队
|
// 创建4个玩家,并加入战队
|
||||||
Player* Jungle = new Player("Jungle");
|
Player *Jungle = new Player("Jungle");
|
||||||
Player* Single = new Player("Single");
|
Player *Single = new Player("Single");
|
||||||
Player* Jianmengtu = new Player("贱萌兔");
|
Player *Jianmengtu = new Player("<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>");
|
||||||
Player* SillyDog = new Player("傻子狗");
|
Player *SillyDog = new Player("ɵ<EFBFBD>ӹ<EFBFBD>");
|
||||||
controller->join(Jungle);
|
controller->join(Jungle);
|
||||||
controller->join(Single);
|
controller->join(Single);
|
||||||
controller->join(Jianmengtu);
|
controller->join(Jianmengtu);
|
||||||
|
@ -18,12 +18,12 @@ int main()
|
||||||
|
|
||||||
printf("\n\n");
|
printf("\n\n");
|
||||||
|
|
||||||
// Jungle发现物资,呼叫队友
|
// Jungle发现物资,呼叫队友
|
||||||
Jungle->call(RESOURCE, controller);
|
Jungle->call(RESOURCE, controller);
|
||||||
|
|
||||||
printf("\n\n");
|
printf("\n\n");
|
||||||
|
|
||||||
// 傻子狗遇到危险,求救队友
|
// 傻子狗遇到危险,求救队友
|
||||||
SillyDog->call(HELP, controller);
|
SillyDog->call(HELP, controller);
|
||||||
|
|
||||||
printf("\n\n");
|
printf("\n\n");
|
||||||
|
|
|
@ -3,7 +3,7 @@
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
// Η°ΟςΙωΓχ
|
// 前向声明
|
||||||
class Level;
|
class Level;
|
||||||
|
|
||||||
class GameAccount
|
class GameAccount
|
||||||
|
|
|
@ -5,20 +5,26 @@
|
||||||
#define random(x) (rand()%x)
|
#define random(x) (rand()%x)
|
||||||
|
|
||||||
GameAccount::GameAccount(){
|
GameAccount::GameAccount(){
|
||||||
printf("创立游戏角色,积分:100,级别:PRIMARY\n");
|
level = nullptr;
|
||||||
|
printf("创立游戏角色,积分:100,级别:PRIMARY\n");
|
||||||
score = 100;
|
score = 100;
|
||||||
name = "none";
|
name = "none";
|
||||||
setLevel(new Primary(this));
|
setLevel(new Primary(this));
|
||||||
}
|
}
|
||||||
|
|
||||||
GameAccount::GameAccount(string iName){
|
GameAccount::GameAccount(string iName){
|
||||||
printf("创立游戏角色,积分:100,级别:PRIMARY\n");
|
level = nullptr;
|
||||||
|
printf("创立游戏角色,积分:100,级别:PRIMARY\n");
|
||||||
score = 100;
|
score = 100;
|
||||||
name = iName;
|
name = iName;
|
||||||
setLevel(new Primary(this));
|
setLevel(new Primary(this));
|
||||||
}
|
}
|
||||||
|
|
||||||
void GameAccount::setLevel(Level* iLevel){
|
void GameAccount::setLevel(Level* iLevel){
|
||||||
|
if(level != nullptr){
|
||||||
|
delete level;
|
||||||
|
level = nullptr;
|
||||||
|
}
|
||||||
this->level = iLevel;
|
this->level = iLevel;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -49,12 +55,12 @@ void GameAccount::win(){
|
||||||
else{
|
else{
|
||||||
setScore(getScore() + 100);
|
setScore(getScore() + 100);
|
||||||
}
|
}
|
||||||
printf("\n\t胜利,最新积分为 %d\n", score);
|
printf("\n\t胜利,最新积分为 %d\n", score);
|
||||||
}
|
}
|
||||||
|
|
||||||
void GameAccount::lose(){
|
void GameAccount::lose(){
|
||||||
setScore(getScore() + 30);
|
setScore(getScore() + 30);
|
||||||
printf("\n\t输牌,最新积分为 %d\n", score);
|
printf("\n\t输牌,最新积分为 %d\n", score);
|
||||||
}
|
}
|
||||||
|
|
||||||
int GameAccount::getScore(){
|
int GameAccount::getScore(){
|
||||||
|
|
|
@ -13,7 +13,7 @@ void Level::playCard(){
|
||||||
}
|
}
|
||||||
|
|
||||||
void Level::play(){
|
void Level::play(){
|
||||||
printf("\t使用基本技能,");
|
printf("\t使用基本技能,");
|
||||||
}
|
}
|
||||||
|
|
||||||
void Level::setGameAccount(GameAccount* iGameAccount){
|
void Level::setGameAccount(GameAccount* iGameAccount){
|
||||||
|
@ -52,7 +52,7 @@ void Primary::peekCards(){
|
||||||
void Primary::upgradeLevel(){
|
void Primary::upgradeLevel(){
|
||||||
if (this->getGameAccount()->getScore() > 150){
|
if (this->getGameAccount()->getScore() > 150){
|
||||||
this->getGameAccount()->setLevel(new Secondary(this));
|
this->getGameAccount()->setLevel(new Secondary(this));
|
||||||
printf("\t升级! 级别:SECONDARY\n\n");
|
printf("\t升级! 级别:SECONDARY\n\n");
|
||||||
}
|
}
|
||||||
else{
|
else{
|
||||||
printf("\n");
|
printf("\n");
|
||||||
|
@ -70,7 +70,7 @@ Secondary::Secondary(Level* level){
|
||||||
}
|
}
|
||||||
|
|
||||||
void Secondary::doubleScore(){
|
void Secondary::doubleScore(){
|
||||||
printf("使用胜利双倍积分技能");
|
printf("使用胜利双倍积分技能");
|
||||||
}
|
}
|
||||||
|
|
||||||
void Secondary::changeCards(){
|
void Secondary::changeCards(){
|
||||||
|
@ -84,11 +84,11 @@ void Secondary::peekCards(){
|
||||||
void Secondary::upgradeLevel(){
|
void Secondary::upgradeLevel(){
|
||||||
if (this->getGameAccount()->getScore() < 150){
|
if (this->getGameAccount()->getScore() < 150){
|
||||||
this->getGameAccount()->setLevel(new Primary(this));
|
this->getGameAccount()->setLevel(new Primary(this));
|
||||||
printf("\t降级! 级别:PRIMARY\n\n");
|
printf("\t降级! 级别:PRIMARY\n\n");
|
||||||
}
|
}
|
||||||
else if (this->getGameAccount()->getScore() > 200){
|
else if (this->getGameAccount()->getScore() > 200){
|
||||||
this->getGameAccount()->setLevel(new Professional(this));
|
this->getGameAccount()->setLevel(new Professional(this));
|
||||||
printf("\t升级! 级别:PROFESSIONAL\n\n");
|
printf("\t升级! 级别:PROFESSIONAL\n\n");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -103,11 +103,11 @@ Professional::Professional(Level* level){
|
||||||
}
|
}
|
||||||
|
|
||||||
void Professional::doubleScore(){
|
void Professional::doubleScore(){
|
||||||
printf("使用胜利双倍积分技能,");
|
printf("使用胜利双倍积分技能,");
|
||||||
}
|
}
|
||||||
|
|
||||||
void Professional::changeCards(){
|
void Professional::changeCards(){
|
||||||
printf("使用换牌技能");
|
printf("使用换牌技能");
|
||||||
}
|
}
|
||||||
|
|
||||||
void Professional::peekCards(){
|
void Professional::peekCards(){
|
||||||
|
@ -117,11 +117,11 @@ void Professional::peekCards(){
|
||||||
void Professional::upgradeLevel(){
|
void Professional::upgradeLevel(){
|
||||||
if (this->getGameAccount()->getScore() < 200){
|
if (this->getGameAccount()->getScore() < 200){
|
||||||
this->getGameAccount()->setLevel(new Secondary(this));
|
this->getGameAccount()->setLevel(new Secondary(this));
|
||||||
printf("\t降级! 级别:SECONDARY\n\n");
|
printf("\t降级! 级别:SECONDARY\n\n");
|
||||||
}
|
}
|
||||||
else if (this->getGameAccount()->getScore() > 250){
|
else if (this->getGameAccount()->getScore() > 250){
|
||||||
this->getGameAccount()->setLevel(new Final(this));
|
this->getGameAccount()->setLevel(new Final(this));
|
||||||
printf("\t升级! 级别:FINAL\n\n");
|
printf("\t升级! 级别:FINAL\n\n");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -136,23 +136,23 @@ Final::Final(Level* level){
|
||||||
}
|
}
|
||||||
|
|
||||||
void Final::doubleScore(){
|
void Final::doubleScore(){
|
||||||
printf("使用胜利双倍积分技能,");
|
printf("使用胜利双倍积分技能,");
|
||||||
}
|
}
|
||||||
|
|
||||||
void Final::changeCards(){
|
void Final::changeCards(){
|
||||||
printf("使用换牌技能,");
|
printf("使用换牌技能,");
|
||||||
}
|
}
|
||||||
|
|
||||||
void Final::peekCards(){
|
void Final::peekCards(){
|
||||||
printf("使用偷看卡牌技能");
|
printf("使用偷看卡牌技能");
|
||||||
}
|
}
|
||||||
|
|
||||||
void Final::upgradeLevel(){
|
void Final::upgradeLevel(){
|
||||||
if (this->getGameAccount()->getScore() < 250){
|
if (this->getGameAccount()->getScore() < 250){
|
||||||
this->getGameAccount()->setLevel(new Professional(this));
|
this->getGameAccount()->setLevel(new Professional(this));
|
||||||
printf("\t降级! 级别:PROFESSIONAL\n\n");
|
printf("\t降级! 级别:PROFESSIONAL\n\n");
|
||||||
}
|
}
|
||||||
else{
|
else{
|
||||||
printf("\t%s 已经是最高级\n\n", this->getGameAccount()->getName().c_str());
|
printf("\t%s 已经是最高级\n\n", this->getGameAccount()->getName().c_str());
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -8,13 +8,13 @@ class Level
|
||||||
public :
|
public :
|
||||||
Level();
|
Level();
|
||||||
virtual ~Level(){}
|
virtual ~Level(){}
|
||||||
// 声明方法
|
// 声明方法
|
||||||
void playCard();
|
void playCard();
|
||||||
void play();
|
void play();
|
||||||
virtual void doubleScore() = 0;
|
virtual void doubleScore() = 0;
|
||||||
virtual void changeCards() = 0;
|
virtual void changeCards() = 0;
|
||||||
virtual void peekCards() = 0;
|
virtual void peekCards() = 0;
|
||||||
// 升级
|
// 升级
|
||||||
virtual void upgradeLevel() = 0;
|
virtual void upgradeLevel() = 0;
|
||||||
GameAccount* getGameAccount();
|
GameAccount* getGameAccount();
|
||||||
void setGameAccount(GameAccount* iGameAccount);
|
void setGameAccount(GameAccount* iGameAccount);
|
||||||
|
@ -36,7 +36,7 @@ public:
|
||||||
void doubleScore();
|
void doubleScore();
|
||||||
void changeCards();
|
void changeCards();
|
||||||
void peekCards();
|
void peekCards();
|
||||||
// 升级
|
// 升级
|
||||||
void upgradeLevel();
|
void upgradeLevel();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -48,7 +48,7 @@ public:
|
||||||
void doubleScore();
|
void doubleScore();
|
||||||
void changeCards();
|
void changeCards();
|
||||||
void peekCards();
|
void peekCards();
|
||||||
// 升级
|
// 升级
|
||||||
void upgradeLevel();
|
void upgradeLevel();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -60,7 +60,7 @@ public:
|
||||||
void doubleScore();
|
void doubleScore();
|
||||||
void changeCards();
|
void changeCards();
|
||||||
void peekCards();
|
void peekCards();
|
||||||
// 升级
|
// 升级
|
||||||
void upgradeLevel();
|
void upgradeLevel();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -72,7 +72,7 @@ public:
|
||||||
void doubleScore();
|
void doubleScore();
|
||||||
void changeCards();
|
void changeCards();
|
||||||
void peekCards();
|
void peekCards();
|
||||||
// 升级
|
// 升级
|
||||||
void upgradeLevel();
|
void upgradeLevel();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -4,7 +4,7 @@
|
||||||
#include "Strategy.h"
|
#include "Strategy.h"
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
// 上下文类
|
// 上下文类
|
||||||
class Context
|
class Context
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
@ -38,7 +38,7 @@ public:
|
||||||
}
|
}
|
||||||
void sort(){
|
void sort(){
|
||||||
this->sortStrategy->sort(arr, N);
|
this->sortStrategy->sort(arr, N);
|
||||||
printf("输出: ");
|
printf("输出: ");
|
||||||
this->print();
|
this->print();
|
||||||
}
|
}
|
||||||
void setInput(int iArr[], int iN){
|
void setInput(int iArr[], int iN){
|
||||||
|
|
|
@ -3,7 +3,7 @@
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
// 抽象策略类
|
// 抽象策略类
|
||||||
class Strategy
|
class Strategy
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
@ -12,12 +12,12 @@ public:
|
||||||
virtual void sort(int arr[], int N) = 0;
|
virtual void sort(int arr[], int N) = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
// 具体策略:冒泡排序
|
// 具体策略:冒泡排序
|
||||||
class BubbleSort :public Strategy
|
class BubbleSort :public Strategy
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
BubbleSort(){
|
BubbleSort(){
|
||||||
printf("冒泡排序\n");
|
printf("冒泡排序\n");
|
||||||
}
|
}
|
||||||
void sort(int arr[], int N){
|
void sort(int arr[], int N){
|
||||||
for (int i = 0; i<N; i++)
|
for (int i = 0; i<N; i++)
|
||||||
|
@ -34,12 +34,12 @@ public:
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// 具体策略:选择排序
|
// 具体策略:选择排序
|
||||||
class SelectionSort :public Strategy
|
class SelectionSort :public Strategy
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
SelectionSort(){
|
SelectionSort(){
|
||||||
printf("选择排序\n");
|
printf("选择排序\n");
|
||||||
}
|
}
|
||||||
void sort(int arr[], int N){
|
void sort(int arr[], int N){
|
||||||
int i, j, k;
|
int i, j, k;
|
||||||
|
@ -59,12 +59,12 @@ public:
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// 具体策略:插入排序
|
// 具体策略:插入排序
|
||||||
class InsertSort :public Strategy
|
class InsertSort :public Strategy
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
InsertSort(){
|
InsertSort(){
|
||||||
printf("插入排序\n");
|
printf("插入排序\n");
|
||||||
}
|
}
|
||||||
void sort(int arr[], int N){
|
void sort(int arr[], int N){
|
||||||
int i, j;
|
int i, j;
|
||||||
|
|
|
@ -1,39 +1,39 @@
|
||||||
#ifndef __DEMO_H__
|
#ifndef __DEMO_H__
|
||||||
#define __DEMO_H__
|
#define __DEMO_H__
|
||||||
|
|
||||||
// 抽象类(基类)
|
// 抽象类(基类)
|
||||||
class AbstractClass
|
class AbstractClass
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
virtual ~AbstractClass(){}
|
virtual ~AbstractClass(){}
|
||||||
// 模板方法,定义一个算法的框架流程
|
// 模板方法,定义一个算法的框架流程
|
||||||
void templateMethod(){
|
void templateMethod(){
|
||||||
// do something
|
// do something
|
||||||
method1();
|
method1();
|
||||||
method2();
|
method2();
|
||||||
method3();
|
method3();
|
||||||
}
|
}
|
||||||
// 基本方法——公共方法
|
// 基本方法——公共方法
|
||||||
void mehtod1(){
|
void mehtod1(){
|
||||||
// do something
|
// do something
|
||||||
}
|
}
|
||||||
// 基本方法2
|
// 基本方法2
|
||||||
virtual void method2() = 0;
|
virtual void method2() = 0;
|
||||||
// 基本方法3——默认实现
|
// 基本方法3——默认实现
|
||||||
void mehtod3(){
|
void mehtod3(){
|
||||||
// do something
|
// do something
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// 具体类(派生类)
|
// 具体类(派生类)
|
||||||
class ConcreteClass :public AbstractClass
|
class ConcreteClass :public AbstractClass
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
// 实现基本方法2
|
// 实现基本方法2
|
||||||
void method2(){
|
void method2(){
|
||||||
// do something
|
// do something
|
||||||
}
|
}
|
||||||
// 重定义基本方法3,覆盖基类的方法3
|
// 重定义基本方法3,覆盖基类的方法3
|
||||||
void method3(){
|
void method3(){
|
||||||
// do something
|
// do something
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,96 +3,110 @@
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
// 基类
|
// 基类
|
||||||
class FingerprintModule
|
class FingerprintModule
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
FingerprintModule(){}
|
FingerprintModule() {}
|
||||||
virtual ~FingerprintModule(){}
|
virtual ~FingerprintModule() {}
|
||||||
void getImage(){
|
void getImage()
|
||||||
printf("采指纹图像\n");
|
{
|
||||||
|
printf("采指纹图像\n");
|
||||||
}
|
}
|
||||||
void output(){
|
void output()
|
||||||
printf("指纹图像处理完成!\n\n");
|
{
|
||||||
|
printf("指纹图像处理完成!\n");
|
||||||
}
|
}
|
||||||
virtual bool isSafeMode() = 0;
|
virtual bool isSafeMode() = 0;
|
||||||
virtual void processImage() = 0;
|
virtual void processImage() = 0;
|
||||||
// 加解密
|
// 加解密
|
||||||
virtual void encrypt() = 0;
|
virtual void encrypt() = 0;
|
||||||
virtual void decrypt() = 0;
|
virtual void decrypt() = 0;
|
||||||
|
|
||||||
// 模板方法
|
// 模板方法
|
||||||
void algorithm(){
|
void algorithm()
|
||||||
// 1.采图
|
{
|
||||||
|
// 1.采图
|
||||||
getImage();
|
getImage();
|
||||||
// 2.安全模式下加密和解密
|
// 2.安全模式下加密和解密
|
||||||
if (isSafeMode()){
|
if (isSafeMode())
|
||||||
// 2.1. 加密
|
{
|
||||||
|
// 2.1. 加密
|
||||||
encrypt();
|
encrypt();
|
||||||
// 2.2. 解密
|
// 2.2. 解密
|
||||||
decrypt();
|
decrypt();
|
||||||
}
|
}
|
||||||
// 3.处理Image
|
// 3.处理Image
|
||||||
processImage();
|
processImage();
|
||||||
// 4.处理结果
|
// 4.处理结果
|
||||||
output();
|
output();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// 派生类
|
// 派生类
|
||||||
class FingerprintModuleA :public FingerprintModule
|
class FingerprintModuleA : public FingerprintModule
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
FingerprintModuleA(){}
|
FingerprintModuleA() {}
|
||||||
void processImage(){
|
void processImage()
|
||||||
printf("使用 第一代版本算法 处理指纹图像\n");
|
{
|
||||||
|
printf("使用 第一代版本算法 处理指纹图像\n");
|
||||||
}
|
}
|
||||||
bool isSafeMode(){
|
bool isSafeMode()
|
||||||
printf("安全模式\n");
|
{
|
||||||
|
printf("安全模式\n");
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
void encrypt(){
|
void encrypt()
|
||||||
printf("使用RSA密钥加密\n");
|
{
|
||||||
|
printf("使用RSA密钥加密\n");
|
||||||
}
|
}
|
||||||
void decrypt(){
|
void decrypt()
|
||||||
printf("使用RSA密钥解密\n");
|
{
|
||||||
|
printf("使用RSA密钥解密\n");
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// 派生类
|
// 派生类
|
||||||
class FingerprintModuleB :public FingerprintModule
|
class FingerprintModuleB : public FingerprintModule
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
FingerprintModuleB(){}
|
FingerprintModuleB() {}
|
||||||
void processImage(){
|
void processImage()
|
||||||
printf("使用 第二代版本算法 处理指纹图像\n");
|
{
|
||||||
|
printf("使用 第二代版本算法 处理指纹图像\n");
|
||||||
}
|
}
|
||||||
bool isSafeMode(){
|
bool isSafeMode()
|
||||||
printf("非安全模式\n");
|
{
|
||||||
|
printf("非安全模式\n");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
void encrypt(){}
|
void encrypt() {}
|
||||||
void decrypt(){}
|
void decrypt() {}
|
||||||
};
|
};
|
||||||
|
|
||||||
// 派生类
|
// 派生类
|
||||||
class FingerprintModuleC :public FingerprintModule
|
class FingerprintModuleC : public FingerprintModule
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
FingerprintModuleC(){}
|
FingerprintModuleC() {}
|
||||||
void processImage(){
|
void processImage()
|
||||||
printf("使用 第一代版本算法 处理指纹图像\n");
|
{
|
||||||
|
printf("使用 第一代版本算法 处理指纹图像\n");
|
||||||
}
|
}
|
||||||
bool isSafeMode(){
|
bool isSafeMode()
|
||||||
printf("安全模式\n");
|
{
|
||||||
|
printf("安全模式\n");
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
void encrypt(){
|
void encrypt()
|
||||||
printf("使用DH密钥加密\n");
|
{
|
||||||
|
printf("使用DH密钥加密\n");
|
||||||
}
|
}
|
||||||
void decrypt(){
|
void decrypt()
|
||||||
printf("使用DH密钥解密\n");
|
{
|
||||||
|
printf("使用DH密钥解密\n");
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif //__FINGERPRINTMODULE_H__
|
#endif //__FINGERPRINTMODULE_H__
|
|
@ -1,61 +1,67 @@
|
||||||
#ifndef __DEMO_H__
|
#ifndef __DEMO_H__
|
||||||
#define __DEMO_H__
|
#define __DEMO_H__
|
||||||
|
|
||||||
// 抽象访问者 Visitor
|
// 抽象访问者 Visitor
|
||||||
class Visitor
|
class Visitor
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
virtual ~Visitor() {}
|
virtual ~Visitor() {}
|
||||||
virtual void visit(ConcreteElementA*) = 0;
|
virtual void visit(ConcreteElementA *) = 0;
|
||||||
virtual void visit(ConcreteElementB*) = 0;
|
virtual void visit(ConcreteElementB *) = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
// 具体访问者 ConcreteVisitor
|
// 具体访问者 ConcreteVisitor
|
||||||
class ConcreteVisitor :public Visitor
|
class ConcreteVisitor : public Visitor
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
// 实现一种针对特定元素的访问操作
|
// 实现一种针对特定元素的访问操作
|
||||||
void visit(ConcreteElementA*){
|
void visit(ConcreteElementA *)
|
||||||
// 元素A的访问操作代码
|
{
|
||||||
|
// 元素A的访问操作代码
|
||||||
}
|
}
|
||||||
void visit(ConcreteElementB*){
|
void visit(ConcreteElementB *)
|
||||||
// 元素B的访问操作代码
|
{
|
||||||
|
// 元素A的访问操作代码
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// 抽象元素
|
// 抽象元素
|
||||||
class Element
|
class Element
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
virtual ~Element() {}
|
virtual ~Element() {}
|
||||||
// 声明抽象方法,以一个抽象访问者的指针作为函数参数
|
// 声明抽象方法,以一个抽象访问者的指针作为函数参数
|
||||||
virtual void accept(Visitor*) = 0;
|
virtual void accept(Visitor *) = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
// 具体元素
|
// 具体元素
|
||||||
class ConcreteElement :public Element
|
class ConcreteElement : public Element
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
void accept(Visitor* visitor){
|
void accept(Visitor *visitor)
|
||||||
|
{
|
||||||
visitor->visit(this);
|
visitor->visit(this);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// 对象结构
|
// 对象结构
|
||||||
class ObjectStructure
|
class ObjectStructure
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
// 提供接口接受访问者访问
|
// 提供接口接受访问者访问
|
||||||
void accept(Visitor* visitor){
|
void accept(Visitor *visitor)
|
||||||
// 遍历访问对象结构中的元素
|
{
|
||||||
for (){
|
// 遍历访问对象结构中的元素
|
||||||
|
for ()
|
||||||
|
{
|
||||||
elementList[i]->accept(visitor);
|
elementList[i]->accept(visitor);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
void addElement(){}
|
void addElement() {}
|
||||||
void removeElement(){}
|
void removeElement() {}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
lsit<Element*>elementList;
|
list<Element *> elementList;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
|
@ -5,7 +5,7 @@
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
// 抽象元素
|
// 抽象元素
|
||||||
class Element
|
class Element
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
@ -36,7 +36,7 @@ private:
|
||||||
string name;
|
string name;
|
||||||
};
|
};
|
||||||
|
|
||||||
// 具体元素:Apple
|
// 具体元素:Apple
|
||||||
class Apple :public Element
|
class Apple :public Element
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
@ -45,7 +45,7 @@ public:
|
||||||
void accept(Visitor*);
|
void accept(Visitor*);
|
||||||
};
|
};
|
||||||
|
|
||||||
// 具体元素:Book
|
// 具体元素:Book
|
||||||
class Book :public Element
|
class Book :public Element
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
|
|
@ -10,7 +10,7 @@ class ShoppingCart
|
||||||
public:
|
public:
|
||||||
ShoppingCart(){}
|
ShoppingCart(){}
|
||||||
void addElement(Element* element){
|
void addElement(Element* element){
|
||||||
printf(" 商品名:%s, \t数量:%d, \t加入购物车成功!\n", element->getName().c_str(), element->getNum());
|
printf(" 商品名:%s, \t数量:%d, \t加入购物车成功!\n", element->getName().c_str(), element->getNum());
|
||||||
elementList.push_back(element);
|
elementList.push_back(element);
|
||||||
}
|
}
|
||||||
void accept(Visitor* visitor){
|
void accept(Visitor* visitor){
|
||||||
|
|
|
@ -4,23 +4,23 @@
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
// 前向声明
|
// 前向声明
|
||||||
class Element;
|
class Element;
|
||||||
class Apple;
|
class Apple;
|
||||||
class Book;
|
class Book;
|
||||||
|
|
||||||
// 抽象访问者
|
// 抽象访问者
|
||||||
class Visitor
|
class Visitor
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
Visitor(){};
|
Visitor(){};
|
||||||
virtual ~Visitor(){}
|
virtual ~Visitor(){}
|
||||||
// 声明一组访问方法
|
// 声明一组访问方法
|
||||||
virtual void visit(Apple*) = 0;
|
virtual void visit(Apple*) = 0;
|
||||||
virtual void visit(Book*) = 0;
|
virtual void visit(Book*) = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
// 具体访问者:顾客
|
// 具体访问者:顾客
|
||||||
class Customer :public Visitor
|
class Customer :public Visitor
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
@ -34,7 +34,7 @@ private:
|
||||||
string name;
|
string name;
|
||||||
};
|
};
|
||||||
|
|
||||||
// 具体访问者:收银员
|
// 具体访问者:Cashier
|
||||||
class Cashier :public Visitor
|
class Cashier :public Visitor
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
|
|
@ -5,19 +5,19 @@
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
Apple *apple1 = new Apple("븐말却틥벎", 7);
|
Apple *apple1 = new Apple("红富士苹果", 7);
|
||||||
Apple *apple2 = new Apple("빻큇틥벎", 5);
|
Apple *apple2 = new Apple("花牛苹果", 5);
|
||||||
Book *book1 = new Book("븐짜촘", 129);
|
Book *book1 = new Book("红楼梦", 129);
|
||||||
Book *book2 = new Book("老써諒", 49);
|
Book *book2 = new Book("终结者", 49);
|
||||||
|
|
||||||
Cashier* cashier = new Cashier();
|
Cashier *cashier = new Cashier();
|
||||||
Customer* jungle = new Customer("Jungle");
|
Customer *jungle = new Customer("Jungle");
|
||||||
jungle->setNum(apple1, 2);
|
jungle->setNum(apple1, 2);
|
||||||
jungle->setNum(apple2, 4);
|
jungle->setNum(apple2, 4);
|
||||||
jungle->setNum(book1, 1);
|
jungle->setNum(book1, 1);
|
||||||
jungle->setNum(book2, 3);
|
jungle->setNum(book2, 3);
|
||||||
|
|
||||||
ShoppingCart* shoppingCart = new ShoppingCart();
|
ShoppingCart *shoppingCart = new ShoppingCart();
|
||||||
shoppingCart->addElement(apple1);
|
shoppingCart->addElement(apple1);
|
||||||
shoppingCart->addElement(apple2);
|
shoppingCart->addElement(apple2);
|
||||||
shoppingCart->addElement(book1);
|
shoppingCart->addElement(book1);
|
||||||
|
|
|
@ -19,13 +19,13 @@ void Customer::setNum(Book* book, int iNum){
|
||||||
|
|
||||||
void Customer::visit(Apple* apple){
|
void Customer::visit(Apple* apple){
|
||||||
int price = apple->getPrice();
|
int price = apple->getPrice();
|
||||||
printf(" %s \t单价: \t%d 元/kg\n", apple->getName().c_str(), apple->getPrice());
|
printf(" %s \t单价: \t%d 元/kg\n", apple->getName().c_str(), apple->getPrice());
|
||||||
}
|
}
|
||||||
|
|
||||||
void Customer::visit(Book* book){
|
void Customer::visit(Book* book){
|
||||||
int price = book->getPrice();
|
int price = book->getPrice();
|
||||||
string name = book->getName();
|
string name = book->getName();
|
||||||
printf(" 《%s》\t单价: \t%d 元/本\n", book->getName().c_str(), book->getPrice());
|
printf(" 《%s》\t单价: \t%d 元/本\n", book->getName().c_str(), book->getPrice());
|
||||||
}
|
}
|
||||||
|
|
||||||
/***** Cashier *******/
|
/***** Cashier *******/
|
||||||
|
@ -38,7 +38,7 @@ void Cashier::visit(Apple* apple){
|
||||||
int price = apple->getPrice();
|
int price = apple->getPrice();
|
||||||
int num = apple->getNum();
|
int num = apple->getNum();
|
||||||
int total = price*num;
|
int total = price*num;
|
||||||
printf(" %s 总价: %d 元\n", name.c_str(), total);
|
printf(" %s 总价: %d 元\n", name.c_str(), total);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Cashier::visit(Book* book){
|
void Cashier::visit(Book* book){
|
||||||
|
@ -46,5 +46,5 @@ void Cashier::visit(Book* book){
|
||||||
string name = book->getName();
|
string name = book->getName();
|
||||||
int num = book->getNum();
|
int num = book->getNum();
|
||||||
int total = price*num;
|
int total = price*num;
|
||||||
printf(" 《%s》 总价: %d 元\n", name.c_str(), total);
|
printf(" 《%s》 总价: %d 元\n", name.c_str(), total);
|
||||||
}
|
}
|
Loading…
Reference in New Issue