diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..21231eb --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,5 @@ +{ + "files.associations": { + "list": "cpp" + } +} \ No newline at end of file diff --git a/02.FactoryMethod/2.Code/FactoryMethod.h b/02.FactoryMethod/2.Code/FactoryMethod.h index 9f66ed2..95d3e6e 100644 --- a/02.FactoryMethod/2.Code/FactoryMethod.h +++ b/02.FactoryMethod/2.Code/FactoryMethod.h @@ -5,7 +5,7 @@ #include using namespace std; -//抽象产品类AbstractProduct +// 鎶借薄浜у搧绫籄bstractProduct class AbstractSportProduct { public: @@ -13,12 +13,12 @@ public: } virtual ~AbstractSportProduct(){} - //抽象方法: + // 鎶借薄鏂规硶锛 virtual void printName(){}; virtual void play(){}; }; -//具体产品类Basketball +// 鍏蜂綋浜у搧绫籅asketball 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 +// 鍏蜂綋浜у搧绫籉ootball 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 +// 鍏蜂綋浜у搧绫籚olleyball 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 +// 鍏蜂綋宸ュ巶绫籅asketballFactory class BasketballFactory :public AbstractFactory { public: @@ -90,7 +90,7 @@ public: } }; -//具体工厂类FootballFactory +// 鍏蜂綋宸ュ巶绫籉ootballFactory class FootballFactory :public AbstractFactory { public: @@ -102,7 +102,7 @@ public: } }; -//具体工厂类VolleyballFactory +// 鍏蜂綋宸ュ巶绫籚olleyballFactory class VolleyballFactory :public AbstractFactory { public: diff --git a/02.FactoryMethod/2.Code/main.cpp b/02.FactoryMethod/2.Code/main.cpp index 76785a7..778ed49 100644 --- a/02.FactoryMethod/2.Code/main.cpp +++ b/02.FactoryMethod/2.Code/main.cpp @@ -4,10 +4,10 @@ int main() { - printf("工厂方法模式\n"); - - //定义工厂类对象和产品类对象 + printf("宸ュ巶鏂规硶妯″紡\n"); + // 瀹氫箟宸ュ巶绫诲璞″拰浜у搧绫诲璞 + std::shared_ptr fac = make_shared(); std::shared_ptr product = std::shared_ptr(fac->getSportProduct()); diff --git a/03.AbstractFactory/2.Code/AbstractFactory.h b/03.AbstractFactory/2.Code/AbstractFactory.h index 6caf8f4..ac969e3 100644 --- a/03.AbstractFactory/2.Code/AbstractFactory.h +++ b/03.AbstractFactory/2.Code/AbstractFactory.h @@ -5,81 +5,89 @@ #include using namespace std; -//抽象产品类AbstractBall +// 鎶借薄浜у搧绫籄bstractBall class AbstractBall { public: - AbstractBall(){ - + AbstractBall() + { } - virtual ~AbstractBall(){} - //抽象方法: + virtual ~AbstractBall() {} + // 鎶借薄鏂规硶锛 virtual void play(){}; }; -//具体产品类Basketball -class Basketball :public AbstractBall +// 鍏蜂綋浜у搧绫籅asketball +class Basketball : public AbstractBall { public: - Basketball(){ + Basketball() + { play(); } - //具体实现方法 - void play(){ + // 鍏蜂綋瀹炵幇鏂规硶 + void play() + { printf("Jungle play Basketball\n\n"); } }; -//具体产品类Football -class Football :public AbstractBall +// 鍏蜂綋浜у搧绫籉ootball +class Football : public AbstractBall { public: - Football(){ + Football() + { play(); } - //具体实现方法 - void play(){ + // 鍏蜂綋瀹炵幇鏂规硶 + void play() + { printf("Jungle play Football\n\n"); } }; -//抽象产品类AbstractShirt +// 鎶借薄浜у搧绫籄bstractShirt class AbstractShirt { public: - AbstractShirt(){} - virtual ~AbstractShirt(){} - //抽象方法: + AbstractShirt() {} + virtual ~AbstractShirt() {} + // 鎶借薄鏂规硶锛 virtual void wearShirt(){}; }; -//具体产品类BasketballShirt -class BasketballShirt :public AbstractShirt +// 鍏蜂綋浜у搧绫籅asketballShirt +class BasketballShirt : public AbstractShirt { public: - BasketballShirt(){ + BasketballShirt() + { wearShirt(); } - //具体实现方法 - void wearShirt(){ + // 鍏蜂綋瀹炵幇鏂规硶 + void wearShirt() + { printf("Jungle wear Basketball Shirt\n\n"); } }; -//具体产品类FootballShirt -class FootballShirt :public AbstractShirt +// 鍏蜂綋浜у搧绫籉ootballShirt +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 +// 鍏蜂綋宸ュ巶绫籅asketballFactory +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 +// 鍏蜂綋宸ュ巶绫籅asketballFactory +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(); } diff --git a/04.BuilderPattern/2.Code/BuilderPattern.h b/04.BuilderPattern/2.Code/BuilderPattern.h index df5a1ed..4e90b1c 100644 --- a/04.BuilderPattern/2.Code/BuilderPattern.h +++ b/04.BuilderPattern/2.Code/BuilderPattern.h @@ -5,7 +5,7 @@ #include using namespace std; -//锟斤拷品锟斤拷House +// 浜у搧绫籋ouse class House { public: @@ -19,7 +19,7 @@ public: void setRoof(string iRoof) { this->roof = iRoof; } - //锟斤拷印House锟斤拷息 + // 鎵撳嵃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; }; -//锟斤拷锟斤拷锟斤拷锟斤拷AbstractBall +// 鎶借薄寤洪犺匒bstractBall class AbstractBuilder { public: @@ -46,7 +46,7 @@ public: house = nullptr; } } - //锟斤拷锟襟方凤拷锟斤拷 + // 鎶借薄鏂规硶锛 virtual void buildFloor() = 0; virtual void buildWall() = 0; virtual void buildRoof() = 0; @@ -55,7 +55,7 @@ public: House *house; }; -//锟斤拷锟藉建锟斤拷锟斤拷ConcreteBuilderA +// 鍏蜂綋寤洪犺匔oncreteBuilderA class ConcreteBuilderA :public AbstractBuilder { public: @@ -70,7 +70,7 @@ public: house = nullptr; } } - //锟斤拷锟斤拷实锟街凤拷锟斤拷 + // 鍏蜂綋瀹炵幇鏂规硶 void buildFloor() { this->house->setFloor("Floor_A"); } @@ -85,7 +85,7 @@ public: } }; -//锟斤拷锟藉建锟斤拷锟斤拷ConcreteBuilderB +// 鍏蜂綋寤洪犺匔oncreteBuilderB class ConcreteBuilderB :public AbstractBuilder { public: @@ -100,7 +100,7 @@ public: house = nullptr; } } - //锟斤拷锟斤拷实锟街凤拷锟斤拷 + // 鍏蜂綋瀹炵幇鏂规硶 void buildFloor() { this->house->setFloor("Floor_B"); } @@ -115,7 +115,7 @@ public: } }; -//指锟斤拷锟斤拷Director +// 鎸囨尌鑰匘irector class Director { public: @@ -128,10 +128,11 @@ public: builder = nullptr; } } - //锟斤拷锟斤拷实锟街凤拷锟斤拷 + // 鍏蜂綋瀹炵幇鏂规硶 void setBuilder(AbstractBuilder *iBuilder) { this->builder = iBuilder; } + // 灏佽缁勮娴佺▼锛岃繑鍥炲缓閫犵粨鏋 House *construct() { builder->buildFloor(); builder->buildWall(); diff --git a/04.BuilderPattern/2.Code/main.cpp b/04.BuilderPattern/2.Code/main.cpp index 466dcb3..e6f335a 100644 --- a/04.BuilderPattern/2.Code/main.cpp +++ b/04.BuilderPattern/2.Code/main.cpp @@ -2,21 +2,21 @@ int main() { - //抽象建造者 + // 鎶借薄寤洪犺 AbstractBuilder *builder; - //指挥者 + // 鎸囨尌鑰 Director *director = new Director(); - //产品:House + // 浜у搧锛欻ouse House *house; - //指定具体建造者A + // 鎸囧畾鍏蜂綋寤洪犺匒 builder = new ConcreteBuilderA(); director->setBuilder(builder); house = director->construct(); house->printfHouseInfo(); delete builder; - //指定具体建造者B + // 鎸囧畾鍏蜂綋寤洪犺匒 builder = new ConcreteBuilderB(); director->setBuilder(builder); house = director->construct(); diff --git a/05.PrototypePattern/2.Code/PrototypePattern.h b/05.PrototypePattern/2.Code/PrototypePattern.h index cea6d10..e712ee8 100644 --- a/05.PrototypePattern/2.Code/PrototypePattern.h +++ b/05.PrototypePattern/2.Code/PrototypePattern.h @@ -5,7 +5,7 @@ #include using namespace std; -//work model类 +// work model绫 class WorkModel { public: @@ -15,7 +15,7 @@ public: } }; -//抽象原型类PrototypeWork +// 鎶借薄鍘熷瀷绫籔rototypeWork class PrototypeWork { public: @@ -27,7 +27,7 @@ private: }; -//具体原型类ConcreteWork +// 鎶借薄鍘熷瀷绫籔rototypeWork 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); diff --git a/05.PrototypePattern/2.Code/main.cpp b/05.PrototypePattern/2.Code/main.cpp index 274afbd..cdd8047 100644 --- a/05.PrototypePattern/2.Code/main.cpp +++ b/05.PrototypePattern/2.Code/main.cpp @@ -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"); diff --git a/10.DecoratorPattern/2.Code/DecoratorPattern.h b/10.DecoratorPattern/2.Code/DecoratorPattern.h index e3bb945..7ca69d4 100644 --- a/10.DecoratorPattern/2.Code/DecoratorPattern.h +++ b/10.DecoratorPattern/2.Code/DecoratorPattern.h @@ -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("锟街伙拷\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__ \ No newline at end of file diff --git a/12.FlyweightPattern/2.Code/FlyweightPattern.h b/12.FlyweightPattern/2.Code/FlyweightPattern.h index 35280f7..178651d 100644 --- a/12.FlyweightPattern/2.Code/FlyweightPattern.h +++ b/12.FlyweightPattern/2.Code/FlyweightPattern.h @@ -5,7 +5,7 @@ #include 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来表示 + // 鍏变韩姹狅細鐢ㄤ竴涓獀ector鏉ヨ〃绀 vector devicePool; }; diff --git a/12.FlyweightPattern/2.Code/main.cpp b/12.FlyweightPattern/2.Code/main.cpp index 04a76da..db04f09 100644 --- a/12.FlyweightPattern/2.Code/main.cpp +++ b/12.FlyweightPattern/2.Code/main.cpp @@ -7,25 +7,25 @@ int main() NetDevice *device1, *device2, *device3, *device4; - // 客户端2获取一个hub + // 瀹㈡埛绔1鑾峰彇涓涓猦ub device1 = factory->getNetDevice('H'); device1->print(1); - // 客户端2获取一个hub + // 瀹㈡埛绔2鑾峰彇涓涓猦ub 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鑾峰彇涓涓猻witch device3 = factory->getNetDevice('S'); device3->print(1); - // 客户端4获取一个hub + // 瀹㈡埛绔4鑾峰彇涓涓猻witch 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"); diff --git a/14.ChainOfResponsibility/2.Code/ChainOfResponsibility.h b/14.ChainOfResponsibility/2.Code/ChainOfResponsibility.h index 02812b2..c569634 100644 --- a/14.ChainOfResponsibility/2.Code/ChainOfResponsibility.h +++ b/14.ChainOfResponsibility/2.Code/ChainOfResponsibility.h @@ -5,7 +5,7 @@ #include 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(); } }; diff --git a/14.ChainOfResponsibility/2.Code/main.cpp b/14.ChainOfResponsibility/2.Code/main.cpp index a132cdc..499b5ea 100644 --- a/14.ChainOfResponsibility/2.Code/main.cpp +++ b/14.ChainOfResponsibility/2.Code/main.cpp @@ -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"); diff --git a/15.CommandPattern/2.Code/main.cpp b/15.CommandPattern/2.Code/main.cpp index 42dc3f5..9aeadcb 100644 --- a/15.CommandPattern/2.Code/main.cpp +++ b/15.CommandPattern/2.Code/main.cpp @@ -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); diff --git a/16.InterpreterPattern/2.Code/InterpreterPattern.h b/16.InterpreterPattern/2.Code/InterpreterPattern.h index 05f2b04..263e39a 100644 --- a/16.InterpreterPattern/2.Code/InterpreterPattern.h +++ b/16.InterpreterPattern/2.Code/InterpreterPattern.h @@ -7,17 +7,17 @@ #include using namespace std; -// 抽象表达式类 +// 鎶借薄琛ㄨ揪寮忕被 class AbstractNode { public: AbstractNode(){} virtual ~AbstractNode(){} - // 声明抽象接口 + // 澹版槑鎶借薄鎺ュ彛 virtual char interpret() = 0; }; -// 终结符表达式:ValueNode +// 缁堢粨绗﹁〃杈惧紡锛歏alueNode class ValueNode :public AbstractNode { public : @@ -25,7 +25,7 @@ public : ValueNode(int iValue){ this->value = iValue; } - // 实现解释操作 + // 瀹炵幇瑙i噴鎿嶄綔 char interpret(){ return value; } @@ -33,7 +33,7 @@ private: int value; }; -// 终结符表达式:OperationNode +// 缁堢粨绗﹁〃杈惧紡锛歄perationNode class OperatorNode :public AbstractNode { public: @@ -41,7 +41,7 @@ public: OperatorNode(string iOp){ this->op = iOp; } - // 实现解释操作 + // 瀹炵幇瑙i噴鎿嶄綔 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: diff --git a/17.IteratorPattern/2.Code/Aggregate.cpp b/17.IteratorPattern/2.Code/Aggregate.cpp index 83eafa8..63fa952 100644 --- a/17.IteratorPattern/2.Code/Aggregate.cpp +++ b/17.IteratorPattern/2.Code/Aggregate.cpp @@ -17,5 +17,5 @@ int Television::getTotalChannelNum(){ } void Television::play(int i){ - printf("锟斤拷锟节诧拷锟脚o拷%s锟斤拷锟斤拷\n", channelList[i].c_str()); + printf("鐜板湪鎾斁锛%s鈥︹n", channelList[i].c_str()); } \ No newline at end of file diff --git a/17.IteratorPattern/2.Code/Aggregate.h b/17.IteratorPattern/2.Code/Aggregate.h index 5df2f5a..58e87bd 100644 --- a/17.IteratorPattern/2.Code/Aggregate.h +++ b/17.IteratorPattern/2.Code/Aggregate.h @@ -2,13 +2,14 @@ #define __AGGREGATE_H__ #include +#include 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 iChannelList); - // 实现创建迭代器 + Television(vector iChannelList); + // 瀹炵幇鍒涘缓杩唬鍣 Iterator* createIterator(); - // 获取总的频道数目 + // 鑾峰彇鎬荤殑棰戦亾鏁扮洰 int getTotalChannelNum(); void play(int i); private: - vector channelList; + vector channelList; }; #endif //__AGGREGATE_H__ \ No newline at end of file diff --git a/17.IteratorPattern/2.Code/Iterator.h b/17.IteratorPattern/2.Code/Iterator.h index 43e8d42..6fdb86e 100644 --- a/17.IteratorPattern/2.Code/Iterator.h +++ b/17.IteratorPattern/2.Code/Iterator.h @@ -7,13 +7,13 @@ #include 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; }; diff --git a/17.IteratorPattern/2.Code/main.cpp b/17.IteratorPattern/2.Code/main.cpp index 51b2c38..58674a2 100644 --- a/17.IteratorPattern/2.Code/main.cpp +++ b/17.IteratorPattern/2.Code/main.cpp @@ -3,16 +3,16 @@ int main() { - vector channelList = { "新闻频道", "财经频道", "体育频道", "电影频道", "音乐频道", "农业频道", "四川卫视", "成都卫视" }; - // 创建电视 + vector 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("锟斤拷锟斤拷锟斤拷锟:\n"); remoteControl->last(); - // 遍历电视所有频道 + // 閬嶅巻鐢佃鎵鏈夐閬 while (remoteControl->hasPrevious()){ remoteControl->currentChannel(); remoteControl->previous(); diff --git a/18.MediatorPattern/2.Code/Colleague.h b/18.MediatorPattern/2.Code/Colleague.h index bd104e1..c706cf3 100644 --- a/18.MediatorPattern/2.Code/Colleague.h +++ b/18.MediatorPattern/2.Code/Colleague.h @@ -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: diff --git a/18.MediatorPattern/2.Code/Landlord.cpp b/18.MediatorPattern/2.Code/Landlord.cpp index dd6f2a6..f2e0e63 100644 --- a/18.MediatorPattern/2.Code/Landlord.cpp +++ b/18.MediatorPattern/2.Code/Landlord.cpp @@ -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); } \ No newline at end of file diff --git a/18.MediatorPattern/2.Code/Mediator.h b/18.MediatorPattern/2.Code/Mediator.h index 9ef0365..07f8de0 100644 --- a/18.MediatorPattern/2.Code/Mediator.h +++ b/18.MediatorPattern/2.Code/Mediator.h @@ -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: diff --git a/18.MediatorPattern/2.Code/Tenant.cpp b/18.MediatorPattern/2.Code/Tenant.cpp index c27ca84..d24a158 100644 --- a/18.MediatorPattern/2.Code/Tenant.cpp +++ b/18.MediatorPattern/2.Code/Tenant.cpp @@ -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()); } \ No newline at end of file diff --git a/18.MediatorPattern/2.Code/common.h b/18.MediatorPattern/2.Code/common.h index 0662f31..e7b391a 100644 --- a/18.MediatorPattern/2.Code/common.h +++ b/18.MediatorPattern/2.Code/common.h @@ -1,7 +1,7 @@ #ifndef __COMMON_H__ #define __COMMON_H__ -// 公共头文件 +// 鍏叡澶存枃浠 #include using namespace std; diff --git a/18.MediatorPattern/2.Code/main.cpp b/18.MediatorPattern/2.Code/main.cpp index 8355e63..53e7a8c 100644 --- a/18.MediatorPattern/2.Code/main.cpp +++ b/18.MediatorPattern/2.Code/main.cpp @@ -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("锟斤拷锟斤拷锟斤拷"); jungle->setMediator(mediator); jianmengtu->setMediator(mediator); - // Jungle和贱萌兔在中介处登记求租信息 + // Jungle鍜岃幢钀屽厰鍦ㄤ腑浠嬪鐧昏姹傜淇℃伅 mediator->registerMethod(jungle); mediator->registerMethod(jianmengtu); diff --git a/20.ObserverPattern/2.Code/AllyCenter.h b/20.ObserverPattern/2.Code/AllyCenter.h index 9b940a7..2e96720 100644 --- a/20.ObserverPattern/2.Code/AllyCenter.h +++ b/20.ObserverPattern/2.Code/AllyCenter.h @@ -4,33 +4,34 @@ #include "common.h" #include -// 前向声明 +// 鍓嶅悜澹版槑 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::vectorplayerList; + // 鐜╁鍒楄〃 + std::vector playerList; }; -// 具体目标 -class AllyCenterController :public AllyCenter +// 鍏蜂綋鐩爣 +class AllyCenterController : public AllyCenter { public: AllyCenterController(); - // 实现通知方法 + // 瀹炵幇閫氱煡鏂规硶 void notify(INFO_TYPE infoType, std::string name); }; diff --git a/20.ObserverPattern/2.Code/Demo.h b/20.ObserverPattern/2.Code/Demo.h index 9b3cda2..c3b8264 100644 --- a/20.ObserverPattern/2.Code/Demo.h +++ b/20.ObserverPattern/2.Code/Demo.h @@ -4,63 +4,69 @@ using namespace std; #include -// 抽象观察者 +// 鎶借薄瑙傚療鑰 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: - // 观察者列表 - listobsList; + // 瑙傚療鑰呭垪琛 + list 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(); diff --git a/20.ObserverPattern/2.Code/Observer.h b/20.ObserverPattern/2.Code/Observer.h index e06b8d0..fd197cf 100644 --- a/20.ObserverPattern/2.Code/Observer.h +++ b/20.ObserverPattern/2.Code/Observer.h @@ -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()); } }; diff --git a/20.ObserverPattern/2.Code/main.cpp b/20.ObserverPattern/2.Code/main.cpp index c7703ae..1a99ff5 100644 --- a/20.ObserverPattern/2.Code/main.cpp +++ b/20.ObserverPattern/2.Code/main.cpp @@ -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("锟斤拷锟斤拷锟斤拷"); + Player *SillyDog = new Player("傻锟接癸拷"); 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"); diff --git a/21.StatePattern/2.Code/GameAccount.h b/21.StatePattern/2.Code/GameAccount.h index c29b60e..0d48022 100644 --- a/21.StatePattern/2.Code/GameAccount.h +++ b/21.StatePattern/2.Code/GameAccount.h @@ -3,7 +3,7 @@ using namespace std; #include -// 前向声明 +// 鍓嶅悜澹版槑 class Level; class GameAccount diff --git a/21.StatePattern/2.Code/GameAccout.cpp b/21.StatePattern/2.Code/GameAccout.cpp index 54f447c..439666f 100644 --- a/21.StatePattern/2.Code/GameAccout.cpp +++ b/21.StatePattern/2.Code/GameAccout.cpp @@ -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(){ diff --git a/21.StatePattern/2.Code/Level.cpp b/21.StatePattern/2.Code/Level.cpp index b18bdc3..4594f88 100644 --- a/21.StatePattern/2.Code/Level.cpp +++ b/21.StatePattern/2.Code/Level.cpp @@ -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鍗囩骇锛 绾у埆锛歋ECONDARY\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闄嶇骇锛 绾у埆锛歅RIMARY\n\n"); } else if (this->getGameAccount()->getScore() > 200){ this->getGameAccount()->setLevel(new Professional(this)); - printf("\t升级! 级别:PROFESSIONAL\n\n"); + printf("\t鍗囩骇锛 绾у埆锛歅ROFESSIONAL\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闄嶇骇锛 绾у埆锛歋ECONDARY\n\n"); } else if (this->getGameAccount()->getScore() > 250){ this->getGameAccount()->setLevel(new Final(this)); - printf("\t升级! 级别:FINAL\n\n"); + printf("\t鍗囩骇锛 绾у埆锛欶INAL\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闄嶇骇锛 绾у埆锛歅ROFESSIONAL\n\n"); } else{ - printf("\t%s 已经是最高级\n\n", this->getGameAccount()->getName().c_str()); + printf("\t%s 宸茬粡鏄渶楂樼骇\n\n", this->getGameAccount()->getName().c_str()); } } \ No newline at end of file diff --git a/21.StatePattern/2.Code/Level.h b/21.StatePattern/2.Code/Level.h index 7eff13d..bca1def 100644 --- a/21.StatePattern/2.Code/Level.h +++ b/21.StatePattern/2.Code/Level.h @@ -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(); }; diff --git a/22.StrategyPattern/2.Code/Context.h b/22.StrategyPattern/2.Code/Context.h index 31b1c1c..fb53c65 100644 --- a/22.StrategyPattern/2.Code/Context.h +++ b/22.StrategyPattern/2.Code/Context.h @@ -4,7 +4,7 @@ #include "Strategy.h" #include -// 上下文类 +// 涓婁笅鏂囩被 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){ diff --git a/22.StrategyPattern/2.Code/Strategy.h b/22.StrategyPattern/2.Code/Strategy.h index 38d4974..2cbce95 100644 --- a/22.StrategyPattern/2.Code/Strategy.h +++ b/22.StrategyPattern/2.Code/Strategy.h @@ -3,7 +3,7 @@ #include -// 抽象策略类 +// 鎶借薄绛栫暐绫 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 -// 基类 +// 鍩虹被 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.瀹夊叏妯″紡涓嬪姞瀵嗗拰瑙e瘑 + if (isSafeMode()) + { + // 2.1. 鍔犲瘑 encrypt(); - // 2.2. 解密 + // 2.2. 瑙e瘑 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("浣跨敤 绗竴浠g増鏈畻娉 澶勭悊鎸囩汗鍥惧儚\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瀵嗛挜瑙e瘑\n"); } }; -// 派生类 -class FingerprintModuleB :public FingerprintModule +// 娲剧敓绫 +class FingerprintModuleB : public FingerprintModule { public: - FingerprintModuleB(){} - void processImage(){ - printf("使用 第二代版本算法 处理指纹图像\n"); + FingerprintModuleB() {} + void processImage() + { + printf("浣跨敤 绗簩浠g増鏈畻娉 澶勭悊鎸囩汗鍥惧儚\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("浣跨敤 绗竴浠g増鏈畻娉 澶勭悊鎸囩汗鍥惧儚\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瀵嗛挜瑙e瘑\n"); } }; -#endif //__FINGERPRINTMODULE_H__ \ No newline at end of file +#endif //__FINGERPRINTMODULE_H__ \ No newline at end of file diff --git a/24.VisitorPattern/2.Code/Demo.h b/24.VisitorPattern/2.Code/Demo.h index 6791bf7..820468f 100644 --- a/24.VisitorPattern/2.Code/Demo.h +++ b/24.VisitorPattern/2.Code/Demo.h @@ -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: - lsitelementList; + list elementList; }; #endif \ No newline at end of file diff --git a/24.VisitorPattern/2.Code/Element.h b/24.VisitorPattern/2.Code/Element.h index 38ed092..774ac10 100644 --- a/24.VisitorPattern/2.Code/Element.h +++ b/24.VisitorPattern/2.Code/Element.h @@ -5,7 +5,7 @@ #include using namespace std; -// 抽象元素 +// 鎶借薄鍏冪礌 class Element { public: @@ -36,7 +36,7 @@ private: string name; }; -// 具体元素:Apple +// 鍏蜂綋鍏冪礌锛欰pple class Apple :public Element { public: @@ -45,7 +45,7 @@ public: void accept(Visitor*); }; -// 具体元素:Book +// 鍏蜂綋鍏冪礌锛欱ook class Book :public Element { public: diff --git a/24.VisitorPattern/2.Code/ShoppingCart.h b/24.VisitorPattern/2.Code/ShoppingCart.h index 9bcaab3..d5506db 100644 --- a/24.VisitorPattern/2.Code/ShoppingCart.h +++ b/24.VisitorPattern/2.Code/ShoppingCart.h @@ -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){ diff --git a/24.VisitorPattern/2.Code/Visitor.h b/24.VisitorPattern/2.Code/Visitor.h index 98f4b7f..88782d4 100644 --- a/24.VisitorPattern/2.Code/Visitor.h +++ b/24.VisitorPattern/2.Code/Visitor.h @@ -4,23 +4,23 @@ #include 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: diff --git a/24.VisitorPattern/2.Code/main.cpp b/24.VisitorPattern/2.Code/main.cpp index 605c221..2ef57e4 100644 --- a/24.VisitorPattern/2.Code/main.cpp +++ b/24.VisitorPattern/2.Code/main.cpp @@ -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); diff --git a/24.VisitorPattern/2.Code/visitor.cpp b/24.VisitorPattern/2.Code/visitor.cpp index 332f4ae..68db61f 100644 --- a/24.VisitorPattern/2.Code/visitor.cpp +++ b/24.VisitorPattern/2.Code/visitor.cpp @@ -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); } \ No newline at end of file