装饰者模式

  1. 1. 装饰者模式Overview
  2. 2. 使用场景
  3. 3. 使用目的
  4. 4. 具体实现
    1. 4.1 接口和数据对象定义
    2. 4.2 具体实现类的定义
    3. 4.3 测试
  5. Reference

1. 装饰者模式Overview

  • Attach additional responsibilities to an object dynamically. Decorator provides a flexible alternative to subclassing for extending functionality.
    • inheriting behavior at runtime through composition and delegation
    • open for extension but close for modification
    • decorator has the same super type as the objects they decorate
    • we could use one or more decorators to wrap an object
    • the decorator adds its own behavior before and/or after delegating to the object it decorates to do the rest of the job

2. 使用场景

  • when we want to give some other responsibility at run time
  • kind of like continue to wrap the intrinsic object with some new responsibility, still an is-a relationship

3. 使用目的

  • Extends the class at run time instead of compile time
  • To give objects some new responsibilities without making any code changes to the underlying classes
  • pitfall
    • sometimes it could lead to a lot of new classes

4. 具体实现

  • One highlight

    • the condimentDecorator is a beverage, and also need to has a beverage

    • we are using inheritance to achieve the type matching, but we aren’t using inheritance to get behavior

      Class Diagram

4.1 接口和数据对象定义

public abstract class Beverage {
    String description = "Unknown Beverage";

    public abstract double cost();

    public String getDescription() {
        return description;
    }
}

public abstract class CondimentDecorator extends Beverage{

    Beverage beverage;

    public abstract String getDescription();
}

4.2 具体实现类的定义

public class DecafCoffee extends Beverage {
    @Override
    public double cost() {
        return 2.5f;
    }

    String description() {
        return "Decat coffee";
    }
}

public class Espresso extends Beverage{

    public Espresso() {
        description = "Espresso";
    }
    @Override
    public double cost() {
        return 4;
    }
}

public class HouseBlend extends Beverage{

    public HouseBlend() {
        description = "House Blend Coffee";
    }

    @Override
    public double cost() {
        return 0.99;
    }
}

public class MilkDecorator extends CondimentDecorator{

    public MilkDecorator(Beverage beverage) {
        this.beverage = beverage;
    }

    @Override
    public double cost() {
        return beverage.cost() + 0.8;
    }

    @Override
    public String getDescription() {
        return beverage.getDescription() + "with milk";
    }
}

public class MochaDecorator extends CondimentDecorator{

    public MochaDecorator(Beverage beverage) {
        this.beverage = beverage;
    }

    @Override
    public double cost() {
        return beverage.cost() + 0.5;
    }

    @Override
    public String getDescription() {
        return beverage.getDescription() + "with Mocha";
    }
}

4.3 测试

@GetMapping("/decorator")
    public String decorator() {
        Beverage decafCoffee = new DecafCoffee();
        log.info("before decoration" + decafCoffee.cost());

        Beverage decafWithMocha = new MochaDecorator(decafCoffee);
        log.info("after decoration" + decafWithMocha.cost());
        log.info("after decoration 2" + new MochaDecorator(new MochaDecorator(decafCoffee)).cost());

        return "check the log";
    }

Reference

  1. https://learning.oreilly.com/library/view/head-first-design/9781492077992/ch02.html

转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。可以在下面评论区评论,也可以邮件至 stone2paul@gmail.com

文章标题:装饰者模式

文章字数:478

本文作者:Leilei Chen

发布时间:2022-09-19, 22:48:01

最后更新:2022-09-19, 22:49:32

原始链接:https://www.llchen60.com/%E8%A3%85%E9%A5%B0%E8%80%85%E6%A8%A1%E5%BC%8F/

版权声明: "署名-非商用-相同方式共享 4.0" 转载请保留原文链接及作者。

目录
×

喜欢就点赞,疼爱就打赏