要求在扩展新的咖啡种类时,具有良好的扩展性、改动方便、维护方便
使用 OO 的来计算不同种类咖啡的费用: 客户可以点单品咖啡,也可以单品咖啡+调料组合。
问题:这样设计,会有很多类,当我们增加一个单品咖啡,或者一个新的调料,类的数量就会倍增,就会出现类爆炸
前面分析到方案1,因为咖啡单品+调料组合会造成类的倍增,因此可以做改进,将调料内置到 Drink 类,这样就不会造成类数量过多。从而提高项目的维护性(如图)
说明:milk、soy、chocolate可以设计为Boolean,表示是否要添加相应的调料。
方案2可以控制类的数量,不至于造成很多的类
在增加或者删除调料种类时,代码的维护量很大
考虑到用户可以添加多份调料时,可以将 hasMilk 返回一个对应 int
考虑使用装饰者模式
装饰者模式:动态的将新功能附加到对象上。在对象功能扩展方面,它比继承更有弹性,装饰者模式也体现了开闭原则(ocp)
这里提到的动态的将新功能附加到对象和 ocp 原则,在后面的应用实例上会以代码的形式体现。
说明:
说明:
package com.atguigu.decorator;
/**
* 具体的Decorator,这里就是调味品
*
* @author usami
*
*/
public class Chocolate extends Decorator {
/**
* @param obj
*/
public Chocolate(Drink obj) {
super(obj);
// TODO Auto-generated constructor stub
super.setDes("巧克力");
super.setPrice(3.0F);
}
}
package com.atguigu.decorator;
/**
* @author usami
*
*/
public class Coffee extends Drink {
@Override
public float cost() {
// TODO Auto-generated method stub
return super.getPrice();
}
}
package com.atguigu.decorator;
/**
* @author usami
*
*/
public class Decorator extends Drink {
/**
* 单品咖啡的价格
*/
private Drink obj;
public Decorator(Drink obj) {
this.obj = obj;
}
@Override
public float cost() {
// TODO Auto-generated method stub
// 自己的价格 + 单品咖啡的价格
return super.getPrice() + obj.cost();
}
@Override
public String getDes() {
// TODO Auto-generated method stub
// obj.getDes():输出被装饰者的信息
return String.format("%s %f && %s", super.des, super.getPrice(), obj.getDes());
}
}
package com.atguigu.decorator;
/**
* @author usami
*
*/
public abstract class Drink {
/**
* 描述
*/
public String des;
/**
* 价格
*/
private float price = 0.0f;
public String getDes() {
return des;
}
public void setDes(String des) {
this.des = des;
}
public float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
@Override
public String toString() {
return "Drink [des=" + des + ", price=" + price + "]";
}
/**
* 计算价格的抽象方法
*
* @return
*/
public abstract float cost();
}
package com.atguigu.decorator;
/**
* @author usami
*
*/
public class Espresso extends Coffee {
public Espresso() {
// TODO Auto-generated constructor stub
this.setDes("意大利咖啡");
this.setPrice(6.0F);
}
}
package com.atguigu.decorator;
/**
* @author usami
*
*/
public class LongBlack extends Coffee {
public LongBlack() {
// TODO Auto-generated constructor stub
this.setDes("美式咖啡");
this.setPrice(5.0F);
}
}
package com.atguigu.decorator;
public class Milk extends Decorator {
public Milk(Drink obj) {
super(obj);
// TODO Auto-generated constructor stub
super.setDes("牛奶");
super.setPrice(2.0F);
}
}
package com.atguigu.decorator;
/**
* @author usami
*
*/
public class ShortBlack extends Coffee {
/**
*
*/
public ShortBlack() {
// TODO Auto-generated constructor stub
this.setDes("ShortBlack");
this.setPrice(4.0F);
}
}
package com.atguigu.decorator;
/**
* @author usami
*
*/
public class Soy extends Decorator {
/**
* @param obj
*/
public Soy(Drink obj) {
super(obj);
// TODO Auto-generated constructor stub
super.setDes("豆浆");
super.setPrice(1.5F);
}
}
package com.atguigu.decorator;
/**
* @author usami
*
*/
public class CoffeeBar {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
// 2份巧克力+1份牛奶的LongBlack
// 1.点一份LongBlack
Drink order = new LongBlack();
System.out.println("费用1=" + order.cost());
System.out.println("描述=" + order.getDes());
// 2.order中加一份牛奶
order = new Milk(order);
System.out.println("order 加一份牛奶 费用=" + order.cost());
System.out.println("order 加一份牛奶 描述=" + order.getDes());
// 3.order加一份巧克力
order = new Chocolate(order);
System.out.println("order 加一份牛奶 加一份巧克力 费用=" + order.cost());
System.out.println("order 加一份牛奶 加一份巧克力 描述=" + order.getDes());
// 3.order加两份巧克力
order = new Chocolate(order);
System.out.println("order 加一份牛奶 加两份巧克力 费用=" + order.cost());
System.out.println("order 加一份牛奶 加两份巧克力 描述=" + order.getDes());
}
}
Java 的 IO 结构,FilterInputStream 就是一个装饰者
/**
* This abstract class is the superclass of all classes representing
* an input stream of bytes.
*
* <p> Applications that need to define a subclass of <code>InputStream</code>
* must always provide a method that returns the next byte of input.
*
* @author Arthur van Hoff
* @see java.io.BufferedInputStream
* @see java.io.ByteArrayInputStream
* @see java.io.DataInputStream
* @see java.io.FilterInputStream
* @see java.io.InputStream#read()
* @see java.io.OutputStream
* @see java.io.PushbackInputStream
* @since JDK1.0
*/
public abstract class InputStream implements Closeable {
/**
* A <code>FilterInputStream</code> contains
* some other input stream, which it uses as
* its basic source of data, possibly transforming
* the data along the way or providing additional
* functionality. The class <code>FilterInputStream</code>
* itself simply overrides all methods of
* <code>InputStream</code> with versions that
* pass all requests to the contained input
* stream. Subclasses of <code>FilterInputStream</code>
* may further override some of these methods
* and may also provide additional methods
* and fields.
*
* @author Jonathan Payne
* @since JDK1.0
*/
public
class FilterInputStream extends InputStream {
/**
* The input stream to be filtered.
*/
protected volatile InputStream in;
代码追踪
package com.atguigu.decorator;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
/**
* @author usami
*
*/
public class DecoratorTest {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
// 说明
// 1. InputStream 是抽象类, 类似我们前面讲的 Drink
// 2. FileInputStream 是 InputStream 子类,类似我们前面的 DeCaf, LongBlack
// 3. FilterInputStream 是 InputStream 子类:类似我们前面 的 Decorator 修饰者
// 4. DataInputStream 是 FilterInputStream 子类,具体的修饰者,类似前面的 Milk, Soy 等
// 5. FilterInputStream 类 有 protected volatile InputStream in; 即含被装饰者
// 6. 分析得出在 jdk 的 io 体系中,就是使用装饰者模式
DataInputStream dis = new DataInputStream(new FileInputStream("~/Documents/abc.txt"));
System.out.println(dis.read());
dis.close();
}
}
原文:https://www.cnblogs.com/iamfatotaku/p/14612315.html