您的位置:首页 > 其它

学习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;
}
}
测试
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();
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: