博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
设计模式——装饰器模式(附java源码案例)
阅读量:4149 次
发布时间:2019-05-25

本文共 2456 字,大约阅读时间需要 8 分钟。

装饰器模式,其实又叫做包装模式,其主要作用就是将附加的功能添加到别的类当中,让这个类实现其包含的类的方法,在具体的调用时,就会按照包装顺序进行调用。

Compoent

   提供一个统一接口,是装饰类和被装饰类的基础类

ConcreteComponent

  具体的实现类,对于接口当中的方式进行实现

Decorator

    继承了Componet接口,同时包含了一个Component接口对象,对于Component当中抽象方法的实现,调用的是,具体传递进来的componet方法,Decorator本身通过采用默认实现,他的存在仅仅是一个声明,只是为了生产出来一些用于装饰的子类,其子类才是具有装饰效果的类。

ConcreteDecoratorA

  具体的装饰类,继承自Decorator,实现了decorator当中的方法,同时提供了自己的一些方法。

public interface Component {    void  doSomeThing();}
public class ConcreteComponent implements Component {    @Override    public void doSomeThing() {        System.out.println("功能A");    }}

主要是对于装饰类Decorator,不做任何多余的方法处理,只是调用传递进来接口的方法

public class Decorator implements Component {    private Component component;    public  Decorator(Component component){        this.component = component;    }    @Override    public void doSomeThing() {        component.doSomeThing();    }}

具体的装饰类实现

public class ConcreteDecorator extends Decorator {    public ConcreteDecorator(Component component) {        super(component);    }    @Override    public void doSomeThing(){        super.doSomeThing();        this.doAnthorThing();    }    public void doAnthorThing(){        System.out.println("功能B!");    }}

装饰类二

public class ConcreteDecorator1 extends Decorator {    public ConcreteDecorator1(Component component) {        super(component);    }    @Override    public void doSomeThing() {        super.doSomeThing();        this.doAnthorThing();    }    public void doAnthorThing() {        System.out.println("功能C");    }}

接下来我们来看下测试代码

public class Client {    public static void main(String[] args)throws Exception {        Component component = new ConcreteComponent();        component.doSomeThing();        System.out.println("------------------");        Component component1 = new ConcreteDecorator(new ConcreteComponent());        component1.doSomeThing();        System.out.println("------------------");        Component component2 = new ConcreteDecorator1(new ConcreteDecorator(new ConcreteComponent()));        component2.doSomeThing();    }}执行结果功能A------------------功能A功能B!------------------功能A功能B!功能C

上述就是装饰器模式的一个大概实现我们对照来看下java IO包当中的实现

InputStream就是我们所说的Component角色对象,FileInputStream就是对应的一个ConcreteComponent角色,FilterInputStream就是我们所说的Decorator角色,继承了Component角色,同时持有了一个Component对象,FilterInputStream也正如我们所介绍的,其方法当中的实现,基本上都是调用传入进来的Component接口的实现,它本身并不做装饰性操作,主要是让其子类去实现装饰性的工作。而BufferedInputStream就是ConcreteDecorator角色,其中也是持有了Component对象的。

下面这篇文章具体讲述了为什么要使用装饰器模式,以及在自己的项目当中如何使用装饰器模式

你可能感兴趣的文章
程序员真的是吃青春饭吗?面试官6个灵魂拷问,分享一点面试小经验
查看>>
程序员进阶!Android黑科技保活实现原理揭秘,分享一点面试小经验
查看>>
算法太TM重要了!2021年Android春招面试经历,这原因我服了
查看>>
经典好文!想找工作的你还不看这份资料就晚了!快来收藏!
查看>>
经验分享:掌握这套精编Android高级面试题解析,跳槽薪资翻倍
查看>>
美团安卓面试,程序员如何自我学习和成长?先收藏了
查看>>
老师讲的真棒!你的技术真的到天花板了吗?不吃透都对不起自己
查看>>
腾讯T2亲自讲解!你有过迷茫吗?系列篇
查看>>
腾讯T2大牛亲自讲解!6年老Android面经总结,面试真题解析
查看>>
BTAJ面试有关散列(哈希)表的面试题详解,成功入职腾讯
查看>>
Context都没弄明白凭什么拿高薪?附小技巧
查看>>
databinding双向绑定,带你玩转自定义view系列,先收藏了
查看>>
flutter开发工具,一篇文章教你搞定计算机网络面试,吐血整理
查看>>
flutter开发桌面应用,如何才能通过一线互联网公司面试?已开源
查看>>
flutter技术入门与实战!妈妈再也不用担心我的面试,隔壁都馋哭了
查看>>
Flutter最新开源框架,已拿到offer
查看>>
flutter音视频开发,小程序FMP优化实录,已拿offer入职
查看>>
Github标星25K+超火的Android实战项目,Android篇
查看>>
Github标星25K+超火的Android实战项目,帮你突破瓶颈
查看>>
retrofit教程,HTTPS面试常问全解析,真香
查看>>