美文网首页
人要衣装,佛要金装之装饰模式

人要衣装,佛要金装之装饰模式

作者: BUG弄潮儿 | 来源:发表于2021-01-06 20:49 被阅读0次

    0x01:装饰模式简介

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

    先看最近一个热点头条:环卫工大姐化妆后变美女

    在这里插入图片描述

    化妆前与化妆后判若两个人,所以各位小伙伴在追求另一半时,不要看走眼了。虽然老祖宗说:人美在心里,鸟美在羽毛,但是外表的美丽是我们的第一感观。闲话少说,言归正传,装饰模式的UML类图如下:


    在这里插入图片描述

    主要角色如下:

    抽象构件(Component):定义一个对象接口或抽象类,可以给这些对象动态地添加职责;

    具体构件(ConcreteComponent):定义一个具体对象,实际被动态地添加职责的对象;

    抽象装饰者(Decorator):装饰抽象类,实现接口或抽象方法,实现了Component接口,用来扩展Component类的功能,但对于Component来说,是无需知道Decorator的存在;

    具体装饰者(ConcreteDecorator):具体装饰对象,起到给Component添加职责的功能;

    0x02:装饰模式的实现

    Component:Component是一个接口或是抽象类,就是定义最核心的对象,也就是最原始的对象

    public abstract class Component {
    
         public abstract void operation();
    
    }
    

    ConretetComponent:具体构件,通过继承实现Component抽象类中的抽象方法。是最核心、最原始、最基本的接口或抽象类的实现,要进行装饰的就是该类

    public class ConcreteComponent extends Component {
    
        @Override
        public void operation() {
            System.out.println("我是最原始的具体对象");
        }
    
    }
    

    Decorator:一般是一个抽象类,在其属性里必然有一个private变量指向Component抽象构件

    public abstract class Decorator extends Component {
    
        private Component component = null;
    
        //通过构造函数传递给被修饰者
        public Decorator(Component component) {
            this.component = component;
        }
    
        //委托给被修饰者执行
        @Override
        public void operation() {
            if(component != null) {
                this.component.operation();
            }
        }
    
    }
    

    ConcreteDecorator:可以写多个具体装饰实现类,把最核心的、最原始的、最基本的东西装饰成其它东西。

    这里就写两个类,稍改一下二者的实现顺序,看看结果。

    ConcreteDecoratorA类,它的operation()方法先执行了decoratorMethod1()方法,再执行了Decorator的operation()方法。

    public class ConcreteDecoratorA extends Decorator {
    
        //定义被修饰者
        public ConcreteDecoratorA(Component component) {
            super(component);
        }
    
        //定义自己的修饰方法
        private void decoratorMethod1() {
            System.out.println("decoratorMethod1 修饰");
        }
    
        @Override
        public void operation() {
            this.decoratorMethod1();
            super.operation();
        }
    
    }
    

    ConcreteDecoratorB类,它的operation()方法先执行了Decorator的operation()方法,再执行了decoratorMethod2()方法。

    public class ConcreteDecoratorB extends Decorator {
    
        //定义被修饰者
        public ConcreteDecoratorB(Component component) {
            super(component);
        }
    
        //定义自己的修饰方法
        private void decoratorMethod2() {
            System.out.println("decoratorMethod2 修饰");
        }
    
        @Override
        public void operation() {
            super.operation();
            this.decoratorMethod2();
        }
    
    }
    

    装饰模式测试代码

    public class Client {
    
        public static void main(String[] args) {
            Component component = new ConcreteComponent();
            //第一次包装
            component = new ConcreteDecoratorA(component);
            //第二次包装
            component = new ConcreteDecoratorB(component);
            //修饰后运行
            component.operation();
        }
    
    }
    

    以上就简单实现了一个装饰模式,装饰可以说在生活中无所不在。给你一套80平的毛坯房,可以有简单装饰、精装饰、豪华装饰等。

    0x03:装饰模式在JDK的运用

    装饰模式在java.io包的各种输入 / 输出流运用最为广泛。下面以输入流为例讲解:

    抽象构件:InputStream,它是一个抽象类,相当于Component;

    具体构件:ByteArrayInputStream、FileInputStream、PipedInputStream、AudioInputStream、StringBufferInputStream等,实现了InputStream抽象类,相当于ConcreteComponent;

    抽象装饰者:FilterInputStream,实现了抽象构件,且成为具体装饰者的父类,相当于Decorator。

    具体装饰者:FilterInputStream的子类如常用的BufferedInputStream、DataInputStream,还有其他不常用的CheckedInputStream、CipherInputStream、LineNumberInputStream、PushBackInputStream。这里其实具体的装饰者还有直接继承InputStream的ObjectInputStream、SequenceInputStream,相当于ConcreteDecorator。这里还涉及到装饰者模式的简化模型。

    这里的装饰者模式大多都是半透明的,具体装饰者提供了比抽象构件多的方法,调用者可以使用装饰者的这些方法。

    装饰者另外3种简化模型

    只有一个具体装饰者,这样就不需要抽象装饰者,具体装饰者直接继承抽象构件就可以了。


    在这里插入图片描述

    只有一个具体构件,这样就不需要抽象构件,抽象装饰者可以直接继承具体构件就可以了。


    在这里插入图片描述

    就是上面组合起来,只有一个具体构件和一个具体装饰者,这样抽象角色都不需要了,具体装饰者直接继承集体构件就可以了。


    在这里插入图片描述

    相关文章

      网友评论

          本文标题:人要衣装,佛要金装之装饰模式

          本文链接:https://www.haomeiwen.com/subject/bkvooktx.html