bugfix: change character code : utf-8

master
FengJungle 2021-10-28 23:15:51 +08:00
parent 4d8d1ca606
commit 6ca04a5438
43 changed files with 452 additions and 394 deletions

5
.vscode/settings.json vendored Normal file
View File

@ -0,0 +1,5 @@
{
"files.associations": {
"list": "cpp"
}
}

View File

@ -5,7 +5,7 @@
#include <string.h>
using namespace std;
//抽象产品类AbstractProduct
// 抽象产品类AbstractProduct
class AbstractSportProduct
{
public:
@ -13,12 +13,12 @@ public:
}
virtual ~AbstractSportProduct(){}
//抽象方法:
// 抽象方法:
virtual void printName(){};
virtual void play(){};
};
//具体产品类Basketball
// 具体产品类Basketball
class Basketball :public AbstractSportProduct
{
public:
@ -26,7 +26,7 @@ public:
printName();
play();
}
//具体实现方法
// 具体实现方法
void printName(){
printf("Jungle get Basketball\n");
}
@ -35,7 +35,7 @@ public:
}
};
//具体产品类Football
// 具体产品类Football
class Football :public AbstractSportProduct
{
public:
@ -43,7 +43,7 @@ public:
printName();
play();
}
//具体实现方法
// 具体实现方法
void printName(){
printf("Jungle get Football\n");
}
@ -52,7 +52,7 @@ public:
}
};
//具体产品类Volleyball
// 具体产品类Volleyball
class Volleyball :public AbstractSportProduct
{
public:
@ -60,7 +60,7 @@ public:
printName();
play();
}
//具体实现方法
// 具体实现方法
void printName(){
printf("Jungle get Volleyball\n");
}
@ -69,7 +69,7 @@ public:
}
};
//抽象工厂类
// 抽象工厂类
class AbstractFactory
{
public:
@ -77,7 +77,7 @@ public:
virtual AbstractSportProduct *getSportProduct() = 0;
};
//具体工厂类BasketballFactory
// 具体工厂类BasketballFactory
class BasketballFactory :public AbstractFactory
{
public:
@ -90,7 +90,7 @@ public:
}
};
//具体工厂类FootballFactory
// 具体工厂类FootballFactory
class FootballFactory :public AbstractFactory
{
public:
@ -102,7 +102,7 @@ public:
}
};
//具体工厂类VolleyballFactory
// 具体工厂类VolleyballFactory
class VolleyballFactory :public AbstractFactory
{
public:

View File

@ -4,10 +4,10 @@
int main()
{
printf("工厂方法模式\n");
//定义工厂类对象和产品类对象
printf("工厂方法模式\n");
// 定义工厂类对象和产品类对象
std::shared_ptr<AbstractFactory> fac = make_shared<BasketballFactory>();
std::shared_ptr<AbstractSportProduct> product = std::shared_ptr<AbstractSportProduct>(fac->getSportProduct());

View File

@ -5,81 +5,89 @@
#include <string.h>
using namespace std;
//抽象产品类AbstractBall
// 抽象产品类AbstractBall
class AbstractBall
{
public:
AbstractBall(){
AbstractBall()
{
}
virtual ~AbstractBall(){}
//抽象方法:
virtual ~AbstractBall() {}
// 抽象方法:
virtual void play(){};
};
//具体产品类Basketball
class Basketball :public AbstractBall
// 具体产品类Basketball
class Basketball : public AbstractBall
{
public:
Basketball(){
Basketball()
{
play();
}
//具体实现方法
void play(){
// 具体实现方法
void play()
{
printf("Jungle play Basketball\n\n");
}
};
//具体产品类Football
class Football :public AbstractBall
// 具体产品类Football
class Football : public AbstractBall
{
public:
Football(){
Football()
{
play();
}
//具体实现方法
void play(){
// 具体实现方法
void play()
{
printf("Jungle play Football\n\n");
}
};
//抽象产品类AbstractShirt
// 抽象产品类AbstractShirt
class AbstractShirt
{
public:
AbstractShirt(){}
virtual ~AbstractShirt(){}
//抽象方法:
AbstractShirt() {}
virtual ~AbstractShirt() {}
// 抽象方法:
virtual void wearShirt(){};
};
//具体产品类BasketballShirt
class BasketballShirt :public AbstractShirt
// 具体产品类BasketballShirt
class BasketballShirt : public AbstractShirt
{
public:
BasketballShirt(){
BasketballShirt()
{
wearShirt();
}
//具体实现方法
void wearShirt(){
// 具体实现方法
void wearShirt()
{
printf("Jungle wear Basketball Shirt\n\n");
}
};
//具体产品类FootballShirt
class FootballShirt :public AbstractShirt
// 具体产品类FootballShirt
class FootballShirt : public AbstractShirt
{
public:
FootballShirt(){
FootballShirt()
{
wearShirt();
}
//具体实现方法
void wearShirt(){
// 具体实现方法
void wearShirt()
{
printf("Jungle wear Football Shirt\n\n");
}
};
//抽象工厂类
// 抽象工厂类
class AbstractFactory
{
public:
@ -88,35 +96,41 @@ public:
virtual AbstractShirt *getShirt() = 0;
};
//具体工厂类BasketballFactory
class BasketballFactory :public AbstractFactory
// 具体工厂类BasketballFactory
class BasketballFactory : public AbstractFactory
{
public:
BasketballFactory(){
BasketballFactory()
{
printf("BasketballFactory\n");
}
AbstractBall *getBall(){
AbstractBall *getBall()
{
printf("Jungle get basketball\n");
return new Basketball();
}
AbstractShirt *getShirt(){
AbstractShirt *getShirt()
{
printf("Jungle get basketball shirt\n");
return new BasketballShirt();
}
};
//具体工厂类BasketballFactory
class FootballFactory :public AbstractFactory
// 具体工厂类BasketballFactory
class FootballFactory : public AbstractFactory
{
public:
FootballFactory(){
FootballFactory()
{
printf("FootballFactory\n");
}
AbstractBall *getBall(){
AbstractBall *getBall()
{
printf("Jungle get football\n");
return new Football();
}
AbstractShirt *getShirt(){
AbstractShirt *getShirt()
{
printf("Jungle get football shirt\n");
return new FootballShirt();
}

View File

@ -5,7 +5,7 @@
#include <string.h>
using namespace std;
//<EFBFBD><EFBFBD>Ʒ<EFBFBD><EFBFBD>House
// 产品类House
class House
{
public:
@ -19,7 +19,7 @@ public:
void setRoof(string iRoof) {
this->roof = iRoof;
}
//<EFBFBD><EFBFBD>ӡHouse<EFBFBD><EFBFBD>Ϣ
// 打印House信息
void printfHouseInfo() {
printf("Floor:%s\t\n", this->floor.c_str());
printf("Wall:%s\t\n", this->wall.c_str());
@ -31,7 +31,7 @@ private:
string roof;
};
//<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>AbstractBall
// 抽象建造者AbstractBall
class AbstractBuilder
{
public:
@ -46,7 +46,7 @@ public:
house = nullptr;
}
}
//<EFBFBD><EFBFBD><EFBFBD>󷽷<EFBFBD><EFBFBD><EFBFBD>
// 抽象方法:
virtual void buildFloor() = 0;
virtual void buildWall() = 0;
virtual void buildRoof() = 0;
@ -55,7 +55,7 @@ public:
House *house;
};
//<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ConcreteBuilderA
// 具体建造者ConcreteBuilderA
class ConcreteBuilderA :public AbstractBuilder
{
public:
@ -70,7 +70,7 @@ public:
house = nullptr;
}
}
//<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʵ<EFBFBD>ַ<EFBFBD><EFBFBD><EFBFBD>
// 具体实现方法
void buildFloor() {
this->house->setFloor("Floor_A");
}
@ -85,7 +85,7 @@ public:
}
};
//<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ConcreteBuilderB
// 具体建造者ConcreteBuilderB
class ConcreteBuilderB :public AbstractBuilder
{
public:
@ -100,7 +100,7 @@ public:
house = nullptr;
}
}
//<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʵ<EFBFBD>ַ<EFBFBD><EFBFBD><EFBFBD>
// 具体实现方法
void buildFloor() {
this->house->setFloor("Floor_B");
}
@ -115,7 +115,7 @@ public:
}
};
//ָ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Director
// 指挥者Director
class Director
{
public:
@ -128,10 +128,11 @@ public:
builder = nullptr;
}
}
//<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʵ<EFBFBD>ַ<EFBFBD><EFBFBD><EFBFBD>
// 具体实现方法
void setBuilder(AbstractBuilder *iBuilder) {
this->builder = iBuilder;
}
// 封装组装流程,返回建造结果
House *construct() {
builder->buildFloor();
builder->buildWall();

View File

@ -2,21 +2,21 @@
int main()
{
//녜蹶쉔芚諒
// 抽象建造者
AbstractBuilder *builder;
//寧뿐諒
// 指挥者
Director *director = new Director();
//끓틔House
// 产品House
House *house;
//寧땍야竟쉔芚諒A
// 指定具体建造者A
builder = new ConcreteBuilderA();
director->setBuilder(builder);
house = director->construct();
house->printfHouseInfo();
delete builder;
//寧땍야竟쉔芚諒B
// 指定具体建造者A
builder = new ConcreteBuilderB();
director->setBuilder(builder);
house = director->construct();

View File

@ -5,7 +5,7 @@
#include <string.h>
using namespace std;
//work model类
// work model类
class WorkModel
{
public:
@ -15,7 +15,7 @@ public:
}
};
//抽象原型类PrototypeWork
// 抽象原型类PrototypeWork
class PrototypeWork
{
public:
@ -27,7 +27,7 @@ private:
};
//具体原型类ConcreteWork
// 抽象原型类PrototypeWork
class ConcreteWork :public PrototypeWork
{
public:
@ -56,7 +56,7 @@ public:
void setModel(WorkModel *iWorkModel){
this->workModel = iWorkModel;
}
//打印work信息
// 打印work信息
void printWorkInfo(){
printf("name:%s\t\n", this->name);
printf("idNum:%d\t\n", this->idNum);

View File

@ -4,47 +4,47 @@ int main()
{
#if 0
ConcreteWork *singleWork = new ConcreteWork("Single",1001,"Single_Model");
printf("\nSingle的作业\n");
printf("\nSingle的作业\n");
singleWork->printWorkInfo();
printf("\njungle直接抄作业……\n");
printf("\njungle直接抄作业……\n");
ConcreteWork *jungleWork = singleWork;
printf("\nJungle的作业\n");
printf("\nJungle的作业\n");
jungleWork->printWorkInfo();
//抄完改名字和学号,否则会被老师查出来
printf("\njungle抄完改名字和学号否则会被老师查出来……\n");
// 抄完改名字和学号,否则会被老师查出来
printf("\njungle抄完改名字和学号否则会被老师查出来……\n");
jungleWork->setName("jungle");
jungleWork->setIdNum(1002);
WorkModel *jungleModel = new WorkModel();
jungleModel->setWorkModelName("Jungle_Model");
jungleWork->setModel(jungleModel);
//检查下是否改对了
printf("\nSingle的作业\n");
// 检查下是否改对了
printf("\nSingle的作业\n");
singleWork->printWorkInfo();
printf("\nJungle的作业\n");
printf("\nJungle的作业\n");
jungleWork->printWorkInfo();
#endif
ConcreteWork *singleWork = new ConcreteWork("Single", 1001, "Single_Model");
printf("\nSingle的作业\n");
printf("\nSingle的作业\n");
ConcreteWork *jungleWork = singleWork->clone();
printf("\nJungle的作业\n");
printf("\njungle直接抄作业……\n");
//抄完改名字和学号,否则会被老师查出来
printf("\njungle抄完改名字和学号否则会被老师查出来……\n");
// 抄完改名字和学号,否则会被老师查出来
printf("\njungle抄完改名字和学号否则会被老师查出来……\n");
jungleWork->setName("jungle");
jungleWork->setIdNum(1002);
WorkModel *jungleModel = new WorkModel();
jungleModel->setWorkModelName("Jungle_Model");
jungleWork->setModel(jungleModel);
//检查下是否改对了
printf("\nSingle的作业\n");
// 检查下是否改对了
printf("\nSingle的作业\n");
singleWork->printWorkInfo();
printf("\nJungle的作业\n");
printf("\nJungle的作业\n");
jungleWork->printWorkInfo();
system("pause");

View File

@ -1,7 +1,7 @@
#ifndef __DECORATOR_PATTERN_H__
#define __DECORATOR_PATTERN_H__
//抽象构件
// 抽象构件
class Component
{
public:
@ -10,17 +10,17 @@ public:
virtual void operation() = 0;
};
//具体构件
// 具体构件
class Phone :public Component
{
public:
Phone(){}
void operation(){
printf("手机\n");
printf("<EFBFBD>ֻ<EFBFBD>\n");
}
};
//抽象装饰类
// 抽象装饰类
class Decorator :public Component
{
public:
@ -41,7 +41,7 @@ private:
Component *component;
};
//具体装饰类:手机壳
// 具体装饰类:手机壳
class DecoratorShell:public Decorator
{
public:
@ -54,11 +54,12 @@ public:
this->newBehavior();
}
void newBehavior(){
printf("装手机壳\n");
printf("装手机壳\n");
}
};
//具体装饰类:手机贴纸
// 具体装饰类:手机贴纸
class DecoratorSticker :public Decorator
{
public:
@ -71,11 +72,11 @@ public:
this->newBehavior();
}
void newBehavior(){
printf("贴卡通贴纸\n");
printf("贴卡通贴纸ֽ\n");
}
};
//具体装饰类:手机挂绳
// 具体装饰类:挂绳
class DecoratorRope :public Decorator
{
public:
@ -88,7 +89,7 @@ public:
this->newBehavior();
}
void newBehavior(){
printf("系手机挂绳\n");
printf("系手机挂绳\n");
}
};
#endif //__DECORATOR_PATTERN_H__

View File

@ -5,7 +5,7 @@
#include <vector>
using namespace std;
// 抽象享元类
// 抽象享元类
class NetDevice
{
public:
@ -21,27 +21,27 @@ public:
}
};
// 具体享元类:集线器
// 具体享元类:集线器
class Hub :public NetDevice
{
public:
Hub(){}
string getName(){
return "集线器";
return "集线器";
}
};
// 具体享元类:交换机
// 具体享元类:交换机
class Switch :public NetDevice
{
public:
Switch(){}
string getName(){
return "交换机";
return "交换机";
}
};
// 享元工厂类
// 享元工厂类
class NetDeviceFactory
{
public:
@ -58,7 +58,7 @@ public:
return NULL;
}
// 单例模式:返回享元工厂类的唯一实例
// 单例模式:返回享元工厂类的唯一实例
static NetDeviceFactory* getFactory(){
if (instance == NULL){
m_mutex.lock();
@ -80,7 +80,7 @@ private:
static NetDeviceFactory* instance;
static std::mutex m_mutex;
// 共享池用一个vector来表示
// 共享池用一个vector来表示
vector<NetDevice*> devicePool;
};

View File

@ -7,25 +7,25 @@ int main()
NetDevice *device1, *device2, *device3, *device4;
// 客户端2获取一个hub
// 客户端1获取一个hub
device1 = factory->getNetDevice('H');
device1->print(1);
// 客户端2获取一个hub
// 客户端2获取一个hub
device2 = factory->getNetDevice('H');
device2->print(2);
// 判断两个hub是否是同一个
printf("判断两个hub是否是同一个:\n");
// 判断两个hub是否是同一个
printf("判断两个hub是否是同一个:\n");
printf("device1:%p\ndevice2:%p\n", device1, device2);
printf("\n\n\n\n");
// 客户端3获取一个switch
// 客户端3获取一个switch
device3 = factory->getNetDevice('S');
device3->print(1);
// 客户端4获取一个hub
// 客户端4获取一个switch
device4 = factory->getNetDevice('S');
device4->print(2);
// 判断两个hub是否是同一个
printf("判断两个switch是否是同一个:\n");
// 判断两个switch是否是同一个
printf("判断两个switch是否是同一个:\n");
printf("device3:%p\ndevice4:%p\n", device3, device4);
printf("\n\n");

View File

@ -5,7 +5,7 @@
#include <time.h>
using namespace std;
// 请求:票据
// 请求:票据
class Bill
{
public:
@ -28,7 +28,8 @@ private:
string name;
double account;
};
// 抽象处理者
// 抽象处理者
class Approver
{
public:
@ -37,11 +38,11 @@ public:
setName(iName);
}
virtual ~Approver(){}
// 添加上级
// 添加上级
void setSuperior(Approver *iSuperior){
this->superior = iSuperior;
}
// 处理请求
// 处理请求
virtual void handleRequest(Bill*) = 0;
string getName(){
return name;
@ -55,7 +56,7 @@ private:
string name;
};
// 具体处理者:组长
// 具体处理者:组长
class GroupLeader :public Approver
{
public:
@ -63,20 +64,20 @@ public:
GroupLeader(string iName){
setName(iName);
}
// 处理请求
// 处理请求
void handleRequest(Bill *bill){
if (bill->getAccount() < 10){
printf("组长 %s 处理了该票据,票据信息:",this->getName().c_str());
printf("组长 %s 处理了该票据,票据信息:",this->getName().c_str());
bill->print();
}
else{
printf("组长无权处理,转交上级……\n");
printf("组长无权处理,转交上级……\n");
this->superior->handleRequest(bill);
}
}
};
// 具体处理者:主管
// 具体处理者:主管
class Head :public Approver
{
public:
@ -84,20 +85,20 @@ public:
Head(string iName){
setName(iName);
}
// 处理请求
// 处理请求
void handleRequest(Bill *bill){
if (bill->getAccount() >= 10 && bill->getAccount()<30){
printf("主管 %s 处理了该票据,票据信息:", this->getName().c_str());
printf("主管 %s 处理了该票据,票据信息:", this->getName().c_str());
bill->print();
}
else{
printf("主管无权处理,转交上级……\n");
printf("主管无权处理,转交上级……\n");
this->superior->handleRequest(bill);
}
}
};
// 具体处理者:经理
// 具体处理者:经理
class Manager :public Approver
{
public:
@ -105,20 +106,20 @@ public:
Manager(string iName){
setName(iName);
}
// 处理请求
// 处理请求
void handleRequest(Bill *bill){
if (bill->getAccount() >= 30 && bill->getAccount()<60){
printf("经理 %s 处理了该票据,票据信息:", this->getName().c_str());
printf("经理 %s 处理了该票据,票据信息:", this->getName().c_str());
bill->print();
}
else{
printf("经理无权处理,转交上级……\n");
printf("经理无权处理,转交上级……\n");
this->superior->handleRequest(bill);
}
}
};
// 具体处理者:老板
// 具体处理者:老板
class Boss :public Approver
{
public:
@ -126,9 +127,9 @@ public:
Boss(string iName){
setName(iName);
}
// 处理请求
// 处理请求
void handleRequest(Bill *bill){
printf("老板 %s 处理了该票据,票据信息:", this->getName().c_str());
printf("老板 %s 处理了该票据,票据信息:", this->getName().c_str());
bill->print();
}
};

View File

@ -3,25 +3,25 @@
int main()
{
// 请求处理者:组长,兵哥,春总,老板
// 请求处理者:组长,兵哥,春总,老板
Approver *zuzhang, *bingge, *chunzong, *laoban;
zuzhang = new GroupLeader("孙大哥");
bingge = new Head("兵哥");
chunzong = new Manager("春总");
laoban = new Boss("张老板");
zuzhang = new GroupLeader("孙大哥");
bingge = new Head("兵哥");
chunzong = new Manager("春总");
laoban = new Boss("张老板");
zuzhang->setSuperior(bingge);
bingge->setSuperior(chunzong);
chunzong->setSuperior(laoban);
// 创建报销单
// 创建报销单
Bill *bill1 = new Bill(1, "Jungle", 8);
Bill *bill2 = new Bill(2, "Lucy", 14.4);
Bill *bill3 = new Bill(3, "Jack", 32.9);
Bill *bill4 = new Bill(4, "Tom", 89);
// 全部先交给组长审批
// 全部先交给组长审批
zuzhang->handleRequest(bill1); printf("\n");
zuzhang->handleRequest(bill2); printf("\n");
zuzhang->handleRequest(bill3); printf("\n");

View File

@ -3,11 +3,11 @@
int main()
{
// 实例化调用者:按钮
// 实例化调用者:按钮
Button *button = new Button();
Command *lampCmd, *fanCmd;
// 按钮控制电灯
// 按钮控制电灯
lampCmd = new LampCommand();
button->setCommand(lampCmd);
button->touch();
@ -16,7 +16,7 @@ int main()
printf("\n\n");
// 按钮控制风扇
// 按钮控制风扇
fanCmd = new FanCommand();
button->setCommand(fanCmd);
button->touch();
@ -30,11 +30,9 @@ int main()
Command *lampCmd2, *fanCmd2;
CommandQueue *cmdQueue = new CommandQueue();
// 按钮控制电灯
lampCmd2 = new LampCommand();
cmdQueue->addCommand(lampCmd2);
// 按钮控制风扇
fanCmd2 = new FanCommand();
cmdQueue->addCommand(fanCmd2);

View File

@ -7,17 +7,17 @@
#include <string.h>
using namespace std;
// 抽象表达式类
// 抽象表达式类
class AbstractNode
{
public:
AbstractNode(){}
virtual ~AbstractNode(){}
// 声明抽象接口
// 声明抽象接口
virtual char interpret() = 0;
};
// 终结符表达式ValueNode
// 终结符表达式ValueNode
class ValueNode :public AbstractNode
{
public :
@ -25,7 +25,7 @@ public :
ValueNode(int iValue){
this->value = iValue;
}
// 实现解释操作
// 实现解释操作
char interpret(){
return value;
}
@ -33,7 +33,7 @@ private:
int value;
};
// 终结符表达式OperationNode
// 终结符表达式OperationNode
class OperatorNode :public AbstractNode
{
public:
@ -41,7 +41,7 @@ public:
OperatorNode(string iOp){
this->op = iOp;
}
// 实现解释操作
// 实现解释操作
char interpret(){
if (op == "and"){
return '&';
@ -55,7 +55,7 @@ private:
string op;
};
// 非终结符表达式SentenceNode
// 非终结符表达式SentenceNode
class SentenceNode :public AbstractNode
{
public:
@ -81,7 +81,7 @@ private:
AbstractNode *operatorNode;
};
// 处理者
// 处理者
class Handler
{
public:

View File

@ -17,5 +17,5 @@ int Television::getTotalChannelNum(){
}
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());
}

View File

@ -2,13 +2,14 @@
#define __AGGREGATE_H__
#include <vector>
#include <string>
using namespace std;
// 前向声明,因为两个类互相引用
// 前向声明,因为两个类互相引用
class Iterator;
class RemoteControl;
// 抽象聚合类 Aggregate
// 抽象聚合类 Aggregate
class Aggregate
{
public:
@ -17,19 +18,19 @@ public:
virtual Iterator* createIterator() = 0;
};
// 具体聚合类 Television
// 具体聚合类 Television
class Television :public Aggregate
{
public:
Television();
Television(vector<string> iChannelList);
// 实现创建迭代器
Television(vector<std::string> iChannelList);
// 实现创建迭代器
Iterator* createIterator();
// 获取总的频道数目
// 获取总的频道数目
int getTotalChannelNum();
void play(int i);
private:
vector<string> channelList;
vector<std::string> channelList;
};
#endif //__AGGREGATE_H__

View File

@ -7,13 +7,13 @@
#include <vector>
using namespace std;
// 抽象迭代器
// 抽象迭代器
class Iterator
{
public:
Iterator(){}
virtual ~Iterator(){}
// 声明抽象遍历方法
// 声明抽象遍历方法
virtual void first() = 0;
virtual void last() = 0;
virtual void next() = 0;
@ -25,7 +25,7 @@ private:
};
// 遥控器:具体迭代器
// 遥控器:具体迭代器
class RemoteControl :public Iterator
{
public:
@ -35,7 +35,7 @@ public:
cursor = -1;
totalNum = tv->getTotalChannelNum();
}
// 实现各个遍历方法
// 实现各个遍历方法
void first(){
cursor = 0;
}
@ -58,11 +58,11 @@ public:
tv->play(cursor);
}
private:
// 游标
// 游标
int cursor;
// 总的频道数目
// 总的频道数目
int totalNum;
// 电视
// 电视
Television* tv;
};

View File

@ -3,16 +3,16 @@
int main()
{
vector<string> channelList = { "新闻频道", "财经频道", "体育频道", "电影频道", "音乐频道", "农业频道", "四川卫视", "成都卫视" };
// 创建电视
vector<string> channelList = { "新闻频道", "财经频道", "体育频道", "电影频道", "音乐频道", "农业频道", "四川卫视", "成都卫视" };
// 创建电视
Television *tv = new Television(channelList);
// 创建遥控器
// 创建遥控器
Iterator *remoteControl = tv->createIterator();
// 顺序遍历
printf("顺序遍历:\n");
// 顺序遍历
printf("顺序遍历:\n");
remoteControl->first();
// 遍历电视所有频道
// 遍历电视所有频道
while (remoteControl->hasNext()){
remoteControl->currentChannel();
remoteControl->next();
@ -20,10 +20,10 @@ int main()
printf("\n\n");
// 逆序遍历
printf("逆序遍历:\n");
// 逆序遍历
printf("<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>:\n");
remoteControl->last();
// 遍历电视所有频道
// 遍历电视所有频道
while (remoteControl->hasPrevious()){
remoteControl->currentChannel();
remoteControl->previous();

View File

@ -4,11 +4,11 @@
#include "common.h"
using namespace std;
// 前向声明
// 前向声明
class Mediator;
class Agency;
// 抽象同事类
// 抽象同事类
class Colleague
{
public:
@ -33,7 +33,7 @@ private:
Mediator* mediator;
};
// 具体同事类:房东
// 具体同事类:房东
class Landlord :public Colleague
{
public:
@ -48,7 +48,7 @@ private:
string phoneNumber;
};
// 具体同事类:租客
// 具体同事类:租客
class Tenant :public Colleague
{
public:

View File

@ -19,11 +19,11 @@ Landlord::Landlord(string iName, int iPrice,
}
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());
}
void Landlord::ask(){
printf("房东%s查看租客信息\n",name.c_str());
printf("房东%s查看租客信息\n",name.c_str());
(this->getMediator())->operation(this);
}

View File

@ -4,19 +4,19 @@
#include "common.h"
#include "Colleague.h"
// 抽象中介者
// 抽象中介者
class Mediator
{
public:
Mediator(){}
virtual ~Mediator(){}
// 声明抽象方法
// 声明抽象方法
virtual void operation(Colleague*) = 0;
// 声明注册方法
// 声明注册方法
virtual void registerMethod(Colleague*) = 0;
};
// 具体中介者
// 具体中介者
class Agency:public Mediator
{
public:

View File

@ -1,21 +1,25 @@
#include "Colleague.h"
#include "Mediator.h"
Tenant::Tenant(){
Tenant::Tenant()
{
name = "none";
setPersonType(NONE_PERSON);
}
Tenant::Tenant(string iName){
Tenant::Tenant(string iName)
{
name = iName;
setPersonType(TENANT);
}
void Tenant::ask(){
printf("租客%s询问房东信息\n", name.c_str());
void Tenant::ask()
{
printf("租客%s询问房东信息\n", name.c_str());
(this->getMediator())->operation(this);
}
void Tenant::answer(){
printf("租客姓名:%s\n", name.c_str());
void Tenant::answer()
{
printf("租客姓名:%s\n", name.c_str());
}

View File

@ -1,7 +1,7 @@
#ifndef __COMMON_H__
#define __COMMON_H__
// 公共头文件
// 公共头文件
#include <vector>
using namespace std;

View File

@ -4,27 +4,27 @@
int main()
{
// 创建租房中介
// 创建租房中介
Agency *mediator = new Agency();
// 创建3位房东
Landlord *fangdong1 = new Landlord("刘备", 1350, "成都市双流区", "1351025");
Landlord *fangdong2 = new Landlord("关羽", 1500, "成都市武侯区", "1378390");
Landlord *fangdong3 = new Landlord("张飞", 1000, "成都市龙泉驿", "1881166");
// 创建3位房东
Landlord *fangdong1 = new Landlord("刘备", 1350, "成都市双流区", "1351025");
Landlord *fangdong2 = new Landlord("关羽", 1500, "成都市武侯区", "1378390");
Landlord *fangdong3 = new Landlord("张飞", 1000, "成都市龙泉驿", "1881166");
fangdong1->setMediator(mediator);
fangdong2->setMediator(mediator);
fangdong3->setMediator(mediator);
// 房东在中介处登记注册房源信息
// 房东在中介处登记注册房源信息
mediator->registerMethod(fangdong1);
mediator->registerMethod(fangdong2);
mediator->registerMethod(fangdong3);
// 创建两位租客Jungle和贱萌兔
// 创建两位租客Jungle和贱萌兔
Tenant *jungle = new Tenant("Jungle");
Tenant *jianmengtu = new Tenant("贱萌兔");
Tenant *jianmengtu = new Tenant("<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>");
jungle->setMediator(mediator);
jianmengtu->setMediator(mediator);
// Jungle和贱萌兔在中介处登记求租信息
// Jungle和贱萌兔在中介处登记求租信息
mediator->registerMethod(jungle);
mediator->registerMethod(jianmengtu);

View File

@ -4,33 +4,34 @@
#include "common.h"
#include <vector>
// 前向声明
// 前向声明
class Observer;
class Player;
// 抽象目标:联盟中心
// 抽象目标:联盟中心
class AllyCenter
{
public:
AllyCenter();
virtual ~AllyCenter(){}
// 声明通知方法
virtual ~AllyCenter() {}
// 声明通知方法
virtual void notify(INFO_TYPE infoType, std::string name) = 0;
// 加入玩家
void join(Observer* player);
// 移除玩家
void remove(Observer* player);
// 加入玩家
void join(Observer *player);
// 移除玩家
void remove(Observer *player);
protected:
// 玩家列表
std::vector<Observer*>playerList;
// 玩家列表
std::vector<Observer *> playerList;
};
// 具体目标
class AllyCenterController :public AllyCenter
// 具体目标
class AllyCenterController : public AllyCenter
{
public:
AllyCenterController();
// 实现通知方法
// 实现通知方法
void notify(INFO_TYPE infoType, std::string name);
};

View File

@ -4,63 +4,69 @@
using namespace std;
#include <list>
// 抽象观察者
// 抽象观察者
class Observer
{
public:
virtual ~Observer() {}
// 声明响应更新方法
// 声明响应更新方法
virtual void update() = 0;
};
// 具体观察者
class ConcreteObserver:public Observer
// 具体观察者
class ConcreteObserver : public Observer
{
public:
// 实现响应更新方法
void update(){
// 具体操作
// 实现响应更新方法
void update()
{
// 具体操作
}
};
// 抽象目标
// 抽象目标
class Subject
{
public:
virtual ~Subject() {}
// 添加观察者
void attach(Observer* obs){
// 添加观察者
void attach(Observer *obs)
{
obsList.push_back(obs);
}
// 移除观察者
void detach(Observer* obs){
// 移除观察者
void detach(Observer *obs)
{
obsList.remove(obs);
}
// 声明通知方法
// 声明通知方法
virtual void notify() = 0;
protected:
// 观察者列表
list<Observer*>obsList;
// 观察者列表
list<Observer *> obsList;
};
// 具体目标
class ConcreteSubject :public Subject
// 具体目标
class ConcreteSubject : public Subject
{
public:
// 实现通知方法
void notify(){
// 具体操作
// 遍历通知观察者对象
for (int i = 0; i < obsList.size(); i++){
// 实现通知方法
void notify()
{
// 具体操作
// 遍历通知观察者对象
for (int i = 0; i < obsList.size(); i++)
{
obsList[i]->update();
}
}
};
// 客户端代码示例
// 客户端代码示例
int main()
{
Subject *sub = new ConcreteSubject();
Subject *sub = new ConcreteSubject();
Observer *obs = new ConcreteObserver();
sub->attach(obs);
sub->notify();

View File

@ -6,13 +6,13 @@ using namespace std;
#include "common.h"
#include "AllyCenter.h"
// 抽象观察者 Observer
// 抽象观察者 Observer
class Observer
{
public:
virtual ~Observer(){}
Observer(){}
// 声明抽象方法
// 声明抽象方法
virtual void call(INFO_TYPE infoType, AllyCenter* ac) = 0;
string getName(){
return name;
@ -24,7 +24,7 @@ private:
string name;
};
// 具体观察者
// 具体观察者
class Player :public Observer
{
public:
@ -34,26 +34,26 @@ public:
Player(string iName){
setName(iName);
}
// 实现
// 实现
void call(INFO_TYPE infoType, AllyCenter* ac){
switch (infoType){
case RESOURCE:
printf("%s :我这里有物资\n", getName().c_str());
printf("%s :我这里有物资\n", getName().c_str());
break;
case HELP:
printf("%s :救救我\n", getName().c_str());
printf("%s :救救我\n", getName().c_str());
break;
default:
printf("Nothing\n");
}
ac->notify(infoType, getName());
}
// 实现具体方法
// 实现具体方法
void help(){
printf("%s:坚持住,我来救你!\n", getName().c_str());
printf("%s:坚持住,我来救你!\n", getName().c_str());
}
void come(){
printf("%s:好的,我来取物资\n", getName().c_str());
printf("%s:好的,我来取物资\n", getName().c_str());
}
};

View File

@ -3,14 +3,14 @@
int main()
{
// 创建一个战队
AllyCenterController* controller = new AllyCenterController();
// 创建一个战队
AllyCenterController *controller = new AllyCenterController();
// 创建4个玩家并加入战队
Player* Jungle = new Player("Jungle");
Player* Single = new Player("Single");
Player* Jianmengtu = new Player("贱萌兔");
Player* SillyDog = new Player("傻子狗");
// 创建4个玩家并加入战队
Player *Jungle = new Player("Jungle");
Player *Single = new Player("Single");
Player *Jianmengtu = new Player("<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>");
Player *SillyDog = new Player("ɵ<EFBFBD>ӹ<EFBFBD>");
controller->join(Jungle);
controller->join(Single);
controller->join(Jianmengtu);
@ -18,12 +18,12 @@ int main()
printf("\n\n");
// Jungle发现物资呼叫队友
// Jungle发现物资呼叫队友
Jungle->call(RESOURCE, controller);
printf("\n\n");
// 傻子狗遇到危险,求救队友
// 傻子狗遇到危险,求救队友
SillyDog->call(HELP, controller);
printf("\n\n");

View File

@ -3,7 +3,7 @@
using namespace std;
#include <iostream>
// Η°ΟςΙωΓχ
// 前向声明
class Level;
class GameAccount

View File

@ -5,20 +5,26 @@
#define random(x) (rand()%x)
GameAccount::GameAccount(){
printf("创立游戏角色积分100级别PRIMARY\n");
level = nullptr;
printf("创立游戏角色积分100级别PRIMARY\n");
score = 100;
name = "none";
setLevel(new Primary(this));
}
GameAccount::GameAccount(string iName){
printf("创立游戏角色积分100级别PRIMARY\n");
level = nullptr;
printf("创立游戏角色积分100级别PRIMARY\n");
score = 100;
name = iName;
setLevel(new Primary(this));
}
void GameAccount::setLevel(Level* iLevel){
if(level != nullptr){
delete level;
level = nullptr;
}
this->level = iLevel;
}
@ -49,12 +55,12 @@ void GameAccount::win(){
else{
setScore(getScore() + 100);
}
printf("\n\t胜利,最新积分为 %d\n", score);
printf("\n\t胜利,最新积分为 %d\n", score);
}
void GameAccount::lose(){
setScore(getScore() + 30);
printf("\n\t输牌,最新积分为 %d\n", score);
printf("\n\t输牌,最新积分为 %d\n", score);
}
int GameAccount::getScore(){

View File

@ -13,7 +13,7 @@ void Level::playCard(){
}
void Level::play(){
printf("\t使用基本技能,");
printf("\t使用基本技能,");
}
void Level::setGameAccount(GameAccount* iGameAccount){
@ -52,7 +52,7 @@ void Primary::peekCards(){
void Primary::upgradeLevel(){
if (this->getGameAccount()->getScore() > 150){
this->getGameAccount()->setLevel(new Secondary(this));
printf("\t升级! 级别SECONDARY\n\n");
printf("\t升级! 级别SECONDARY\n\n");
}
else{
printf("\n");
@ -70,7 +70,7 @@ Secondary::Secondary(Level* level){
}
void Secondary::doubleScore(){
printf("使用胜利双倍积分技能");
printf("使用胜利双倍积分技能");
}
void Secondary::changeCards(){
@ -84,11 +84,11 @@ void Secondary::peekCards(){
void Secondary::upgradeLevel(){
if (this->getGameAccount()->getScore() < 150){
this->getGameAccount()->setLevel(new Primary(this));
printf("\t降级! 级别PRIMARY\n\n");
printf("\t降级! 级别PRIMARY\n\n");
}
else if (this->getGameAccount()->getScore() > 200){
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(){
printf("使用胜利双倍积分技能,");
printf("使用胜利双倍积分技能,");
}
void Professional::changeCards(){
printf("使用换牌技能");
printf("使用换牌技能");
}
void Professional::peekCards(){
@ -117,11 +117,11 @@ void Professional::peekCards(){
void Professional::upgradeLevel(){
if (this->getGameAccount()->getScore() < 200){
this->getGameAccount()->setLevel(new Secondary(this));
printf("\t降级! 级别SECONDARY\n\n");
printf("\t降级! 级别SECONDARY\n\n");
}
else if (this->getGameAccount()->getScore() > 250){
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(){
printf("使用胜利双倍积分技能,");
printf("使用胜利双倍积分技能,");
}
void Final::changeCards(){
printf("使用换牌技能,");
printf("使用换牌技能,");
}
void Final::peekCards(){
printf("使用偷看卡牌技能");
printf("使用偷看卡牌技能");
}
void Final::upgradeLevel(){
if (this->getGameAccount()->getScore() < 250){
this->getGameAccount()->setLevel(new Professional(this));
printf("\t降级! 级别PROFESSIONAL\n\n");
printf("\t降级! 级别PROFESSIONAL\n\n");
}
else{
printf("\t%s 已经是最高级\n\n", this->getGameAccount()->getName().c_str());
printf("\t%s 已经是最高级\n\n", this->getGameAccount()->getName().c_str());
}
}

View File

@ -8,13 +8,13 @@ class Level
public :
Level();
virtual ~Level(){}
// 声明方法
// 声明方法
void playCard();
void play();
virtual void doubleScore() = 0;
virtual void changeCards() = 0;
virtual void peekCards() = 0;
// 升级
// 升级
virtual void upgradeLevel() = 0;
GameAccount* getGameAccount();
void setGameAccount(GameAccount* iGameAccount);
@ -36,7 +36,7 @@ public:
void doubleScore();
void changeCards();
void peekCards();
// 升级
// 升级
void upgradeLevel();
};
@ -48,7 +48,7 @@ public:
void doubleScore();
void changeCards();
void peekCards();
// 升级
// 升级
void upgradeLevel();
};
@ -60,7 +60,7 @@ public:
void doubleScore();
void changeCards();
void peekCards();
// 升级
// 升级
void upgradeLevel();
};
@ -72,7 +72,7 @@ public:
void doubleScore();
void changeCards();
void peekCards();
// 升级
// 升级
void upgradeLevel();
};

View File

@ -4,7 +4,7 @@
#include "Strategy.h"
#include <stdio.h>
// 上下文类
// 上下文类
class Context
{
public:
@ -38,7 +38,7 @@ public:
}
void sort(){
this->sortStrategy->sort(arr, N);
printf("输出: ");
printf("输出: ");
this->print();
}
void setInput(int iArr[], int iN){

View File

@ -3,7 +3,7 @@
#include <stdio.h>
// 抽象策略类
// 抽象策略类
class Strategy
{
public:
@ -12,12 +12,12 @@ public:
virtual void sort(int arr[], int N) = 0;
};
// 具体策略:冒泡排序
// 具体策略:冒泡排序
class BubbleSort :public Strategy
{
public:
BubbleSort(){
printf("冒泡排序\n");
printf("冒泡排序\n");
}
void sort(int arr[], int N){
for (int i = 0; i<N; i++)
@ -34,12 +34,12 @@ public:
}
};
// 具体策略:选择排序
// 具体策略:选择排序
class SelectionSort :public Strategy
{
public:
SelectionSort(){
printf("选择排序\n");
printf("选择排序\n");
}
void sort(int arr[], int N){
int i, j, k;
@ -59,12 +59,12 @@ public:
}
};
// 具体策略:插入排序
// 具体策略:插入排序
class InsertSort :public Strategy
{
public:
InsertSort(){
printf("插入排序\n");
printf("插入排序\n");
}
void sort(int arr[], int N){
int i, j;

View File

@ -1,39 +1,39 @@
#ifndef __DEMO_H__
#define __DEMO_H__
// 抽象类(基类)
// 抽象类(基类)
class AbstractClass
{
public:
virtual ~AbstractClass(){}
// 模板方法,定义一个算法的框架流程
// 模板方法,定义一个算法的框架流程
void templateMethod(){
// do something
method1();
method2();
method3();
}
// 基本方法——公共方法
// 基本方法——公共方法
void mehtod1(){
// do something
}
// 基本方法2
// 基本方法2
virtual void method2() = 0;
// 基本方法3——默认实现
// 基本方法3——默认实现
void mehtod3(){
// do something
}
};
// 具体类(派生类)
// 具体类(派生类)
class ConcreteClass :public AbstractClass
{
public:
// 实现基本方法2
// 实现基本方法2
void method2(){
// do something
}
// 重定义基本方法3覆盖基类的方法3
// 重定义基本方法3覆盖基类的方法3
void method3(){
// do something
}

View File

@ -3,96 +3,110 @@
#include <stdio.h>
// 基类
// 基类
class FingerprintModule
{
public:
FingerprintModule(){}
virtual ~FingerprintModule(){}
void getImage(){
printf("采指纹图像\n");
FingerprintModule() {}
virtual ~FingerprintModule() {}
void getImage()
{
printf("采指纹图像\n");
}
void output(){
printf("指纹图像处理完成!\n\n");
void output()
{
printf("指纹图像处理完成!\n");
}
virtual bool isSafeMode() = 0;
virtual void processImage() = 0;
// 加解密
// 加解密
virtual void encrypt() = 0;
virtual void decrypt() = 0;
// 模板方法
void algorithm(){
// 1.采图
// 模板方法
void algorithm()
{
// 1.采图
getImage();
// 2.安全模式下加密和解密
if (isSafeMode()){
// 2.1. 加密
// 2.安全模式下加密和解密
if (isSafeMode())
{
// 2.1. 加密
encrypt();
// 2.2. 解密
// 2.2. 解密
decrypt();
}
// 3.处理Image
// 3.处理Image
processImage();
// 4.处理结果
// 4.处理结果
output();
}
};
// 派生类
class FingerprintModuleA :public FingerprintModule
// 派生类
class FingerprintModuleA : public FingerprintModule
{
public:
FingerprintModuleA(){}
void processImage(){
printf("使用 第一代版本算法 处理指纹图像\n");
FingerprintModuleA() {}
void processImage()
{
printf("使用 第一代版本算法 处理指纹图像\n");
}
bool isSafeMode(){
printf("安全模式\n");
bool isSafeMode()
{
printf("安全模式\n");
return true;
}
void encrypt(){
printf("使用RSA密钥加密\n");
void encrypt()
{
printf("使用RSA密钥加密\n");
}
void decrypt(){
printf("使用RSA密钥解密\n");
void decrypt()
{
printf("使用RSA密钥解密\n");
}
};
// 派生类
class FingerprintModuleB :public FingerprintModule
// 派生类
class FingerprintModuleB : public FingerprintModule
{
public:
FingerprintModuleB(){}
void processImage(){
printf("使用 第二代版本算法 处理指纹图像\n");
FingerprintModuleB() {}
void processImage()
{
printf("使用 第二代版本算法 处理指纹图像\n");
}
bool isSafeMode(){
printf("非安全模式\n");
bool isSafeMode()
{
printf("非安全模式\n");
return false;
}
void encrypt(){}
void decrypt(){}
void encrypt() {}
void decrypt() {}
};
// 派生类
class FingerprintModuleC :public FingerprintModule
// 派生类
class FingerprintModuleC : public FingerprintModule
{
public:
FingerprintModuleC(){}
void processImage(){
printf("使用 第一代版本算法 处理指纹图像\n");
FingerprintModuleC() {}
void processImage()
{
printf("使用 第一代版本算法 处理指纹图像\n");
}
bool isSafeMode(){
printf("安全模式\n");
bool isSafeMode()
{
printf("安全模式\n");
return true;
}
void encrypt(){
printf("使用DH密钥加密\n");
void encrypt()
{
printf("使用DH密钥加密\n");
}
void decrypt(){
printf("使用DH密钥解密\n");
void decrypt()
{
printf("使用DH密钥解密\n");
}
};
#endif //__FINGERPRINTMODULE_H__
#endif //__FINGERPRINTMODULE_H__

View File

@ -1,61 +1,67 @@
#ifndef __DEMO_H__
#define __DEMO_H__
// 抽象访问者 Visitor
// 抽象访问者 Visitor
class Visitor
{
public:
virtual ~Visitor() {}
virtual void visit(ConcreteElementA*) = 0;
virtual void visit(ConcreteElementB*) = 0;
virtual void visit(ConcreteElementA *) = 0;
virtual void visit(ConcreteElementB *) = 0;
};
// 具体访问者 ConcreteVisitor
class ConcreteVisitor :public Visitor
// 具体访问者 ConcreteVisitor
class ConcreteVisitor : public Visitor
{
public:
// 实现一种针对特定元素的访问操作
void visit(ConcreteElementA*){
// 元素A的访问操作代码
// 实现一种针对特定元素的访问操作
void visit(ConcreteElementA *)
{
// 元素A的访问操作代码
}
void visit(ConcreteElementB*){
// 元素B的访问操作代码
void visit(ConcreteElementB *)
{
// 元素A的访问操作代码
}
};
// 抽象元素
// 抽象元素
class Element
{
public:
virtual ~Element() {}
// 声明抽象方法,以一个抽象访问者的指针作为函数参数
virtual void accept(Visitor*) = 0;
// 声明抽象方法,以一个抽象访问者的指针作为函数参数
virtual void accept(Visitor *) = 0;
};
// 具体元素
class ConcreteElement :public Element
// 具体元素
class ConcreteElement : public Element
{
public:
void accept(Visitor* visitor){
void accept(Visitor *visitor)
{
visitor->visit(this);
}
};
// 对象结构
// 对象结构
class ObjectStructure
{
public:
// 提供接口接受访问者访问
void accept(Visitor* visitor){
// 遍历访问对象结构中的元素
for (){
// 提供接口接受访问者访问
void accept(Visitor *visitor)
{
// 遍历访问对象结构中的元素
for ()
{
elementList[i]->accept(visitor);
}
}
void addElement(){}
void removeElement(){}
void addElement() {}
void removeElement() {}
private:
lsit<Element*>elementList;
list<Element *> elementList;
};
#endif

View File

@ -5,7 +5,7 @@
#include <iostream>
using namespace std;
// 抽象元素
// 抽象元素
class Element
{
public:
@ -36,7 +36,7 @@ private:
string name;
};
// 具体元素Apple
// 具体元素Apple
class Apple :public Element
{
public:
@ -45,7 +45,7 @@ public:
void accept(Visitor*);
};
// 具体元素Book
// 具体元素Book
class Book :public Element
{
public:

View File

@ -10,7 +10,7 @@ class ShoppingCart
public:
ShoppingCart(){}
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);
}
void accept(Visitor* visitor){

View File

@ -4,23 +4,23 @@
#include <iostream>
using namespace std;
// 前向声明
// 前向声明
class Element;
class Apple;
class Book;
// 抽象访问者
// 抽象访问者
class Visitor
{
public:
Visitor(){};
virtual ~Visitor(){}
// 声明一组访问方法
// 声明一组访问方法
virtual void visit(Apple*) = 0;
virtual void visit(Book*) = 0;
};
// 具体访问者:顾客
// 具体访问者:顾客
class Customer :public Visitor
{
public:
@ -34,7 +34,7 @@ private:
string name;
};
// 具体访问者:收银员
// 具体访问者:Cashier
class Cashier :public Visitor
{
public:

View File

@ -5,19 +5,19 @@
int main()
{
Apple *apple1 = new Apple("븐말却틥벎", 7);
Apple *apple2 = new Apple("빻큇틥벎", 5);
Book *book1 = new Book("븐짜촘", 129);
Book *book2 = new Book("老써諒", 49);
Apple *apple1 = new Apple("红富士苹果", 7);
Apple *apple2 = new Apple("花牛苹果", 5);
Book *book1 = new Book("红楼梦", 129);
Book *book2 = new Book("终结者", 49);
Cashier* cashier = new Cashier();
Customer* jungle = new Customer("Jungle");
Cashier *cashier = new Cashier();
Customer *jungle = new Customer("Jungle");
jungle->setNum(apple1, 2);
jungle->setNum(apple2, 4);
jungle->setNum(book1, 1);
jungle->setNum(book2, 3);
ShoppingCart* shoppingCart = new ShoppingCart();
ShoppingCart *shoppingCart = new ShoppingCart();
shoppingCart->addElement(apple1);
shoppingCart->addElement(apple2);
shoppingCart->addElement(book1);

View File

@ -19,13 +19,13 @@ void Customer::setNum(Book* book, int iNum){
void Customer::visit(Apple* apple){
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){
int price = book->getPrice();
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 *******/
@ -38,7 +38,7 @@ void Cashier::visit(Apple* apple){
int price = apple->getPrice();
int num = apple->getNum();
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){
@ -46,5 +46,5 @@ void Cashier::visit(Book* book){
string name = book->getName();
int num = book->getNum();
int total = price*num;
printf(" 《%s》 总价: %d 元\n", name.c_str(), total);
printf(" 《%s》 总价: %d 元\n", name.c_str(), total);
}