Java 设计模式(3) —— 装饰者模式

一、装饰者模式
将原始对象进行装饰封装,动态的将新功能附加到对象上。在对象功能扩展方面,它比继承更有弹性

二、示例
咖啡馆订单项目,咖啡馆的咖啡种类有多种,可以添加的配料也有多种,不同的咖啡可以与配料进行任意搭配组合下单。要得到组合出的饮料价格和描述。
1.传统模式
1)、咖啡种类:Espresso、ShortBlack、LongBlack、Decaf
2)、调料:Milk、Soy、Chocolate
从面向对象的角度设计,封装一个饮料的对象,附上所有的属性(咖啡种类和配料),以及方法,通过不同的对象进行继承,每一个对象根据不同的属性值重写必要的方法(计算价格,获取咖啡描述)
1.饮料对象
/**

  • 饮料的超类,封装基本方法和定义抽象方法

*/
public abstract class Drink {

public String description="";
private float price=0f;
public void setDescription(String description)
{
    this.description=description;
}
public String getDescription()
{
    return description+"-"+this.getPrice();
}
public float getPrice()
{
    return price;
}
public void setPrice(float price)
{
    this.price=price;
}
public abstract float cost();

}
传统模式存在的问题:

若新增一种咖啡种类或者调料需要将每一个对象都接入新的组合
若一个调料添加多次,则无法实现
2.使用装饰者模式后

装饰者模式就像打包一个快递

1)、主体:陶瓷、衣服

2)、包装:报纸填充、塑料泡沫、纸板、木板

1、定义装饰者类:

/**

  • 自定义装饰者,也继承饮料的超类

*/
public class Decorator extends Drink {

private Drink Obj;
// 自身引入超类的对象
public Decorator(Drink Obj) {
    this.Obj = Obj;
}
// 自定义方法,可利用递归计算价格的总和
@Override
public float cost() {
    return super.getPrice() + Obj.cost();
}
@Override
public String getDescription() {
    return super.description + "-" + super.getPrice() + "&&" + Obj.getDescription();
}

}
2、被装饰者继承装饰者类

public class Chocolate extends Decorator {

public Chocolate(Drink Obj) {      
    super(Obj);
    super.setDescription("Chocolate");
    super.setPrice(3.0f);
}

}
3、被装饰过得对象则可以任意组合搭配

    Drink order;
    order=new Decaf();
    // 任意搭配,自定义添加组合方式
    // 选好一份咖啡后添加一次牛奶,两次巧克力
    // 用装饰者将对象包装起来,在装饰者内自定义新的方法逻辑,不影响老的对象
    order=new LongBlack();
    order=new Milk(order);
    order=new Chocolate(order);
    order=new Chocolate(order);

三、Java内置的装饰者

1、Java内典型的装饰者案例

InputStream
FilterInputStream
2、自定义一层IO流内的装饰者

/**

  • 自定义IO流的一层装饰者,重新定义read方法,将读取到的数据转成大写
  • 装饰之后方法可任意嵌套,灵活修改

*/
public class UpperCaseInputStream extends FilterInputStream {

protected UpperCaseInputStream(InputStream in) {
    super(in);
}
public int read() throws IOException {
    int c = super.read();
    return c == -1 ? c : Character.toUpperCase((char) (c));
}
public int read(byte[] b, int offset, int len) throws IOException {
    int result = super.read(b, offset, len);
    for (int i = 0; i < result; i++) {
        b = (byte) Character.toUpperCase((char) (b));
    }
    return result;
}

}
四、总结
通过装饰者模式可以实现对象之间的解耦
将对象进行装饰包装,当有新的需求时可直接在装饰的对象上进行操作,自定义性强,灵活
开放 — 关闭原则的设计意义,开放新的装饰者方法,关闭内部单独的模块对象

Java设计模式所有示例代码,持续更新中

评论

暂无评论