zoukankan      html  css  js  c++  java
  • 装饰器模式

    装饰器和继承的区别

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

    类图

    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("拖地");
        }
    }
    
  • 相关阅读:
    MySQL改变表的存储引擎
    数字三角形合集
    POJ 3250 Bad Hair Day 单调栈
    Linux 网卡驱动学习(二)(网络驱动接口小结)
    Lecture Notes: Macros
    [转]LNMP环境下的Web常见问题排查(精品)
    ssh-copy-id password
    python
    python
    Ceph
  • 原文地址:https://www.cnblogs.com/wfszmg/p/14027325.html
Copyright © 2011-2022 走看看