设计模式-策略模式C#版

简介:

设计模式-策略模式C#版

策略模式是一种常见和常用的设计模式,策略的独立和抽象。

常见的场景就是电子商务中的打折策略。可以随着用户类型的不同,打折的策略也不同。

或者是游戏中打怪场景,怪的掉血策略,随着自己的级别,装备不同,怪的掉血不同。

今天的列子是打折策略,根据用户类型不同,打折策略不同。

需要在金额上做不同的打折策略,所以就在金额上留下一个口子,一个接口,传入不同的策略实现,每种实现都针对金额打不同的折扣。

 

 
  1. using System; 
  2. using System.Collections.Generic; 
  3. using System.Linq; 
  4. using System.Text; 
  5.  
  6. namespace DomainModel.Model 
  7.     /// <summary> 
  8.     /// 打折策略 
  9.     /// </summary> 
  10.     public interface IDiscountStrategy 
  11.     { 
  12.         decimal Apply(decimal originalPrice); 
  13.     } 
  14.  
  15.     public class Price 
  16.     { 
  17.         private IDiscountStrategy _discountStrategy; 
  18.         private decimal _salePrice; 
  19.  
  20.         public Price(decimal salePrice, IDiscountStrategy discountStrategy) 
  21.         { 
  22.             this._salePrice = salePrice; 
  23.             this._discountStrategy = discountStrategy; 
  24.         } 
  25.         /// <summary> 
  26.         /// 应用策略 
  27.         /// </summary> 
  28.         /// <param name="discountStrategy"></param> 
  29.         public void SetDiscountStrategy(IDiscountStrategy discountStrategy) 
  30.         { 
  31.             this._discountStrategy = discountStrategy; 
  32.         } 
  33.         /// <summary> 
  34.         /// 返回打折后的价格 
  35.         /// </summary> 
  36.         public decimal SalePrice 
  37.         { 
  38.  
  39.             get 
  40.             { 
  41.                 return this._discountStrategy.Apply(this._salePrice); 
  42.             } 
  43.         } 
  44.  
  45.     } 
  46.  
  47.     public class Product 
  48.     { 
  49.  
  50.         public int Id { getset; } 
  51.  
  52.         public string Name { getset; } 
  53.  
  54.         public Price Price { getset; } 
  55.     } 
  56.     public enum CustomerType 
  57.     { 
  58.         /// <summary> 
  59.         ///  不打折
  60.         /// </summary> 
  61.         General = 0, 
  62.         /// <summary> 
  63.         ///  6折
  64.         /// </summary> 
  65.         Trade = 1 
  66.     } 
  67.     public class NullDiscountStrategy : IDiscountStrategy 
  68.     { 
  69.  
  70.         public decimal Apply(decimal originalPrice) 
  71.         { 
  72.             return originalPrice; 
  73.         } 
  74.     } 
  75.     public class TradeDiscountStrategy : IDiscountStrategy 
  76.     { 
  77.         public decimal Apply(decimal originalPrice) 
  78.         { 
  79.             return originalPrice * 0.6m; 
  80.         } 
  81.     } 
  82.     /// <summary> 
  83.     /// 折扣策略工厂 
  84.     /// </summary> 
  85.     public sealed class DiscountStrategyFactory 
  86.     { 
  87.  
  88.         public static IDiscountStrategy GetDiscountStrategy(CustomerType customerType) 
  89.         { 
  90.             switch (customerType) 
  91.             { 
  92.                 case CustomerType.General: 
  93.                     return new NullDiscountStrategy(); 
  94.                 default
  95.                     return new TradeDiscountStrategy(); 
  96.             } 
  97.         } 
  98.     } 
  99.     public interface IProductRepository 
  100.     { 
  101.         IList<Product> Find(); 
  102.     } 
  103.  
  104.     public static class ProductListExtensions 
  105.     { 
  106.         public static void ApplyDiscount(this IList<Product> products, IDiscountStrategy discountStrategy) 
  107.         { 
  108.             foreach (var p in products) 
  109.             { 
  110.                 p.Price.SetDiscountStrategy(discountStrategy); 
  111.             } 
  112.         } 
  113.     } 
  114.  
  115.     public class ProductRepository:IProductRepository  
  116.     { 
  117.         public IList<Product> Find() 
  118.         { 
  119.             return new List<Product>(); 
  120.         } 
  121.     } 
  122.     public class ProductService 
  123.     { 
  124.         private IProductRepository _productRepository; 
  125.         public ProductService() 
  126.         { 
  127.             this._productRepository = new ProductRepository(); 
  128.         } 
  129.         public ProductService(IProductRepository productRepository) 
  130.         { 
  131.             this._productRepository = productRepository; 
  132.         } 
  133.  
  134.         public IList<Product> Find(CustomerType customerType) 
  135.         { 
  136.             var discountStrategy = DiscountStrategyFactory.GetDiscountStrategy(customerType); 
  137.             var products = this._productRepository.Find(); 
  138.             products.ApplyDiscount(discountStrategy); 
  139.             return products; 
  140.         } 
  141.     } 
  142.  
  143.     public class Client 
  144.     { 
  145.         private CustomerType _customerType = CustomerType.Trade; 
  146.          
  147.         public void FindProducts() 
  148.         { 
  149.             var service = new ProductService(); 
  150.             var products = service.Find(this._customerType); 
  151.         } 
  152.     } 

 

 

参考文献

1.走向.NET架构设计—第三章—分层设计,初涉架构(中篇)

 

 




本文转自 virusswb 51CTO博客,原文链接http://blog.51cto.com/virusswb/1020660,如需转载请自行联系原作者

目录
相关文章
|
4月前
|
设计模式 算法 搜索推荐
Java 设计模式之策略模式:灵活切换算法的艺术
策略模式通过封装不同算法并实现灵活切换,将算法与使用解耦。以支付为例,微信、支付宝等支付方式作为独立策略,购物车根据选择调用对应支付逻辑,提升代码可维护性与扩展性,避免冗长条件判断,符合开闭原则。
595 35
|
5月前
|
设计模式 人工智能 算法
基于多设计模式的状态扭转设计:策略模式与责任链模式的实战应用
接下来,我会结合实战案例,聊聊如何用「策略模式 + 责任链模式」构建灵活可扩展的状态引擎,让抽奖系统的状态管理从「混乱战场」变成「有序流水线」。
|
9月前
|
设计模式 算法 Java
设计模式觉醒系列(04)策略模式|简单工厂模式的升级版
本文介绍了简单工厂模式与策略模式的概念及其融合实践。简单工厂模式用于对象创建,通过隐藏实现细节简化代码;策略模式关注行为封装与切换,支持动态替换算法,增强灵活性。两者结合形成“策略工厂”,既简化对象创建又保持低耦合。文章通过支付案例演示了模式的应用,并强调实际开发中应根据需求选择合适的设计模式,避免生搬硬套。最后推荐了JVM调优、并发编程等技术专题,助力开发者提升技能。
|
设计模式 算法 Kotlin
Kotlin - 改良设计模式 - 策略模式
Kotlin - 改良设计模式 - 策略模式
161 4
|
9月前
|
设计模式 算法 搜索推荐
【设计模式】【行为型模式】策略模式(Strategy)
一、入门 什么是策略模式? 策略模式是一种行为设计模式,允许在运行时选择算法或行为。它将算法封装在独立的类中,使得它们可以互换,而不影响客户端代码。 为什么需要策略模式? 策略模式的主要目的是解决算法
190 14
|
设计模式 算法 开发者
「全网最细 + 实战源码案例」设计模式——策略模式
策略模式(Strategy Pattern)是一种行为型设计模式,用于定义一系列可替换的算法或行为,并将它们封装成独立的类。通过上下文持有策略对象,在运行时动态切换算法,提高代码的可维护性和扩展性。适用于需要动态切换算法、避免条件语句、经常扩展算法或保持算法独立性的场景。优点包括符合开闭原则、运行时切换算法、解耦上下文与策略实现、减少条件判断;缺点是增加类数量和策略切换成本。示例中通过定义抽象策略接口和具体策略类,结合上下文类实现动态算法选择。
453 8
「全网最细 + 实战源码案例」设计模式——策略模式
|
设计模式 存储 缓存
前端必须掌握的设计模式——策略模式
策略模式(Strategy Pattern)是一种行为型设计模式,旨在将多分支复杂逻辑解耦。每个分支类只关心自身实现,无需处理策略切换。它避免了大量if-else或switch-case代码,符合开闭原则。常见应用场景包括表单验证、风格切换和缓存调度等。通过定义接口和上下文类,策略模式实现了灵活的逻辑分离与扩展。例如,在国际化需求中,可根据语言切换不同的词条包,使代码更加简洁优雅。总结来说,策略模式简化了多条件判断,提升了代码的可维护性和扩展性。
|
设计模式 算法 C#
C# 一分钟浅谈:策略模式与状态模式
【10月更文挑战第13天】本文介绍了两种常见的行为型设计模式:策略模式和状态模式。策略模式通过封装一系列算法并使其可互换,实现算法的灵活变化;状态模式则通过改变对象的内部状态来改变其行为。文章通过C#代码示例详细说明了这两种模式的应用场景、常见问题及解决方法。
207 19
|
设计模式 算法 Kotlin
Kotlin教程笔记(53) - 改良设计模式 - 策略模式
Kotlin教程笔记(53) - 改良设计模式 - 策略模式
150 1
|
设计模式 前端开发 JavaScript
JavaScript设计模式及其在实战中的应用,涵盖单例、工厂、观察者、装饰器和策略模式
本文深入探讨了JavaScript设计模式及其在实战中的应用,涵盖单例、工厂、观察者、装饰器和策略模式,结合电商网站案例,展示了设计模式如何提升代码的可维护性、扩展性和可读性,强调了其在前端开发中的重要性。
222 2