设计模式-装饰者模式[Decorator]
生活随笔
收集整理的這篇文章主要介紹了
设计模式-装饰者模式[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 * 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 }?運行結果
轉載于:https://www.cnblogs.com/masque/p/3833141.html
總結
以上是生活随笔為你收集整理的设计模式-装饰者模式[Decorator]的全部內容,希望文章能夠幫你解決所遇到的問題。