设计模式-装饰器模式

参考:https://blog.csdn.net/tang_seven/article/details/128977529

概述

  • 装饰器模式,指在不改变原有对象结构的基础情况下,动态地给该对象增加一些额外功能的职责。装饰器模式相比生成子类更加灵活。它属于对象结构型模式。
  • 装饰器模式强调自身功能的扩展,是代理模式的一个特殊应用。
  • 简单来说,就是一个物件,然后不断在该物件上添加符合该物件的功能,进行扩展。以生活中的例子为例:
    我们点一个快餐,然后炒饭是快餐中的一种。我们以该炒饭为对象物件,在其中加蛋、加肉、加火腿等等(即进行扩展),使得炒饭变得更加美味(功能强大)。
  • 装饰器模式的目标是使用组合关系来创建一个包装对象(即装饰对象)来包裹真实对象,并在保持真实对象的类结构不变的前提下,为其提供额外的功能。

优缺点

优点

  • 装饰器模式可拓展性强,在不改变原有对象的情况下,动态的给一个对象扩展功能,即插即用。同时,易于维护,哪个功能出问题直接找相关代码即可。
  • 通过使用不用装饰类及这些装饰类的排列组合,可以实现不同效果。

缺点

  • 装饰器模式会增加许多子类,会使得代码变得繁多增加程序的复杂性

装饰器结构

  • 抽象构件(Component):定义一个抽象接口以规范准备接收附加责任的对象。
  • 具体构件(ConcreteComponent)角色:实现抽象构件,通过装饰角色为其添加一些职责。
  • 抽象装饰(Decorator):继承抽象构件,并包含具体构件的实例,可以通过其子类扩展具体构件的功能。
  • 具体装饰(ConcreteDecorator):实现抽象装饰的相关方法,并给具体构件对象添加附加的责任。
    首先需要有一个Component,规定类别、行为。然后需要有一个ConcreteComponent继承该Component,并规定为具体的物件。接着是一个Decorator,同样继承该Component,同时在该类中传入Component对象进行静态代理。最后是多个ConcreteDecorator继承自Decorator,实现功能的拓展。

代码示例

创建Component对象,规定为Tree,行为功能为输出名字和输出价格:

public abstract class Tree {
    protected abstract  String  getMessage();
 
    protected abstract int getPrice();
}

创建ConcreteComponent,规定为圣诞树:

public class ChristmasTree extends Tree{
    @Override
    protected String getMessage() {
        return "这是一颗圣诞树";
    }
 
    @Override
    protected int getPrice() {
        return 50;
    }
}

创建Decorator对象,进行静态代理

public abstract class ChristmasTreeDecorator extends Tree{
    //静态代理
    private final Tree tree;
 
    public ChristmasTreeDecorator(Tree tree){
        this.tree=tree;
    }
 
    @Override
    protected String getMessage() {
        return this.tree.getMessage();
    }
 
    @Override
    protected int getPrice() {
        return this.tree.getPrice();
    }
}

创建多个ConcreteDecorator对象,实现功能扩展(加装饰球、加缎带)

public class BallDecorator extends ChristmasTreeDecorator{
    public BallDecorator(Tree tree) {
        super(tree);
    }
 
    @Override
    protected String getMessage() {
        return super.getMessage()+ ", 加了装饰球";
    }
 
    @Override
    protected int getPrice() {
        return super.getPrice()+20;
    }
}
public class RibbonDecorator extends ChristmasTreeDecorator{
    public RibbonDecorator(Tree tree) {
        super(tree);
    }
    @Override
    protected String getMessage() {
        return super.getMessage()+ ", 加了缎带";
    }
 
    @Override
    protected int getPrice() {
        return super.getPrice()+10;
    }
}

测试:

public class test {
    public static void main(String[] args) {
        System.out.println("===来一棵圣诞树===");
        Tree christmasTree = new ChristmasTree();
        System.out.println(christmasTree.getMessage() + ",总价格:" + christmasTree.getPrice() + "元。");
 
        System.out.println("===加些圣诞球吧===");
        christmasTree = new BallDecorator(christmasTree);
        System.out.println(christmasTree.getMessage() + ",总价格:" + christmasTree.getPrice() + "元。");
 
        System.out.println("===再加些缎带吧===");
        christmasTree = new RibbonDecorator(christmasTree);
        System.out.println(christmasTree.getMessage() + ",总价格:" + christmasTree.getPrice() + "元。");
 
 
    }
}
posted @ 2023-11-16 16:54  hasome  阅读(10)  评论(0编辑  收藏  举报