坂田网站建设费用明细,江西网站icp备案注销,wordpress文章怎么备份,全国做网站找哪家好文章目录一、引入二、装饰器模式2.1 Intent 意图2.2 Applicability 适用性2.3 类图2.4 优缺点2.5 应用实例#xff1a;Java IO 类2.6 应用实例#xff1a;咖啡馆订购系统一、引入 咖啡馆订购系统 Initial 初始 4 种咖啡 House blend (混合咖啡)Dark Roast (深度烘培)Decaf (…
文章目录一、引入二、装饰器模式2.1 Intent 意图2.2 Applicability 适用性2.3 类图2.4 优缺点2.5 应用实例Java IO 类2.6 应用实例咖啡馆订购系统一、引入 咖啡馆订购系统 Initial 初始 4 种咖啡 House blend (混合咖啡)Dark Roast (深度烘培)Decaf (低咖啡因咖啡)Espresso (意式浓缩咖啡) 需求变更客户可以加料咖啡、牛奶、糖等 steamed milksoymochaWhip
使用继承类爆炸 NO 修改父类Beverage 需求倘若变更料种类增加、料价格变化需要修改 Beverage 类
Classes should be open for extension, but closed for modification开放封闭原则对修改封闭
No Decorator…
二、装饰器模式 akaWrapper (包装器) 2.1 Intent 意图
Attach additional responsibilities to an object dynamically. 可动态地将其他职责附加到对象上.Decorators provide a flexible alternative to subclassing for extending functionality. 装饰器为扩展功能提供了一种替代子类化的灵活替代方案. Dynamically extensionBetter than inheritance
2.2 Applicability 适用性
To add responsibilities to individual objects dynamically without affecting other object.When extension by subclassing is impractical. 当子类化扩展是不切实际的. 如子类数量爆炸
2.3 类图 Component: defines the interface for objects that can have responsibilities added to them dynamically. 为可以动态地添加职责的对象定义接口ConcreteComponent: defines an object to which additional responsibilities can be attached. 定义一个可以附加其他责任的对象.Decorator: maintains a reference to a Component object and defines an interface that conforms to Component’s interface. 维护对 Component 对象的引用并定义一个符合 Component 接口的接口ConcreteDecorator: adds responsibilities to the component. 向组件添加职责
2.4 优缺点
优点 More flexibility than static inheritance. 比静态继承更灵活 With Decorators, responsibilities can be added and removed at runtime simply by attaching and detaching them. 避免类爆炸通过排列和组合可以创建许多行为组合装饰类和被装饰类可以独立发展不会相互耦合装饰模式是继承的一个替代模式装饰模式可以动态扩展一个实现类的功能 缺点 Lots of little objectsA decorator and its component are not same多层装饰比较复杂
2.5 应用实例Java IO 类
InputStream and OutputStream 字节流 Reader and Writer 字符流 FilterInputStreamprotected volatile InputStream in; FilterInputStream 与 InputStream 为组合和被组合关系
与装饰器设计模式类图相符 public class JavaIO {public static void main(String[] args) throws FileNotFoundException {// Open an InputStream.FileInputStream in new FileInputStream(test.dat);// Create a buffered InputStream.BufferedInputStream bin new BufferedInputStream(in);// Create a buffered, data InputStream.DataInputStream dbin new DataInputStream(bin);// Create an unbuffered, data InputStream.DataInputStream din new DataInputStream(in);// Create a buffered, pushback, data InputStream.PushbackInputStream pbdbin new PushbackInputStream(dbin);}
}2.6 应用实例咖啡馆订购系统
饮品抽象类Beverage
public abstract class Beverage {protected String description Unknown Beverage;public String getDescription() {return description;}public abstract double cost();
}咖啡类继承 Beverage
public class DarkRoast extends Beverage {public DarkRoast() {description DarkRoast;}public double cost() {return .99;}
}
public class Espresso extends Beverage {public Espresso() {description Espresso;}public double cost() {return 1.99;}
}public class HouseBlend extends Beverage {public HouseBlend() {description House Blend Coffee;}public double cost() {return .89;}
}装饰器CondimentDecorator
public abstract class CondimentDecorator extends Beverage {public abstract String getDescription();
}装饰器子类ConcreteDecorator
public class Mocha extends CondimentDecorator {Beverage beverage;public Mocha(Beverage beverage) {this.beverage beverage;}public String getDescription() {return beverage.getDescription() , Mocha;}public double cost() {return .20 beverage.cost();}
}public class Soy extends CondimentDecorator {Beverage beverage;public Soy(Beverage beverage) {this.beverage beverage;}public String getDescription() {return beverage.getDescription() , Soy;}public double cost() {return .15 beverage.cost();}
}
public class Whip extends CondimentDecorator {Beverage beverage;public Whip(Beverage beverage) {this.beverage beverage;}public String getDescription() {return beverage.getDescription() , Whip;}public double cost() {return .10 beverage.cost();}
}使用与测试
public class StarbuzzCoffee {public static void main(String[] args) {Beverage beverage new Espresso();System.out.println(beverage.getDescription() $ beverage.cost());Beverage beverage2 new DarkRoast();beverage2 new Mocha(beverage2);beverage2 new Mocha(beverage2);beverage2 new Whip(beverage2);System.out.println(beverage2.getDescription() $ beverage2.cost());Beverage beverage3 new HouseBlend();beverage3 new Soy(beverage3);beverage3 new Mocha(beverage3);beverage3 new Whip(beverage3);System.out.println(beverage3.getDescription() $ beverage3.cost());}
}