设计模式-装饰者模式[Decorator]
装饰者模式
本文地址:http://www.cnblogs.com/masque/p/3833141.html
部分资料来自网络,代码都是已运行实践,转载请在明显位置注明出处.
下面是一个咖啡添加不同的调料得到价钱和名称的算法
先定义了一个接口
1 package org.masque.designpatterns.decorator; 2 /** 3 * 4 * Description: 5 * InterForAll.java Create on 2014年7月9日 上午10:59:50 6 * @author masque.java@outlook.com 7 * @version 1.0 8 * Copyright (c) 2014 Company,Inc. All Rights Reserved. 9 */ 10 public interface InterForAll { 11 12 double display(); 13 14 String print(); 15 }
实现价钱和描述.
抽象方法的父类有价钱和描述的属性,计算价钱和合并名称的方法.
1 package org.masque.designpatterns.decorator; 2 /** 3 * 4 * Description: 5 * AbstractParent.java Create on 2014年7月9日 上午11:00:04 6 * @author masque.java@outlook.com 7 * @version 1.0 8 * Copyright (c) 2014 Company,Inc. All Rights Reserved. 9 */ 10 public abstract class AbstractParent implements InterForAll { 11 12 protected String describe; 13 14 protected double price; 15 16 @Override 17 public abstract double display(); 18 19 @Override 20 public abstract String print(); 21 22 public String getDescribe() { 23 return describe; 24 } 25 26 public void setDescribe(String describe) { 27 this.describe = describe; 28 } 29 30 public double getPrice() { 31 return price; 32 } 33 34 public void setPrice(double price) { 35 this.price = price; 36 } 37 38 @Override 39 public String toString() { 40 return "AbstractParent [describe=" + describe + ", price=" + price 41 + "]"; 42 } 43 }
一个咖啡类
1 package org.masque.designpatterns.decorator; 2 /** 3 * 4 * Description: 5 * Coffee.java Create on 2014年7月9日 上午11:00:11 6 * @author masque.java@outlook.com 7 * @version 1.0 8 * Copyright (c) 2014 Company,Inc. All Rights Reserved. 9 */ 10 public final class Coffee extends AbstractParent { 11 12 public Coffee(){ 13 this.describe = "Coffee"; 14 this.price = .95; 15 } 16 17 @Override 18 public double display() { 19 return this.price; 20 } 21 22 @Override 23 public String print() { 24 return this.describe; 25 } 26 }
一个牛奶类
1 package org.masque.designpatterns.decorator; 2 /** 3 * 4 * Description: 5 * Milk.java Create on 2014年7月9日 上午11:00:32 6 * @author masque.java@outlook.com 7 * @version 1.0 8 * Copyright (c) 2014 Company,Inc. All Rights Reserved. 9 */ 10 public final class Milk extends AbstractParent { 11 12 private AbstractParent abstractParent; 13 14 public Milk(AbstractParent abstractParent){ 15 this.describe = "Milk"; 16 this.price = .2; 17 this.abstractParent = abstractParent; 18 } 19 20 @Override 21 public double display() { 22 return abstractParent.display() + price; 23 } 24 25 @Override 26 public String print() { 27 return abstractParent.print() + "," + describe; 28 } 29 }
一个莫妮卡类
1 package org.masque.designpatterns.decorator; 2 /** 3 * 4 * Description: 5 * Mocha.java Create on 2014年7月9日 上午11:00:39 6 * @author masque.java@outlook.com 7 * @version 1.0 8 * Copyright (c) 2014 Company,Inc. All Rights Reserved. 9 */ 10 public final class Mocha extends AbstractParent { 11 12 private AbstractParent abstractParent; 13 14 public Mocha(AbstractParent abstractParent){ 15 this.describe = "Mocha"; 16 this.price = .3; 17 this.abstractParent = abstractParent; 18 } 19 20 @Override 21 public double display() { 22 return abstractParent.display() + price; 23 } 24 25 @Override 26 public String print() { 27 return abstractParent.print() + "," + describe; 28 } 29 }
我们来实现咖啡加牛奶加莫妮卡的实现
1 package org.masque.designpatterns.decorator; 2 /** 3 * 4 * Description: 5 * Main.java Create on 2014年7月9日 上午11:00:22 6 * @author masque.java@outlook.com 7 * @version 1.0 8 * Copyright (c) 2014 Company,Inc. All Rights Reserved. 9 */ 10 public class Main { 11 12 public static void main(String[] args) { 13 AbstractParent coffee1 = new Coffee(); 14 System.out.println(coffee1.toString()); 15 16 AbstractParent coffee2 = new Coffee(); 17 coffee2 = new Milk(coffee2); 18 System.out.println("------+Milk----------"); 19 System.out.println(coffee2.toString()); 20 System.out.println(coffee2.display()); 21 System.out.println(coffee2.print()); 22 23 System.out.println("------+Mocha----------"); 24 coffee2 = new Mocha(coffee2); 25 System.out.println(coffee2.toString()); 26 System.out.println(coffee2.display()); 27 System.out.println(coffee2.print()); 28 } 29 }
运行结果