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

简介:

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

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

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

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

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

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

 

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

 

 

参考文献

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

 

 




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

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