设计模式-装饰者模式[Decorator]
生活随笔
收集整理的這篇文章主要介紹了
设计模式-装饰者模式[Decorator]
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
裝飾者模式
本文地址:http://www.cnblogs.com/masque/p/3833141.html???????????????????????
部分資料來(lái)自網(wǎng)絡(luò),代碼都是已運(yùn)行實(shí)踐,轉(zhuǎn)載請(qǐng)?jiān)诿黠@位置注明出處.
下面是一個(gè)咖啡添加不同的調(diào)料得到價(jià)錢(qián)和名稱(chēng)的算法
先定義了一個(gè)接口
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 }實(shí)現(xiàn)價(jià)錢(qián)和描述.
抽象方法的父類(lèi)有價(jià)錢(qián)和描述的屬性,計(jì)算價(jià)錢(qián)和合并名稱(chēng)的方法.
?一個(gè)咖啡類(lèi)
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 }?一個(gè)牛奶類(lèi)
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 }?一個(gè)莫妮卡類(lèi)
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 }?我們來(lái)實(shí)現(xiàn)咖啡加牛奶加莫妮卡的實(shí)現(xiàn)
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 }?運(yùn)行結(jié)果
轉(zhuǎn)載于:https://www.cnblogs.com/masque/p/3833141.html
總結(jié)
以上是生活随笔為你收集整理的设计模式-装饰者模式[Decorator]的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Python之道
- 下一篇: IOS 高级语法与设计模式5(5.3 协