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

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > asp.net >内容正文

asp.net

设计模式四:策略模式

發(fā)布時(shí)間:2023/12/20 asp.net 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 设计模式四:策略模式 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

策略模式
所謂策略其實(shí)就是做一件事情有很多很多的方法,比如說一個(gè)商場要搞促銷,促銷的方式有可能有很多:打折啊,滿100返50啊、積分等等之類的。這種不同的促銷方式在我們系統(tǒng)中表示就是一個(gè)一個(gè)的策略,并且策略是可以隨時(shí)更換的,這個(gè)時(shí)候在設(shè)計(jì)系統(tǒng)時(shí)就可以使用策略模式。
商場有可能會更換或追加新的促銷模式,也就是策略存在調(diào)整,也就是會更改以前的代碼,為了滿足開閉原則,這時(shí)就要使用抽象類和接口,這里我們偏向使用接口。在接口里面定義策略的方法,根據(jù)不同的情況編寫不同的實(shí)現(xiàn)類,實(shí)現(xiàn)不同的策略,策略模式比較適用于算法經(jīng)常變化的情況,比如計(jì)算工資的方式、出行方式的選擇等等。

如圖所示,我們先定義策略的接口(Promotion),然后在這個(gè)策略接口里定義策略的方法(GetPrice()),接著我們定義了兩種具體的策略(Discount打折)和(MoneyBack返現(xiàn))。
策略模式會專門有一個(gè)上下文對象(PromotionContext)專門管理策略類,并且上下文對象和策略接口之間是聚合的關(guān)系,也就是整體和部分的關(guān)系,因此在上下文對象里應(yīng)該保存一個(gè)促銷類型的引用,另外上下文對象里一般會有一些方便客戶端調(diào)用的方法,如GetPrice()。客戶端程序可以通過上下文對象得到價(jià)格,這個(gè)GetPrice()里會根據(jù)不同的策略,執(zhí)行不同的策略方法。
如果客戶端不想使用上下文中定義的默認(rèn)的策略,也可以去修改策略類,因?yàn)樯舷挛闹杏幸粋€(gè)ChangePromotion()的方法,客戶端主要使用上下文對象,如果需要修改策略,他還要依賴于具體的策略對象。

示例:

1、策略接口:

1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace 策略模式 8 { 9 /* 10 策略接口 11 */ 12 public interface IPromotion 13 { 14 /// <summary> 15 /// 根據(jù)原價(jià)和策略計(jì)算新價(jià)格 16 /// </summary> 17 /// <param name="originPrice">原價(jià)</param> 18 /// <returns></returns> 19 double GetPrice(double originPrice); 20 } 21 }

2、Discount打折策略類

1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace 策略模式 8 { 9 /// <summary> 10 /// 打折策略類 11 /// </summary> 12 public class Discount :IPromotion 13 { 14 15 public double GetPrice(double originPrice) 16 { 17 Console.WriteLine("打八折:"); 18 return originPrice * 0.8; 19 } 20 } 21 }

3、MoneyBack返現(xiàn)類

1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace 策略模式 8 { 9 /* 10 返現(xiàn)策略類:滿100返50的策略 11 */ 12 class MoneyBack :IPromotion 13 { 14 public double GetPrice(double originPrice) 15 { 16 Console.WriteLine("滿100返50"); 17 return originPrice - (int)originPrice / 100 * 50; 18 } 19 } 20 }

4、策略上下文類

1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace 策略模式 8 { 9 /* 10 策略上下文,為客戶選擇合適的策略 11 */ 12 public class PromotionContext 13 { 14 private IPromotion p = null; 15 16 public PromotionContext(IPromotion p) 17 { 18 this.p = p; 19 } 20 21 public double GetPrice(double originPrice) 22 { 23 // 默認(rèn)策略 24 if (this.p == null) 25 { 26 this.p = new Discount(); 27 } 28 return this.p.GetPrice(originPrice); 29 } 30 31 /// <summary> 32 /// 更改策略的方法 33 /// </summary> 34 /// <param name="p"></param> 35 public void ChangePromotion(IPromotion p) 36 { 37 this.p = p; 38 } 39 } 40 }

5、主程序調(diào)用

1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace 策略模式 8 { 9 class Program 10 { 11 static void Main(string[] args) 12 { 13 // 默認(rèn)策略:打八折的策略 14 PromotionContext pc = new PromotionContext(null); 15 Console.WriteLine(pc.GetPrice(200)) ; 16 17 // 更改策略:滿100返50的策略 18 pc.ChangePromotion(new MoneyBack()); 19 Console.WriteLine(pc.GetPrice(155.9)); 20 Console.ReadKey(); 21 } 22 } 23 }

?代碼連接地址:http://files.cnblogs.com/files/dotnet261010/%E8%AE%BE%E8%AE%A1%E6%A8%A1%E5%BC%8F.rar

轉(zhuǎn)載于:https://www.cnblogs.com/dotnet261010/p/7355397.html

創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎勵來咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎

總結(jié)

以上是生活随笔為你收集整理的设计模式四:策略模式的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。