设计模式 基础篇01 装饰器模式



主要组件

  1. 抽象构件(Component)角色:定义一个抽象接口以规范准备接收附加责任的对象
  2. 具体构件(Concrete Component)角色:实现抽象构件,通过装饰角色为其添加一些职责。
  3. 抽象装饰(Decorator)角色:实现抽象构件,并包含具体构件的实例,可以通过其子类扩展具体构件的功能。
  4. 具体装饰(ConcreteDecorator)角色:继承抽象装饰,实现抽象装饰的相关方法,并给具体构件对象添加附加的责任

结构示意图

在这里插入图片描述


简要概括

抽象装饰要实现抽象构件类、包含抽象构件的引用A,则后续的具体装饰也是抽象构件的实现类,所以具体装饰可以通过抽象构件类的引用A来声明使用,自由互相嵌套包裹

在这里插入图片描述

另外:

在这里插入图片描述

应用场景

  • 1.切换功能
    通过基础组件的构造函数来设置不同的装饰器类,以此来切换不同的装饰器方法,实现功能的切换.
    如样例代码1,在具体装扮的基础上,切换不同的装扮
  • 2.叠加功能
    因为所有装饰器都继承于基础组件,则所有装饰器都是基础组件的子类,可以不断将前一个装饰器添加到该装饰器之中,如此,可以在执行本装饰器的功能之前,先执行前面装饰器的功能,实现功能上的叠加。
    如样例代码2,在具体装扮的基础上,一层层往上叠加装扮,即
    室内服装=冬天基础装扮+室内装扮
    室外服装=冬天基础装扮+室内装扮+室外装扮

样例代码1 切换功能

/**
 * @program: DesignPatterns
 * @description:
 * @author: TATE.LU
 * @create: 2020-12-07 16:11
 **/

public class Decorator {
    public static void main(String []args){
        Clothes clothes=new ConcreteClothes();
    //这是具体的一种装扮,实现了Clothes接口规范,可以想怎么穿,就怎么穿
        System.out.println("\n某种具体装扮----------");
        clothes.putOn();
//这是冬天的穿衣打扮父类,实现了Clothes接口规范,定义冬天的衣服最基本要穿什么
        WinterClothes winterClothes=new WinterClothes(clothes);


        System.out.println("\n\n切换成冬天基础装扮----------");
        WinterBaseClothes winterBaseClothes=new WinterBaseClothes(winterClothes);
        winterBaseClothes.putOn();



        System.out.println("\n\n切换成室内装扮-----------");
        IndoorWinterClothes indoorWinterClothes=new IndoorWinterClothes(winterClothes);
        indoorWinterClothes.putOn();


        System.out.println("\n\n切换成出门装扮!---------");
        OutdoorWinterClothes outdoorWinterClothes=new OutdoorWinterClothes(winterClothes);
        outdoorWinterClothes.putOn();
    }


}
interface Clothes{
    public void putOn();
}
class ConcreteClothes implements Clothes{
    public ConcreteClothes(){
//        System.out.println("具体的装扮");
    }

    public void putOn() {

        System.out.print("某种具体装扮");
    }
}
class WinterClothes implements Clothes{
    private Clothes clothes;
    public WinterClothes(Clothes clothes){
        this.clothes=clothes;
    }


    public void putOn() {


        if(this.clothes!=null){
            this.clothes.putOn();
        }


    }
}
class WinterBaseClothes extends WinterClothes{
    public WinterBaseClothes(Clothes clothes){
        super(clothes);
    }
    public void putOn(){
        super.putOn();
        baseDecorate();
    }

    //    基本装扮
    public void baseDecorate(){
        System.out.print("+ 保暖衣,保暖裤,鞋子");

    }


}
class IndoorWinterClothes extends WinterClothes{
    public IndoorWinterClothes(Clothes clothes){
        super(clothes);
    }

    public void putOn(){

        super.putOn();
        indoorDecorate();

    }



    //    室内装扮
    public void indoorDecorate(){
        System.out.print("+ 长袖,长裤 ");

    }
}
class OutdoorWinterClothes extends WinterClothes{
    public OutdoorWinterClothes(Clothes clothes){
        super(clothes);

    }


    public void putOn() {
        super.putOn();
        outdoorDecorate();
    }
    public void outdoorDecorate(){
        System.out.print("+ 羽绒服,围巾,帽子 ");
    }

}

运行结果

在这里插入图片描述


样例代码2 叠加功能

/**
 * @program: DesignPatterns
 * @description:
 * @author: TATE.LU
 * @create: 2020-12-07 16:11
 **/
 
public class Decorator {
    public static void main(String []args){
        Clothes clothes=new ConcreteClothes();
    //这是具体的一种装扮,实现了Clothes接口规范,可以想怎么穿,就怎么穿
        System.out.println("\n某种具体装扮----------");
        clothes.putOn();

//这是冬天的穿衣打扮父类,实现了Clothes接口规范,定义冬天的衣服最基本要穿什么
        WinterClothes winterClothes=new WinterClothes(clothes);

//加上冬天基础装扮
        System.out.println("\n\n加上冬天基础装扮----------");
        WinterBaseClothes winterBaseClothes=new WinterBaseClothes(winterClothes);
        winterBaseClothes.putOn();


//加上室内装扮
        System.out.println("\n\n加上室内装扮-----------");
        IndoorWinterClothes indoorWinterClothes=new IndoorWinterClothes(winterBaseClothes);
        indoorWinterClothes.putOn();


//加上室外装扮
        System.out.println("\n\n加上室外装扮---------");
        OutdoorWinterClothes outdoorWinterClothes=new OutdoorWinterClothes(indoorWinterClothes);
        outdoorWinterClothes.putOn();
    }
 
}
interface Clothes{
    public void putOn();
}
class ConcreteClothes implements Clothes{
    public ConcreteClothes(){
//        System.out.println("具体的装扮");
    }
 
    public void putOn() {
 
        System.out.print("某种具体装扮");
    }
}
class WinterClothes implements Clothes{
    private Clothes clothes;
    public WinterClothes(Clothes clothes){
        this.clothes=clothes;
    }
 
 
    public void putOn() {
 
 
        if(this.clothes!=null){
            this.clothes.putOn();
        }
 
 
    }
}
class WinterBaseClothes extends WinterClothes{
    public WinterBaseClothes(Clothes clothes){
        super(clothes);
    }
    public void putOn(){
        super.putOn();
        baseDecorate();
    }
 
    //    基本装扮
    public void baseDecorate(){
        System.out.print("+ 保暖衣,保暖裤,鞋子");
 
    }
 
 
}
class IndoorWinterClothes extends WinterClothes{
    public IndoorWinterClothes(Clothes clothes){
        super(clothes);
    }
 
    public void putOn(){
 
        super.putOn();
        indoorDecorate();
 
    }
 
 
 
    //    室内装扮
    public void indoorDecorate(){
        System.out.print("+ 长袖,长裤 ");
 
    }
}
class OutdoorWinterClothes extends WinterClothes{
    public OutdoorWinterClothes(Clothes clothes){
        super(clothes);
 
    }
 
 
    public void putOn() {
        super.putOn();
        outdoorDecorate();
    }
    public void outdoorDecorate(){
        System.out.print("+ 羽绒服,围巾,帽子 ");
    }
 
}

运行结果
在这里插入图片描述


参考链接

大话设计模式 BY 程杰
C语言中文网

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值