• 装饰模式Decorator


    1.意图:动态地给一个对象添加一些额外职责。就增加功能而言,Decorator模式比生成子类更加灵活。
    2.结构
    在这里插入图片描述

    Component定义一个对象接口,可以给这些对象动态地添加职责;
    ConcreteComponent定义一个对象,可以给这个对象添加一些职责;
    Decorator维持一个指向Component对象的指针,并定义一个于Component接口一致的接口;
    ConcreteDecorator向组件添加职责。
    3.适用性:
    在不影响其他对象的情况下,以动态、透明的方式给单个对象添加职责;
    处理那些可以撤销的职责;
    当不能采用生成子类的方式进行扩充时。一种情况是,可能有大量独立的扩展,为支持每一种组合将产生大量的子类,使得子类数目呈爆炸性增长。另一种情况可能是,由于类定义被隐藏,或类定义不能用于生成子类。
    代码案例
    发票Invoice由抬头Head部分、正文部分和脚注Foot部分构成。现采用装饰Decorator模式实现打印发票的功能。
    软件设计师考试2016年下半年下午题第6题

    /**
     * 装饰模式
     */
    class Invoice{
        public void printInvoice(){
            System.out.println("This is the content of the invoice!");
        }
    }
    
    class Decorator extends Invoice{
        protected Invoice ticket;
        public Decorator (Invoice t){
            ticket = t;
        }
        public void printInvoice(){
            if(ticket!=null) {
                ticket.printInvoice();
            }
        }
    }
    class HeadDecorator extends Decorator{
        public HeadDecorator(Invoice t) {
            super(t);
        }
        public void printInvoice(){
            System.out.println("This is the header of the invoice!");
            super.printInvoice();
        }
    }
    class FootDecorator extends Decorator{
        public FootDecorator(Invoice t) {
            super(t);
        }
        public void printInvoice(){
            super.printInvoice();
            System.out.println("This is the foot of the invoice!");
        }
    }
    class Test{
        public static void main(String[] args) {
            Invoice t = new Invoice();
            Invoice ticket;
            ticket = new HeadDecorator(new FootDecorator(t));
            ticket.printInvoice();
            System.out.println("--------------------");
            ticket = new HeadDecorator(new FootDecorator(null));
            ticket.printInvoice();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49

    运行结果
    在这里插入图片描述

  • 相关阅读:
    LeetCode50天刷题计划(Day 31— 插入区间(9.00-11.20)
    【虚拟现实】一、AR与VR的基本原理
    Java入门笔记
    【译】SQLAlchemy文档:SQLAlchemy 统一教程
    强软弱虚引用
    C语言 深度探究C语言中的字符串
    electron 应用开发优秀实践
    Linux系统--多线程
    ECR - Elastic Container Registry
    打破汽车零部件企业供应链壁垒,数商云SCM供应链系统实现一体化采购协同
  • 原文地址:https://blog.csdn.net/qq_39548700/article/details/128182133