2023年8月31日,周四上午
这是我目前碰到的最难的设计模式.....
非常难以理解而且比较灵活多半,学得贼难受,写得贼费劲.....
2023年8月31日,周四晚上19:48
终于写完了,花了一天的时间来学习装饰模式和写这篇博客。
虽然基本上把我今天的收获都写下来了,
但感觉写得还是不够好,有很多东西没有表达出来、表达清楚,
以后有空再更新吧....
目录
装饰模式是一种结构型设计模式,它允许向一个现有的对象添加新的功能,同时又不改变其结构。
假设我有一个类Xxx,需要给它添加加法功能和减法功能,
但是由于这个类已经很复杂了、而且继承的层次已经很深了,
我不想改动里面的代码,也不想再多弄一个子类,那么怎么添加这两个功能呢?
- class Xxx:public Ppp{
- public:
- void func1();
- void func2();
- void func3();
- void func4();
- void func5();
- void func6();
- void func7();
- };
在这种情况下,可以使用装饰模式来添加这两个功能,
因为这样既不用改动类内部代码,也不用再多弄一个子类。
- // 原始类
- class Xxx :public Ppp{
- public:
- void func1() { /* 原始功能1的实现 */ }
- void func2() { /* 原始功能2的实现 */ }
- // ... 其他原始功能的实现
- void func7() { /* 原始功能7的实现 */ }
- };
-
- // 装饰类 - 加法功能
- class AddDecorator : public Xxx {
- private:
- Xxx* component;
-
- public:
- AddDecorator(Xxx* component) : component(component) {}
-
- void func1() override {
- component->func1();
- // 加法功能的实现
- }
- };
-
- // 装饰类 - 减法功能
- class SubtractDecorator : public Xxx {
- private:
- Xxx* component;
-
- public:
- SubtractDecorator(Xxx* component) : component(component) {}
-
- void func2() override {
- component->func2();
- // 减法功能的实现
- }
- };
-
- int main() {
- Xxx* xxx = new Xxx();
-
- // 使用加法功能的装饰类
- Xxx* xxxWithAddition = new AddDecorator(xxx);
- xxxWithAddition->func1();
-
- // 使用减法功能的装饰类
- Xxx* xxxWithSubtraction = new SubtractDecorator(xxx);
- xxxWithSubtraction->func2();
-
- delete xxxWithSubtraction;
- delete xxxWithAddition;
- delete xxx;
-
- return 0;
- }
注意,这并不是一个完整的装饰模式,但有助于理解装饰模式的作用。
- #include
-
- // 抽象构件类
- class Component {
- public:
- virtual void func() = 0;
- };
-
- // 具体构件类
- class ConcreteComponent : public Component {
- public:
- void func() override {
- std::cout << "具体构件的操作" << std::endl;
- }
- };
-
- // 装饰类
- class Decorator : public Component {
- protected:
- Component* component;
-
- public:
- Decorator(Component* component) : component(component) {}
-
- void func() override {
- if (component != nullptr) {
- component->func();
- }
- }
- };
-
- // 具体装饰类A
- class ConcreteDecoratorA : public Decorator {
- public:
- ConcreteDecoratorA(Component* component) : Decorator(component) {}
-
- void addedBehavior() {
- std::cout << "具体装饰类的附加行为A" << std::endl;
- }
-
- void func() override {
- Decorator::func();
- addedBehavior();
- }
- };
-
- // 具体装饰类B
- class ConcreteDecoratorB : public Decorator {
- public:
- ConcreteDecoratorB(Component* component) : Decorator(component) {}
-
- void addedBehavior() {
- std::cout << "具体装饰类的附加行为B" << std::endl;
- }
-
- void func() override {
- Decorator::func();
- addedBehavior();
- }
- };
-
- int main() {
- Component* component = new ConcreteComponent();
-
- //用decoratedComponentA修饰component
- Component* decoratedComponentA = new ConcreteDecoratorA(component);
- //用decoratedComponentB修饰被decoratedComponentA修饰过的component
- Component* decoratedComponentB = new ConcreteDecoratorB(decoratedComponentA);
-
- decoratedComponentB->func();
-
- //要记得释放申请的堆内存
- delete decoratedComponentA;
- delete decoratedComponentB;
- delete component;
-
- return 0;
- }

装饰模式是怎么实现的不改动类原来的代码和结构就增加新的功能的?
装饰器是如何装饰具体构建类的?
他们有同一个抽象父类
这意味这什么?它们都会有抽象构件类的纯虚函数
装饰器需要引入抽象构件类
能引入抽象构件类,意味传入具体构件类对象后,只能调用其中抽象构件类有的方法
装饰器会重写父类的方法,并在重写的方法里面调用具体构件类的方法
具体装饰器有自己独特的成员和方法
具体装饰器会把具体构件类装入
具体装饰器会重写抽象装饰器的方法,并在重写的方法里面调用父类的方法和自己独特的方法
不管怎样,具体装饰类必须引入抽象构件类并传入具体构件类对象
装饰模式的核心在于重写?
重写的时候加入额外的方法
具体构件类需要实现抽象构件类的纯虚函数,否则无法被创建
装饰器的作用是什么?
引入具体构件类
在重写方法中调用具体构件类的方法
抽象构件类的作用是什么?
具体构件类的作用是什么?
定义最基础的功能
具体装饰器的作用是什么?
写额外的功能,然后通过重写方法加入额外的功能
- #include
-
- class Product{
- public:
- virtual void buy()=0;
- };
-
- class Computer:public Product{
- public:
- void buy ()override{
- std::cout<<"买了一台电脑"<
- }
- };
-
- class Decorate:public Product{
- private:
- Product *product;
- public:
- Decorate(Product *product):product(product){};
-
- void buy() override{
- if(product!=nullptr){
- product->buy();
- }
- }
- };
-
- class Mouse:public Decorate{
- public:
- Mouse(Product *product):Decorate(product){};
-
- void buy() override{
- Decorate::buy();
- std::cout<<"又买了鼠标"<
- }
- };
-
- class KeyBoard:public Decorate{
- public:
- KeyBoard(Product *product):Decorate(product){};
-
- void buy() override{
- Decorate::buy();
- std::cout<<"又买了键盘"<
- }
- };
-
- int main(){
- Product *computer=new Computer();
- Mouse *mouse=new Mouse(computer);
- KeyBoard *keyBoard=new KeyBoard(mouse);
- keyBoard->buy();
- }

-
相关阅读:
Leetcode:【485. 最大连续 1 的个数】
Nginx访问控制与虚拟主机
Redis源码学习(31),字典学习,dict.c(一)
【userfaultfd+msg_msg+pipe_buffer】CISCN2022-cactus
React - Ant Design3.x版本安装使用,并按需引入和自定义主题
Spark SQL简介
如何评价最近爆红的FastAPI?
定制SQLmap和WAF绕过
【图论】图文详解Tarjan算法有向图缩点
宁德时代打响增长保卫战
-
原文地址:https://blog.csdn.net/m0_61629312/article/details/132600100