diff --git a/README.md b/README.md index 606e5e3d8da33027849f6a509ffe39dadf6c0277..c10b44346e743c617f7726eb5da868bb9f448c66 100644 --- a/README.md +++ b/README.md @@ -43,6 +43,7 @@ + [Template Method](https://github.com/liu-jianhao/Cpp-Design-Patterns/tree/master/Template%20Method) + [Observer/Event](https://github.com/liu-jianhao/Cpp-Design-Patterns/tree/master/Observer) + [Strategy](https://github.com/liu-jianhao/Cpp-Design-Patterns/tree/master/Strategy) ++ [Strategy(Head-First版)](https://github.com/liu-jianhao/Cpp-Design-Patterns/tree/master/Strategy-Pattern) ### 单一职责: + [Decorator](https://github.com/liu-jianhao/Cpp-Design-Patterns/tree/master/Decorator) + [Bridge](https://github.com/liu-jianhao/Cpp-Design-Patterns/tree/master/Bridge) diff --git a/Strategy-Pattern/DecoyDuck.cpp b/Strategy-Pattern/DecoyDuck.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1360b8544229faafe83b1c2350c8437d3e5b5c0f --- /dev/null +++ b/Strategy-Pattern/DecoyDuck.cpp @@ -0,0 +1,15 @@ +#include "DecoyDuck.hpp" + +#include "FlyNoWay.hpp" +#include "MuteQuack.hpp" + +#include + +DecoyDuck::DecoyDuck() : + Duck(std::make_unique(), std::make_unique()) { +} + +void DecoyDuck::display() { + std::cout << "I'm a real decoy duck" << std::endl; +} + diff --git a/Strategy-Pattern/DecoyDuck.hpp b/Strategy-Pattern/DecoyDuck.hpp new file mode 100644 index 0000000000000000000000000000000000000000..b213de59c492aedb964e7f9e96635363b80bd4b7 --- /dev/null +++ b/Strategy-Pattern/DecoyDuck.hpp @@ -0,0 +1,13 @@ +#ifndef DECOYDUCK_HPP +#define DECOYDUCK_HPP + +#include "Duck.hpp" + +class DecoyDuck : public Duck { +public: + DecoyDuck(); + + void display(); +}; + +#endif \ No newline at end of file diff --git a/Strategy-Pattern/Duck.hpp b/Strategy-Pattern/Duck.hpp new file mode 100644 index 0000000000000000000000000000000000000000..42aaad5034aa081e6384fe169c52db6ef987c546 --- /dev/null +++ b/Strategy-Pattern/Duck.hpp @@ -0,0 +1,35 @@ +#ifndef DUCK_HPP +#define DUCK_HPP + +#include "FlyBehavior.hpp" +#include "QuackBehavior.hpp" + +#include +#include + +class Duck { +public: + std::unique_ptr flyBehavior; + std::unique_ptr quackBehavior; + + Duck(std::unique_ptr fb, std::unique_ptr qb) : flyBehavior(std::move(fb)), quackBehavior(std::move(qb)) {} + + virtual ~Duck(){} + + // 下面两个函数使用了多态 + void performFly(){ + flyBehavior->fly(); + } + + void performQuack(){ + quackBehavior->quack(); + } + + void swim(){ + std::cout << "All ducks swim!" << std::endl; + } + virtual void display(){} +}; + + +#endif \ No newline at end of file diff --git a/Strategy-Pattern/FlyBehavior.hpp b/Strategy-Pattern/FlyBehavior.hpp new file mode 100644 index 0000000000000000000000000000000000000000..e7e022e706941e0270f580b351b97ad6d82d4b99 --- /dev/null +++ b/Strategy-Pattern/FlyBehavior.hpp @@ -0,0 +1,12 @@ +#ifndef FLYBEHAVIOR_HPP +#define FLYBEHAVIOR_HPP + +// 接口类 +class FlyBehavior { +public: + virtual void fly() = 0; + + virtual ~FlyBehavior() = default; +}; + +#endif \ No newline at end of file diff --git a/Strategy-Pattern/FlyNoWay.hpp b/Strategy-Pattern/FlyNoWay.hpp new file mode 100644 index 0000000000000000000000000000000000000000..6cf2324fc7cdb92cd73c9b5cc03062bff8e4525b --- /dev/null +++ b/Strategy-Pattern/FlyNoWay.hpp @@ -0,0 +1,15 @@ +#ifndef FLYNOWAY_HPP +#define FLYNOWAY_HPP + +#include "FlyBehavior.hpp" + +#include + +class FlyNoWay : public FlyBehavior { +public: + void fly() { + std::cout << "I can't fly" << std::endl; + } +}; + +#endif \ No newline at end of file diff --git a/Strategy-Pattern/FlyWithWings.hpp b/Strategy-Pattern/FlyWithWings.hpp new file mode 100644 index 0000000000000000000000000000000000000000..652a12f69de39db0b27d5db4bc1cbdb9544e3465 --- /dev/null +++ b/Strategy-Pattern/FlyWithWings.hpp @@ -0,0 +1,15 @@ +#ifndef FLYWITHWINGS_HPP +#define FLYWITHWINGS_HPP + +#include "FlyBehavior.hpp" + +#include + +class FlyWithWings : public FlyBehavior { +public: + void fly() { + std::cout << "I'm flying" << std::endl; + } +}; + +#endif \ No newline at end of file diff --git a/Strategy-Pattern/Makefile b/Strategy-Pattern/Makefile new file mode 100644 index 0000000000000000000000000000000000000000..951371aa47f534550b68dd924cfc44bc1f7bbd7b --- /dev/null +++ b/Strategy-Pattern/Makefile @@ -0,0 +1,5 @@ +all: + g++ -g -Wall ./*.cpp -o main + +clean: + rm main diff --git a/Strategy-Pattern/MallardDuck.cpp b/Strategy-Pattern/MallardDuck.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b7351c20eccb7f2910d2bf1796598d7a65f524cd --- /dev/null +++ b/Strategy-Pattern/MallardDuck.cpp @@ -0,0 +1,15 @@ +#include "MallardDuck.hpp" + +#include "FlyWithWings.hpp" +#include "Quack.hpp" + +#include + +MallardDuck::MallardDuck() : + Duck(std::make_unique(), std::make_unique()) { +} + +void MallardDuck::display() { + std::cout << "I'm a real mallard duck" << std::endl; +} + diff --git a/Strategy-Pattern/MallardDuck.hpp b/Strategy-Pattern/MallardDuck.hpp new file mode 100644 index 0000000000000000000000000000000000000000..5f4622cb28ac47e997269155fe9d38c7f1df6a72 --- /dev/null +++ b/Strategy-Pattern/MallardDuck.hpp @@ -0,0 +1,13 @@ +#ifndef MALLARDDUCK_HPP +#define MALLARDDUCK_HPP + +#include "Duck.hpp" + +class MallardDuck : public Duck { +public: + MallardDuck(); + + void display(); +}; + +#endif \ No newline at end of file diff --git a/Strategy-Pattern/MuteQuack.hpp b/Strategy-Pattern/MuteQuack.hpp new file mode 100644 index 0000000000000000000000000000000000000000..6ceb97f334cb70b0c2925cabaa5f7e5429342754 --- /dev/null +++ b/Strategy-Pattern/MuteQuack.hpp @@ -0,0 +1,15 @@ +#ifndef MUTEQUACK_HPP +#define MUTEQUACK_HPP + +#include "QuackBehavior.hpp" + +#include + +class MuteQuack : public QuackBehavior { +public: + void quack() { + std::cout << "Mute Quack" << std::endl; + } +}; + +#endif \ No newline at end of file diff --git a/Strategy-Pattern/Quack.hpp b/Strategy-Pattern/Quack.hpp new file mode 100644 index 0000000000000000000000000000000000000000..cdaf06c6f9784d479b3176bd49953c89076a6348 --- /dev/null +++ b/Strategy-Pattern/Quack.hpp @@ -0,0 +1,15 @@ +#ifndef QUACK_HPP +#define QUACK_HPP + +#include "QuackBehavior.hpp" + +#include + +class Quack : public QuackBehavior { +public: + void quack() { + std::cout << "Quack" << std::endl; + } +}; + +#endif \ No newline at end of file diff --git a/Strategy-Pattern/QuackBehavior.hpp b/Strategy-Pattern/QuackBehavior.hpp new file mode 100644 index 0000000000000000000000000000000000000000..ddccdd63179f5a5def19ae56183b609316530b44 --- /dev/null +++ b/Strategy-Pattern/QuackBehavior.hpp @@ -0,0 +1,12 @@ +#ifndef QUACKBEHAVIOR_HPP +#define QUACKBEHAVIOR_HPP + +// 接口类 +class QuackBehavior { +public: + virtual void quack() = 0; + + virtual ~QuackBehavior() = default; +}; + +#endif \ No newline at end of file diff --git a/Strategy-Pattern/README.md b/Strategy-Pattern/README.md new file mode 100644 index 0000000000000000000000000000000000000000..2c830e14aeab06cb3d9958026b6199a529f0d1a5 --- /dev/null +++ b/Strategy-Pattern/README.md @@ -0,0 +1,3 @@ +## ???? + +![](https://img-blog.csdnimg.cn/20190527222242725.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlc3Ricm9va2xpdQ==,size_16,color_FFFFFF,t_70) diff --git a/Strategy-Pattern/RedHeadDuck.cpp b/Strategy-Pattern/RedHeadDuck.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9274d5bcbb6b44c06f23b4ebc99d98a1d194ae6a --- /dev/null +++ b/Strategy-Pattern/RedHeadDuck.cpp @@ -0,0 +1,15 @@ +#include "RedHeadDuck.hpp" + +#include "FlyWithWings.hpp" +#include "Quack.hpp" + +#include + +RedHeadDuck::RedHeadDuck() : + Duck(std::make_unique(), std::make_unique()) { +} + +void RedHeadDuck::display() { + std::cout << "I'm a real red head duck" << std::endl; +} + diff --git a/Strategy-Pattern/RedHeadDuck.hpp b/Strategy-Pattern/RedHeadDuck.hpp new file mode 100644 index 0000000000000000000000000000000000000000..43a80ea09eb04b1c2935411f89734ea94ca1c8ee --- /dev/null +++ b/Strategy-Pattern/RedHeadDuck.hpp @@ -0,0 +1,13 @@ +#ifndef REDHEADDUCK_HPP +#define REDHEADDUCK_HPP + +#include "Duck.hpp" + +class RedHeadDuck : public Duck { +public: + RedHeadDuck(); + + void display(); +}; + +#endif \ No newline at end of file diff --git a/Strategy-Pattern/RubberDuck.cpp b/Strategy-Pattern/RubberDuck.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e5883cceac6502997987fcaad3bc627c79a2e7ca --- /dev/null +++ b/Strategy-Pattern/RubberDuck.cpp @@ -0,0 +1,15 @@ +#include "RubberDuck.hpp" + +#include "FlyNoWay.hpp" +#include "Squeak.hpp" + +#include + +RubberDuck::RubberDuck() : + Duck(std::make_unique(), std::make_unique()) { +} + +void RubberDuck::display() { + std::cout << "I'm a real rubber duck" << std::endl; +} + diff --git a/Strategy-Pattern/RubberDuck.hpp b/Strategy-Pattern/RubberDuck.hpp new file mode 100644 index 0000000000000000000000000000000000000000..a775019d69c239582cf86eff4ff881fb321c28a3 --- /dev/null +++ b/Strategy-Pattern/RubberDuck.hpp @@ -0,0 +1,13 @@ +#ifndef RUBBERDUCK_HPP +#define RUBBERDUCK_HPP + +#include "Duck.hpp" + +class RubberDuck : public Duck { +public: + RubberDuck(); + + void display(); +}; + +#endif \ No newline at end of file diff --git a/Strategy-Pattern/Squeak.hpp b/Strategy-Pattern/Squeak.hpp new file mode 100644 index 0000000000000000000000000000000000000000..71dc619984583a1cb1a2d34f41fe6abb5b678882 --- /dev/null +++ b/Strategy-Pattern/Squeak.hpp @@ -0,0 +1,15 @@ +#ifndef SQUEAK_HPP +#define SQUEAK_HPP + +#include "QuackBehavior.hpp" + +#include + +class Squeak : public QuackBehavior { +public: + void quack() { + std::cout << "Squeak" << std::endl; + } +}; + +#endif \ No newline at end of file diff --git a/Strategy-Pattern/main.cpp b/Strategy-Pattern/main.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0581f19897d7e69957fdfdc9e564ee209b9ba03e --- /dev/null +++ b/Strategy-Pattern/main.cpp @@ -0,0 +1,43 @@ +#include "MallardDuck.hpp" +#include "RedHeadDuck.hpp" +#include "DecoyDuck.hpp" +#include "RubberDuck.hpp" + +#include + +int main() { + MallardDuck mallard; + mallard.display(); + mallard.swim(); + mallard.performQuack(); + mallard.performFly(); + + std::cout << std::endl; + + RedHeadDuck redHead; + redHead.display(); + redHead.swim(); + redHead.performQuack(); + redHead.performFly(); + + std::cout << std::endl; + + DecoyDuck decoy; + decoy.display(); + decoy.swim(); + decoy.performQuack(); + decoy.performFly(); + + std::cout << std::endl; + + RubberDuck rubberDuckie; + rubberDuckie.display(); + rubberDuckie.swim(); + rubberDuckie.performQuack(); + rubberDuckie.performFly(); + + std::cout << std::endl; + + return 0; +} +