学习head first 设计模式之装饰者模式
2017-12-22 14:19
344 查看
装饰者模式:动态的将责任附加到对象上,若要扩展功能,装饰者提供了比继承更有弹性的替代方案
装饰模式案例 星巴克菜单
抽象组件Beverage
package headfirst.designpatterns.decorator.starbuzz;
public abstract class Beverage {
String description = "Unknown Beverage";
public String getDescription() {
return description;
}
public abstract double cost();
}
抽象装饰者类CondimentDecorator
package headfirst.designpatterns.decorator.starbuzz;
public abstract class CondimentDecorator extends Beverage {
public abstract String getDescription();
}
浓缩咖啡具体组件Espresso
package headfirst.designpatterns.decorator.starbuzz;
public class Espresso extends Beverage {
public Espresso() {
description = "Espresso";
}
public double cost() {
return 1.99;
}
}
黑咖啡具体组件DarkRoast
package headfirst.designpatterns.decorator.starbuzz;
public class DarkRoast extends Beverage {
public DarkRoast() {
description = "Dark Roast Coffee";
}
public double cost() {
return .99;
}
}
摩卡装饰者Mocha
package headfirst.designpatterns.decorator.starbuzz;
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();
}
}
奶油装饰者Whip
package headfirst.designpatterns.decorator.starbuzz;
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();
}
}
测试
package headfirst.designpatterns.decorator.starbuzz;
public class StarbuzzCoffee {
public static void main(String args[]) {
Beverage beverage2 = new DarkRoast();
beverage2 = new Mocha(beverage2);
beverage2 = new Mocha(beverage2);
beverage2 = new Whip(beverage2);
System.out.println(beverage2.getDescription()
+ " $" + beverage2.cost());
}
}
这个是说明2个摩卡一个奶油的黑咖啡
继承同一超类 将自己本身当做参数传递
java自带的io操作大量运用到装饰者模式
例如自己手动写个小写转换的类继承类FilterInputStream
package headfirst.designpatterns.decorator.io;
import java.io.*;
public class LowerCaseInputStream extends FilterInputStream {
public LowerCaseInputStream(InputStream in) {
super(in);
}
public int read() throws IOException {
int c = in.read();
return (c == -1 ? c : Character.toLowerCase((char)c));
}
public int read(byte[] b, int offset, int len) throws IOException {
int result = in.read(b, offset, len);
for (int i = offset; i < offset+result; i++) {
b[i] = (byte)Character.toLowerCase((char)b[i]);
}
return result;
}
}
测试
装饰模式案例 星巴克菜单
抽象组件Beverage
package headfirst.designpatterns.decorator.starbuzz;
public abstract class Beverage {
String description = "Unknown Beverage";
public String getDescription() {
return description;
}
public abstract double cost();
}
抽象装饰者类CondimentDecorator
package headfirst.designpatterns.decorator.starbuzz;
public abstract class CondimentDecorator extends Beverage {
public abstract String getDescription();
}
浓缩咖啡具体组件Espresso
package headfirst.designpatterns.decorator.starbuzz;
public class Espresso extends Beverage {
public Espresso() {
description = "Espresso";
}
public double cost() {
return 1.99;
}
}
黑咖啡具体组件DarkRoast
package headfirst.designpatterns.decorator.starbuzz;
public class DarkRoast extends Beverage {
public DarkRoast() {
description = "Dark Roast Coffee";
}
public double cost() {
return .99;
}
}
摩卡装饰者Mocha
package headfirst.designpatterns.decorator.starbuzz;
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();
}
}
奶油装饰者Whip
package headfirst.designpatterns.decorator.starbuzz;
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();
}
}
测试
package headfirst.designpatterns.decorator.starbuzz;
public class StarbuzzCoffee {
public static void main(String args[]) {
Beverage beverage2 = new DarkRoast();
beverage2 = new Mocha(beverage2);
beverage2 = new Mocha(beverage2);
beverage2 = new Whip(beverage2);
System.out.println(beverage2.getDescription()
+ " $" + beverage2.cost());
}
}
这个是说明2个摩卡一个奶油的黑咖啡
继承同一超类 将自己本身当做参数传递
java自带的io操作大量运用到装饰者模式
例如自己手动写个小写转换的类继承类FilterInputStream
package headfirst.designpatterns.decorator.io;
import java.io.*;
public class LowerCaseInputStream extends FilterInputStream {
public LowerCaseInputStream(InputStream in) {
super(in);
}
public int read() throws IOException {
int c = in.read();
return (c == -1 ? c : Character.toLowerCase((char)c));
}
public int read(byte[] b, int offset, int len) throws IOException {
int result = in.read(b, offset, len);
for (int i = offset; i < offset+result; i++) {
b[i] = (byte)Character.toLowerCase((char)b[i]);
}
return result;
}
}
测试
package headfirst.designpatterns.decorator.io; import java.io.*; public class InputTest { public static void main(String[] args) throws IOException { int c; try { InputStream in = new LowerCaseInputStream( new BufferedInputStream( new FileInputStream("test.txt"))); while((c = in.read()) >= 0) { System.out.print((char)c); } in.close(); } catch (IOException e) { e.printStackTrace(); } } }
相关文章推荐
- 《Head First 设计模式》学习笔记:装饰者模式
- (Head First 设计模式)学习笔记(3) --装饰者模式(StarBuzz咖啡店实例)
- Head First 设计模式学习笔记——装饰者模式
- (Head First 设计模式)学习笔记(3) --装饰者模式(StarBuzz咖啡店实例)
- head first 设计模式学习随笔(3)----装饰者模式
- Head First 设计模式学习——装饰者模式
- head first 设计模式学习随笔(5)----单件模式
- 学习javascript设计模式之装饰者模式
- 设计模式学习---装饰者
- <JAVA学习笔记二>——设计模式之装饰者设计模式
- (Head First 设计模式)学习笔记(2) --观察者模式(气象站实例)
- 【设计模式】学习笔记3:装饰者模式(Decorator )
- 《head first 设计模式》day three 装饰者模式
- 设计模式学习笔记(三)- -装饰者模式
- 学习:设计模式之装饰者模式
- Head First-设计模式:装饰者模式
- Java学习总结(8)—内存流,打印流,对象流,RandomAccessFile,装饰者设计模式
- Head First 设计模式——装饰者模式(Decorator Pattern)
- Head First 设计模式学习笔记 day one Strategy model
- Java-马士兵设计模式学习笔记-装饰者模式