zhipingok 发表于 2013-1-26 15:54:15

装饰模式

   从来没写过技术博客,今天第一次,
   这几天在在研究设计模式,昨天还在研究桥接模式。搜到了一篇文章,让我感兴趣:
http://bc10360114.iteye.com/blog/425944
以下是文章里面举的一个示例:
例如,一杯咖啡为例,子类实现类为四个:中杯加奶、大杯加奶、 中杯不加奶、大杯不加奶。
但是,我们注意到:上面四个子类中有概念重叠,可从另外一个角度进行考虑,这四个类实际是两个角色的组合:抽象 和行为,其中抽象为:中杯和大杯;行为为:加奶 不加奶(如加橙汁 加苹果汁).
实现四个子类在抽象和行为之间发生了固定的绑定关系,如果以后动态增加加葡萄汁的行为,就必须再增加两个类:中杯加葡萄汁和大杯加葡萄汁。显然混乱,扩展性极差。
我们也不排除这种可能性,如果我去点咖啡,我会点葡萄汁加奶,特大杯加啡,(其实我没去过咖啡厅,不过我想只要给钱,他们会满足我的要求的)变态的可能性很多。对于这种场合,更适用与装饰模饰模式 ,以下是我实现在一段代码
/** ** 倒咖啡的接口 * */interface Coffee{void pour();}/** ** 倒咖啡的实现类,实现了倒咖啡的最原始方法 * */class OrgCoffee implements Coffee{@Overridepublic void pour() {System.out.println("给你倒咖啡了");}}/** ** 加糖的咖啡 * 咖啡的装饰类 * */class SugarCoffee implements Coffee{private Coffee coffee;public SugarCoffee(Coffee c){this.coffee=c;}@Overridepublic void pour() {System.out.println("加糖...");coffee.pour();}}/** ** 加 奶的咖啡 * 咖啡的装饰类 * */class MilkCoffee implements Coffee{private Coffee coffee;public MilkCoffee(Coffee c){this.coffee=c;}@Overridepublic void pour() {System.out.println("加奶...");coffee.pour();}}/** ** 加葡萄汁的咖啡 * 咖啡的装饰类 * */class GrapeJuiceCoffee implements Coffee{private Coffee coffee;public GrapeJuiceCoffee(Coffee c){this.coffee=c;}@Overridepublic void pour() {System.out.println("加葡萄汁...");coffee.pour();}}/////////////////////////////////////////////////////** ** 大杯的咖啡 * 咖啡的装饰类 * */class BigCupCoffee implements Coffee{private Coffee coffee;public BigCupCoffee(Coffee c){this.coffee=c;}@Overridepublic void pour() {System.out.println("换大杯");coffee.pour();}}/** ** 小杯的咖啡 * 咖啡的装饰类 * */class SmallCupCoffee implements Coffee{private Coffee coffee;public SmallCupCoffee(Coffee c){this.coffee=c;}@Overridepublic void pour() {System.out.println("换小杯");coffee.pour();}}/** * 特大杯的咖啡 * 咖啡的装饰类 */class VeryBigCupCoffee implements Coffee{private Coffee coffee;public VeryBigCupCoffee(Coffee c){this.coffee=c;}@Overridepublic void pour() {System.out.println("换特大杯");coffee.pour();}}public class CoffeeClient {/** * @param args */public static void main(String[] args) {System.out.println("客人A:给我来杯咖啡 :\n");OrgCoffee coffee=new OrgCoffee();coffee.pour();System.out.println("=============================================\n");System.out.println("客人B:给我来杯加糖咖啡 :\n");OrgCoffee coffeeB=new OrgCoffee(); //初始化一杯纯咖啡SugarCoffee sugerCoffeeB=new SugarCoffee(coffeeB);//加糖咖啡sugerCoffeeB.pour();System.out.println("=============================================\n");System.out.println("客人C:给我来杯加糖咖啡 加奶,再加葡萄汁的特大杯咖啡:\n");OrgCoffee coffeeC=new OrgCoffee(); //初始化一杯纯咖啡SugarCoffee sugerCoffeeC=new SugarCoffee(coffeeB);//加糖咖啡MilkCoffee milkCoffeeC=new MilkCoffee(sugerCoffeeC);//再把他加奶GrapeJuiceCoffee grapeJuiceCoffee =new GrapeJuiceCoffee(milkCoffeeC);//加葡萄汁BigCupCoffee bigCupCoffee =new BigCupCoffee(grapeJuiceCoffee);// 换上特大杯bigCupCoffee.pour();System.out.println("=============================================\n");}}//以下是我实现装饰IO,使用了保存文件加密以及读取文件解密的功能。我先把代码贴出来,以后有机会在做解释
 
 
package cn.zhipingok.io;import java.io.IOException;import java.io.InputStream;public class MyinputStream extends InputStream {private InputStream input;public MyinputStream(InputStream is){input=is;}@Overridepublic int read() throws IOException {int i=input.read();return (i==-1)?-1:i^1;}} 
package cn.zhipingok.io;import java.io.IOException;import java.io.OutputStream;public class MyOutputStream extends OutputStream {OutputStream output;public MyOutputStream(OutputStream os){output=os;}@Overridepublic void write(int b) throws IOException {output.write(b^1);}} 
public static void write() throws IOException{BufferedOutputStream bos=new BufferedOutputStream(new MyOutputStream(new FileOutputStream("C:\\test.text")));//PrintStream ps=new PrintStream(new MyOutputStream(new FileOutputStream("C:\\test.text")));String s="明朋几时有,把酒问青天";bos.write(s.getBytes());bos.close();}public static void read()throws IOException{MyinputStream my=new MyinputStream(new FileInputStream("C:\\test.text"));BufferedReader reader=new BufferedReader(new InputStreamReader(my));System.out.print(reader.readLine());} 
页: [1]
查看完整版本: 装饰模式