【java--设计模式】装饰模式Decorator

  • 目的: 动态的给一个对象添加一些额外的职责。
  • 就增加功能而言,Decorator模式比生成子类更加灵活(在不改变原有对象的基础上,将功能附加到对象上,符合开闭原则,是比继承更有弹性的替代方案)
  • 优点:
    装饰类和被装饰类可以独立发展,不会相互耦合,装饰模式是继承的一个替代模式,装饰模式可以动态扩展一个实现类的功能
    符合开闭原则
  • 缺点: 多层装饰较为复杂,可读性差

【java--设计模式】装饰模式Decorator_第1张图片

  • Component(被装饰对象基类):定义对象的接口,可以给这些对象动态的增加职责
  • ConcreteComponent(具体被装饰对象):定义具体的对象,Decorator可以给它增加额外的职责
  • Decorator(装饰者抽象类):维护指向Component实例的引用,定义与Component一致的接口(也就是继承或实现Component)
  • ConcreteDecorator(具体装饰者):给内部特有的具体被装饰对象增加具体的职责

例子:
【java--设计模式】装饰模式Decorator_第2张图片


Component(被装饰对象基类):
有价格、名字,这两个方法

package decorator;

public interface Drink {
    public double price();
    public String name();
}

ConcreteComponent(具体被装饰对象):

package decorator;

//具体被装饰类的对象:Beer
public class Beer implements Drink {

    @Override
    public double price() {
        return 2.00;
    }

    @Override
    public String name() {
        return "啤酒";
    }
}

package decorator;
//具体被装饰类的对象:可乐
public class Cola implements Drink{
    @Override
    public String name() {
        return "可乐";
    }

    @Override
    public double price() {
        return 1.00;
    }
}

Decorator(装饰者抽象类):

package decorator;

//装饰器抽象类(基类):
public class Decorator implements Drink {

    //声明时用Drink,方便扩展
    protected Drink drink;

    public Decorator(Drink drink){
        this.drink = drink;
    }

    @Override
    public double price() {
        return drink.price();
    }

    @Override
    public String name() {
        return drink.name();
    }
}

ConcreteDecorator(具体装饰者):

package decorator;

//具体的装饰者:青岛啤酒
public class QingdaoDecorator extends Decorator {

    public QingdaoDecorator(Drink drink){
        super(drink);
    }

    @Override
    public double price() {
        return drink.price() + 4.00;
    }

    @Override
    public String name() {
        return "青岛" + drink.name();
    }
}

package decorator;
//燕京啤酒
public class YanjingDecorator extends Decorator {
    public YanjingDecorator(Drink drink){
        super(drink);
    }

    @Override
    public double price() {
        return drink.price() + 2.00;
    }

    @Override
    public String name() {
        return "燕京" + drink.name();
    }
}

package decorator;
//百威啤酒
public class BaiweiDecorator extends Decorator{
    public BaiweiDecorator(Drink drink){
        super(drink);
    }

    @Override
    public double price() {
        return drink.price() + 3.00;
    }

    @Override
    public String name() {
        return "百威" + drink.name();
    }
}

package decorator;
//百事可乐
public class PepsiDecorator extends Decorator{
    public PepsiDecorator(Drink drink){
        super(drink);
    }

    @Override
    public String name() {
        return "百事" + drink.name();
    }

    @Override
    public double price() {
        return drink.price() + 2.00;
    }
}

测试:

package decorator;

public class Test {
    public static void main(String[] args) {
        Drink beer = new Beer();
        QingdaoDecorator qingdaoDecorator = new QingdaoDecorator(beer);
        System.out.println(qingdaoDecorator.name() + qingdaoDecorator.price());//青岛啤酒6.0

        BaiweiDecorator baiweiDecorator = new BaiweiDecorator(beer);
        System.out.println(baiweiDecorator.name() + baiweiDecorator.price());//百威啤酒5.0

        YanjingDecorator yanjingDecorator = new YanjingDecorator(beer);
        System.out.println(yanjingDecorator.name() + yanjingDecorator.price());//燕京啤酒4.0


        Drink cola = new Cola();
        PepsiDecorator pepsiDecorator = new PepsiDecorator(cola);
        System.out.println(pepsiDecorator.name() + pepsiDecorator.price());//百事可乐3.0
    }
}

你可能感兴趣的:(Java)