设计模式-策略模式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,如需转载请自行联系原作者

目录
相关文章
|
3月前
|
设计模式 算法
策略模式-大话设计模式
策略模式-大话设计模式
|
2天前
|
设计模式 运维 算法
Java设计模式-策略模式(15)
Java设计模式-策略模式(15)
|
9天前
|
设计模式 算法 PHP
PHP中的设计模式:策略模式的深度解析
在PHP开发中,策略模式是一种行为设计模式,它允许你在运行时根据不同情况选择不同的算法或行为。本文将深入探讨策略模式的定义、结构、使用场景以及在PHP中的实现方法,并通过实例展示如何在PHP项目中应用策略模式来提高代码的灵活性和可维护性。
|
10天前
|
设计模式 人工智能 算法
PHP中的设计模式:策略模式的深入解析与实践软件测试中的人工智能革命:提升效率与准确性的新篇章
在PHP开发中,理解并运用设计模式是提升代码质量和可维护性的重要途径。本文聚焦于策略模式(Strategy Pattern),一种行为型设计模式,它允许在运行时选择算法或业务规则。通过本文,我们将深入探讨策略模式的定义、结构、使用场景以及如何在PHP项目中有效地实现和利用策略模式。不同于性能优化等技术性摘要,本文着重于提供对策略模式全面而实用的理解,助力开发者编写出更加灵活和可扩展的应用程序。 本文深入探讨了人工智能在软件测试领域的应用,揭示了其如何显著提高测试过程的效率和准确性。通过实际案例分析,展示了AI技术在自动化测试、缺陷检测及结果分析中的关键作用,并讨论了实施AI测试策略时面临的挑
16 3
|
14天前
|
设计模式 算法 PHP
PHP中的设计模式:策略模式的深度解析
在本文中,我们将深入探讨PHP编程中的策略模式,这是一种行为型设计模式,用于定义一系列算法,将每个算法封装起来,并使它们可以互换。通过详细讲解策略模式的概念、结构以及在PHP中的实现方式,我们希望能够帮助读者更好地理解和应用这一设计模式,提升代码的灵活性和可维护性。
24 1
|
21天前
|
设计模式 C# 开发者
C#设计模式入门实战教程
C#设计模式入门实战教程
|
2天前
|
设计模式 存储 算法
PHP中的设计模式:策略模式的深入解析与应用在软件开发的浩瀚海洋中,PHP以其独特的魅力和强大的功能吸引了无数开发者。作为一门历史悠久且广泛应用的编程语言,PHP不仅拥有丰富的内置函数和扩展库,还支持面向对象编程(OOP),为开发者提供了灵活而强大的工具集。在PHP的众多特性中,设计模式的应用尤为引人注目,它们如同精雕细琢的宝石,镶嵌在代码的肌理之中,让程序更加优雅、高效且易于维护。今天,我们就来深入探讨PHP中使用频率颇高的一种设计模式——策略模式。
本文旨在深入探讨PHP中的策略模式,从定义到实现,再到应用场景,全面剖析其在PHP编程中的应用价值。策略模式作为一种行为型设计模式,允许在运行时根据不同情况选择不同的算法或行为,极大地提高了代码的灵活性和可维护性。通过实例分析,本文将展示如何在PHP项目中有效利用策略模式来解决实际问题,并提升代码质量。
|
29天前
|
设计模式 缓存 算法
揭秘策略模式:如何用Java设计模式轻松切换算法?
【8月更文挑战第30天】设计模式是解决软件开发中特定问题的可重用方案。其中,策略模式是一种常用的行为型模式,允许在运行时选择算法行为。它通过定义一系列可互换的算法来封装具体的实现,使算法的变化与客户端分离。例如,在电商系统中,可以通过定义 `DiscountStrategy` 接口和多种折扣策略类(如 `FidelityDiscount`、`BulkDiscount` 和 `NoDiscount`),在运行时动态切换不同的折扣逻辑。这样,`ShoppingCart` 类无需关心具体折扣计算细节,只需设置不同的策略即可实现灵活的价格计算,符合开闭原则并提高代码的可维护性和扩展性。
39 2
|
1月前
|
设计模式 算法 开发者
深入理解工厂模式与策略模式:设计模式的灵活应用
深入理解工厂模式与策略模式:设计模式的灵活应用
|
11天前
|
设计模式 算法 PHP
PHP中的设计模式:策略模式的应用与实践
在软件开发中,设计模式是解决问题的最佳实践。本文将探讨PHP中的策略模式,通过实际应用案例,展示如何有效地使用策略模式来提高代码的灵活性和可维护性。我们将从基本概念入手,逐步深入到实际编码,最终实现一个具有策略模式的应用。