日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > asp.net >内容正文

asp.net

设计模式-装饰者模式[Decorator]

發布時間:2023/12/20 asp.net 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 设计模式-装饰者模式[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 }

?運行結果

轉載于:https://www.cnblogs.com/masque/p/3833141.html

總結

以上是生活随笔為你收集整理的设计模式-装饰者模式[Decorator]的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。