设计模式(十一)装饰器模式

版权声明:转载必须注明本文转自晓_晨的博客:http://blog.csdn.net/niunai112

目录

导航

设计模式之六大设计原则
设计模式(一)单例模式
设计模式(二)工厂模式
设计模式(三)策略模式
设计模式(四)适配器模式
设计模式(五)享元模式
设计模式(六)建造者模式
设计模式(七)原型模式
设计模式(八)桥接模式
设计模式(九)外观模式
设计模式(十)组合模式
设计模式(十一)装饰器模式
设计模式(十二)代理模式
设计模式(十三)迭代器模式
设计模式(十四)观察者模式
设计模式(十五)中介者模式
设计模式(十六)命令模式
设计模式(十七)状态模式
设计模式(十八)访问者模式
设计模式(十九)责任链模式
设计模式(二十)解释器模式
设计模式(二十一)备忘录模式
设计模式(二十二)模板模式
设计模式总结篇(为什么要学习设计模式,学习设计模式的好处)

前言

一个诠释了对扩展开发,对修改关闭原则的设计模式,一个类需要新增行为时,使用装饰器模式,可以在不改变原类的基础上,完成行为的增加。

例子

远古时候的人是没穿衣服的,但是到了后来,社会形成,物质条件好了后,人们会开始穿衣服出门。可以把穿衣服写成一个装饰器模型。

/***
 *
 *@Author ChenjunWang
 *@Description:
 *@Date: Created in 17:25 2018/3/30
 *@Modified By:
 *
 */
public interface IHuman {
    void show();
}

/***
 *
 *@Author ChenjunWang
 *@Description:
 *@Date: Created in 17:40 2018/3/30
 *@Modified By:
 *
 */
public class Human implements IHuman {
    @Override
    public void show() {
        System.out.println("一个没穿衣服的人。");
    }
}

/***
 *
 *@Author ChenjunWang
 *@Description:
 *@Date: Created in 18:31 2018/3/30
 *@Modified By:
 *
 */
public abstract class HumanDecorator implements IHuman {
    protected IHuman human;

    public HumanDecorator(IHuman human){

        this.human = human;
    }
    @Override
    public void show() {

        human.show();
    }
}
/***
 *
 *@Author ChenjunWang
 *@Description:
 *@Date: Created in 17:53 2018/3/30
 *@Modified By:
 *
 */
public class WearHuman extends HumanDecorator{

    public WearHuman(IHuman human){

        super(human);
    }
    public void wear(){
        System.out.println("穿上了衣服");
    }
    public void show(){
        human.show();
        wear();
    }

}

public class Test {
    public static void main(String[] args) {
        Human human = new Human();
        System.out.println("-------------------远古时候的人,出去的时候---------------------");
        human.show();
        System.out.println("-------------------后来有衣服的时候,出去的时候---------------------");
        WearHuman wearHuman = new WearHuman(human);
        wearHuman.show();
    }
}
运行结果如下
--------------------

-------------------远古时候的人,出去的时候---------------------
一个没穿衣服的人。
-------------------后来有衣服的时候,出去的时候---------------------
一个没穿衣服的人。
穿上了衣服

装饰器模式是为了解决,当现有的接口无法满足业务需求时的补救的模式,是一个非常灵活的模式,它能在为一个方法的前后进行包装,不仅如此,它能无限进行包装,当业务再次发生变动时,能很快的进行调整,比如后来,人们的物质生活水平提高后,人们出门会带饰品。只需要增加一个饰品的装饰类就可以了。

/***
 *
 *@Author ChenjunWang
 *@Description:
 *@Date: Created in 18:27 2018/3/30
 *@Modified By:
 *
 */
public class JewelleryHuman extends HumanDecorator {

    public JewelleryHuman(IHuman human){

        super(human);
    }
    public void wearJewl(){
        System.out.println("带上饰品");
    }
    @Override
    public void show() {

        human.show();
        wearJewl();

    }
}


/***
 *
 *@Author ChenjunWang
 *@Description:
 *@Date: Created in 18:15 2018/3/30
 *@Modified By:
 *
 */
public class Test {
    public static void main(String[] args) {

        Human human = new Human();
        System.out.println("-------------------远古时候的人,出去的时候---------------------");
        human.show();
        System.out.println("-------------------后来有衣服的时候,出去的时候---------------------");
        WearHuman wearHuman = new WearHuman(human);
        wearHuman.show();

        System.out.println("-------------------再后来物质条件好的时候,出去的时候---------------------");
        JewelleryHuman jewelleryHuman = new JewelleryHuman(wearHuman);
        jewelleryHuman.show();
    }
}
运行结果如下
--------------------
-------------------远古时候的人,出去的时候---------------------
一个没穿衣服的人。
-------------------后来有衣服的时候,出去的时候---------------------
一个没穿衣服的人。
穿上了衣服
-------------------再后来物质条件好的时候,出去的时候---------------------
一个没穿衣服的人。
穿上了衣服
带上饰品

总结

优点

(1)扩展对象功能,比继承灵活,不会导致类个数急剧增加
(2)可以根据业务快速的在原有类上进行调整。

缺点

(1)利用装饰器模式,常常造成设计中有大量的小类,数量实在太多,可能会造成使用此API程序员的困扰。

Git地址

本篇实例Github地址:https://github.com/stackisok/Design-Pattern/tree/master/src/decorator

回到最上方


有什么不懂或者不对的地方,欢迎留言。
喜欢LZ文章的小伙伴们,可以关注一波,也可以留言,LZ会回你们的。
觉得写得不错的小伙伴,欢迎转载,但请附上原文地址,谢谢^_^!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值