装饰者模式

装饰者模式:动态地将责任附加到对象上,如果要扩展功能,装饰者提供了比继承更具有弹性的替代方案。

[]类图

这里写图片描述

[]装饰者模式的实现

抽象基类

public abstract class Beverage {

protected String description = "Unknown Beverage";

public String getDescription() {
    return description;
}

public abstract double cost();

}

具体被装饰类

public class Espresso extends Beverage {

public Espresso() {
    description = "Espresso";
}

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

具体被装饰类

public class DarkRoast extends Beverage {

public DarkRoast() {
    description = "DarkRoast";
}

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

具体被装饰类

public class HouseBlend extends Beverage {

public HouseBlend() {
    description = "HouseBlend";
}

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

抽象装饰类

public abstract class CondimentDecorator extends Beverage {

public abstract String getDescription();

}

具体装饰类

public class Mocha extends CondimentDecorator {

private Beverage beverage;

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

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

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

具体装饰类

public class Whip extends CondimentDecorator {

private Beverage beverage;

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

@Override
public String getDescription() {
    return beverage.getDescription() + ", Whip";
}

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

具体装饰类

public class Soy extends CondimentDecorator {

private Beverage beverage;

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

@Override
public String getDescription() {
    return beverage.getDescription() + ", Soy";
}

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

测试

public static void main(String args[]) {

    Beverage espresso = new Espresso();
    System.out.println(espresso.getDescription() + " $" + espresso.cost());

    Beverage darkRoast = new DarkRoast();
    darkRoast = new Mocha(darkRoast);
    darkRoast = new Mocha(darkRoast);
    darkRoast = new Whip(darkRoast);
    System.out.println(darkRoast.getDescription() + " $" + darkRoast.cost());

    Beverage houseBlend = new HouseBlend();
    houseBlend = new Soy(houseBlend);
    houseBlend = new Mocha(houseBlend);
    houseBlend = new Whip(houseBlend);
    System.out.println(houseBlend.getDescription() + " $" + houseBlend.cost());
}

[]

()装饰者和被装饰者对象有相同的超类型。
()可以用一个或者多个装饰者包装一个对象。
()既然装饰者和被装饰者对象有相同的超类型,所以在任何需要被包装的场合,可以用装饰过的对象代替它。
()装饰者可以在所委托被装饰者的行为之前或者之后。加上自己的行为,以达到特定的目的。
()对象可以在任何时候被装饰,所以可以在运行时动态地,不限量地用你喜欢的装饰者来装饰对象。
()使用装饰者模式,导致需要管理的类数量增多,容易导致装饰类时出现装饰错误。可以利用工厂模式和生成器模式来解决这个问题。
()jdk中的io包中就有使用装饰者模式,具体可看io包中类的设计。

InputStream in = new FileInputStream("");
in = new BufferedInputStream(in);
int i = in.read();
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值