網站首頁 編程語言 正文
策略模式
所謂策略其實就是做一件事情有很多很多的方法,比如說一個商場要搞促銷,促銷的方式有可能有很多:打折啊,滿100返50啊、積分等等之類的。這種不同的促銷方式在我們系統中表示就是一個一個的策略,并且策略是可以隨時更換的,這個時候在設計系統時就可以使用策略模式。
商場有可能會更換或追加新的促銷模式,也就是策略存在調整,也就是會更改以前的代碼,為了滿足開閉原則,這時就要使用抽象類和接口,這里我們偏向使用接口。在接口里面定義策略的方法,根據不同的情況編寫不同的實現類,實現不同的策略,策略模式比較適用于算法經常變化的情況,比如計算工資的方式、出行方式的選擇等等。
如圖所示,我們先定義策略的接口(Promotion),然后在這個策略接口里定義策略的方法(GetPrice()),接著我們定義了兩種具體的策略(Discount打折)和(MoneyBack返現)。
策略模式會專門有一個上下文對象(PromotionContext)專門管理策略類,并且上下文對象和策略接口之間是聚合的關系,也就是整體和部分的關系,因此在上下文對象里應該保存一個促銷類型的引用,另外上下文對象里一般會有一些方便客戶端調用的方法,如GetPrice()。客戶端程序可以通過上下文對象得到價格,這個GetPrice()里會根據不同的策略,執行不同的策略方法。
如果客戶端不想使用上下文中定義的默認的策略,也可以去修改策略類,因為上下文中有一個ChangePromotion()的方法,客戶端主要使用上下文對象,如果需要修改策略,他還要依賴于具體的策略對象。
示例:
1、策略接口:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace 策略模式 { /* 策略接口 */ public interface IPromotion { ////// 根據原價和策略計算新價格 /// /// 原價 ///double GetPrice(double originPrice); } }
2、Discount打折策略類
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace 策略模式 { ////// 打折策略類 /// public class Discount :IPromotion { public double GetPrice(double originPrice) { Console.WriteLine("打八折:"); return originPrice * 0.8; } } }
3、MoneyBack返現類
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace 策略模式 { /* 返現策略類:滿100返50的策略 */ class MoneyBack :IPromotion { public double GetPrice(double originPrice) { Console.WriteLine("滿100返50"); return originPrice - (int)originPrice / 100 * 50; } } }
4、策略上下文類
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace 策略模式 { /* 策略上下文,為客戶選擇合適的策略 */ public class PromotionContext { private IPromotion p = null; public PromotionContext(IPromotion p) { this.p = p; } public double GetPrice(double originPrice) { // 默認策略 if (this.p == null) { this.p = new Discount(); } return this.p.GetPrice(originPrice); } ////// 更改策略的方法 /// /// public void ChangePromotion(IPromotion p) { this.p = p; } } }
5、主程序調用
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace 策略模式 { class Program { static void Main(string[] args) { // 默認策略:打八折的策略 PromotionContext pc = new PromotionContext(null); Console.WriteLine(pc.GetPrice(200)) ; // 更改策略:滿100返50的策略 pc.ChangePromotion(new MoneyBack()); Console.WriteLine(pc.GetPrice(155.9)); Console.ReadKey(); } } }
代碼下載地址:點擊下載
原文鏈接:https://www.cnblogs.com/dotnet261010/p/7355397.html
相關推薦
- 2022-04-03 在Python中如何優雅地創建表格的實現_python
- 2022-09-03 C#實現工廠方法模式_C#教程
- 2022-08-11 Python操作數據庫之數據庫編程接口_python
- 2023-05-21 linux中grep命令使用實戰詳解_Linux
- 2022-05-10 oracle如何創建或刪除臨時表空間和空間詳解
- 2023-11-12 ubuntu18.04 開機后 USB 端口不能使用;臨時添加usb驅動導致鼠標鍵盤不可用ubunt
- 2022-07-12 Linux系統下的時區配置管理
- 2022-11-16 python?sklearn與pandas實現缺失值數據預處理流程詳解_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支