• 装饰器模式


    装饰器和继承的区别

    • 继承模式: 这种方式是静态的, 一定是要写一个新的子类, 对类层级进行拓展
    • 装饰器模式(关联机制): 动态的; 拿到一个对象就可以对其进行拓展, 不需要修改原有类的逻辑

    类图

    package 装饰器模式;
    
    import jdk.nashorn.internal.ir.annotations.Ignore;
    
    import static java.lang.annotation.ElementType.PARAMETER;
    
    /**
     * @author 无法手执玫瑰
     * 2020/11/0023 21:13
     */
    public class DecoratorPatter {
        public static void main(String[] args) {
            new RobotDecorator(new FirstRobot()).doMoreThing();
        }
    
    }
    interface Robot{
        void doSomething();
    }
    
    class FirstRobot implements Robot{
    
        @Override
        public void doSomething() {
            System.out.println("唱歌");
            System.out.println("对话");
        }
    }
    
    class RobotDecorator implements Robot{
        private Robot robot;
    
        public RobotDecorator(Robot robot) {
            this.robot = robot;
        }
    
        @Override
        public void doSomething() {
            robot.doSomething();
        }
        public void doMoreThing(){
            robot.doSomething();
            System.out.println("跳舞");
            System.out.println("拖地");
        }
    }
    
  • 相关阅读:
    FL2440-学习记录(二)
    FL2440-学习记录(三)
    C专家编程 第一章
    C陷阱与缺陷
    ARM体系结构与编程-第二章
    二叉堆 及 大根堆的python实现
    八皇后问题
    非递归全排列 python实现
    Python 学习
    poj1064 Cable master(二分查找,精度)
  • 原文地址:https://www.cnblogs.com/wfszmg/p/14027325.html
Copyright © 2020-2023  润新知