zoukankan      html  css  js  c++  java
  • 装饰者模式(C++)

    概述:

    23种设计模式之一,英文叫Decorator Pattern,又叫装饰者模式。装饰模式是在不必改变原类文件和使用继承的情况下,动态地扩展一个对象的功能。它是通过创建一个包装对象,也就是装饰来包裹真实的对象。

    装饰模式的特点:

    (1) 装饰对象和真实对象有相同的接口。这样客户端对象就能以和真实对象相同的方式和装饰对象交互。
    (2) 装饰对象包含一个真实对象的引用(reference)
    (3) 装饰对象接受所有来自客户端的请求。它把这些请求转发给真实的对象。
    (4) 装饰对象可以在转发这些请求以前或以后增加一些附加功能。这样就确保了在运行时,不用修改给定对象的结构就可以在外部增加附加的功能。在面向对象的设计中,通常是通过继承来实现对给定类的功能扩展。

    代码

    //header.h

    #include<iostream>
    #include<string>
    using namespace std;
    
    class Ship
    {
    public:
        virtual void run()=0;
        virtual void shot()=0;
    };
    
    class NorthCarolina:public Ship
    {
    public:
        void run(){cout<<"NorthCarolina is running."<<endl;};
        void shot(){cout<<"NorthCarolina is shotting."<<endl;};
    };
    
    class Iowa:public Ship
    {
        public:
        void run(){cout<<"Iowa is running."<<endl;};
        void shot(){cout<<"Iowa is shotting."<<endl;};
    };
    
    class Decorator:public Ship
    {
    public:
        Decorator(Ship* ship):ship(ship){};
        void run(){ship->run();}
        void shot(){ship->shot();}
    protected:
        Ship* ship;
    };
    
    class ScoutPlane:public Decorator
    {
        string scout;
    public:
        ScoutPlane(Ship* ship):Decorator(ship){}
        void setScout(){scout="Searching......";};
        void getScout(){cout<<scout<<endl;}
        void run(){
            ship->run();
            setScout();
            getScout();
        }
        void shot(){
            ship->shot();
        }
    };
    
    class Ammu:public Decorator
    {
        string ammu;
    public:
        Ammu(Ship* ship):Decorator(ship){}
        void setAmmu(){ammu="AP";};
        void getAmmu(){cout<<"shotting "<<ammu<<"."<<endl;}
        void run(){
            ship->run();
        }
        void shot(){
            ship->shot();
            setAmmu();
            getAmmu();
        }
    };
    

    //main.cpp

    #include"header.h"
    using namespace std;
    
    int main()
    {
        Ship* ship1(new Iowa);
        Ship* ship2(new NorthCarolina);
        ship1->run();
        ship1->shot();
        cout<<"----------------"<<endl;
        ship2->run();
        ship2->shot();
        cout<<"----------------"<<endl;
        Ship* ship3(new Ammu(ship1));
        ship3->run();
        ship3->shot();
        cout<<"----------------"<<endl;
        Ship* ship4(new ScoutPlane(ship3));
        ship4->run();
        ship4->shot();
        cout<<"----------------"<<endl;
        Ship* ship5(new ScoutPlane(ship2));
        ship5->run();
        ship5->shot();
        system("pause");
        return 0;
    }

    优点

    1. Decorator模式与继承关系的目的都是要扩展对象的功能,但是Decorator可以提供比继承更多的灵活性。
    2. 通过使用不同的具体装饰类以及这些装饰类的排列组合,设计师可以创造出很多不同行为的组合。

    缺点

    1. 这种比继承更加灵活机动的特性,也同时意味着更加多的复杂性。
    2. 装饰模式会导致设计中出现许多小类,如果过度使用,会使程序变得很复杂。
    3. 装饰模式是针对抽象组件(Component)类型编程。但是,如果你要针对具体组件编程时,就应该重新思考你的应用架构,以及装饰者是否合适。当然也可以改变Component接口,增加新的公开的行为,实现“半透明”的装饰者模式。在实际项目中要做出最佳选择。

    设计原则

    1. 多用组合,少用继承。
      利用继承设计子类的行为,是在编译时静态决定的,而且所有的子类都会继承到相同的行为。然而,如果能够利用组合的做法扩展对象的行为,就可以在运行时动态地进行扩展。
    2. 类应设计的对扩展开放,对修改关闭。
  • 相关阅读:
    Wireshark——工具
    Wireshark——网络协议
    Wireshark——过滤器
    Wireshark——数据包、着色规则和提示
    Floyd最小环
    有向环覆盖问题
    KM算法
    归并排序
    树状数组
    构造强连通图
  • 原文地址:https://www.cnblogs.com/inory/p/5468664.html
Copyright © 2011-2022 走看看