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

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

创新互联一直秉承“诚信做人,踏实做事”的原则,不欺瞒客户,是我们最起码的底线! 以服务为基础,以质量求生存,以技术求发展,成交一个客户多一个朋友!为您提供成都做网站、网站设计、成都网页设计、小程序定制开发、成都网站开发、成都网站制作、成都软件开发、成都app软件开发公司是成都本地专业的网站建设和网站设计公司,等你一起来见证!

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

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

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

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

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

 

  1. using System; 
  2. using System.Collections.Generic; 
  3. using System.Linq; 
  4. using System.Text; 
  5.  
  6. namespace DomainModel.Model 
  7. { 
  8.     ///  
  9.     /// 打折策略 
  10.     ///  
  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.         ///  
  27.         /// 应用策略 
  28.         ///  
  29.         ///  
  30.         public void SetDiscountStrategy(IDiscountStrategy discountStrategy) 
  31.         { 
  32.             this._discountStrategy = discountStrategy; 
  33.         } 
  34.         ///  
  35.         /// 返回打折后的价格 
  36.         ///  
  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.         ///  
  60.         /// 不打折
  61.         ///  
  62.         General = 0, 
  63.         ///  
  64.         /// 6折
  65.         ///  
  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.     ///  
  84.     /// 折扣策略工厂 
  85.     ///  
  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 Find(); 
  103.     } 
  104.  
  105.     public static class ProductListExtensions 
  106.     { 
  107.         public static void ApplyDiscount(this IList 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 Find() 
  119.         { 
  120.             return new List(); 
  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 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架构设计—第三章—分层设计,初涉架构(中篇)

 

 


网页名称:设计模式-策略模式C#版
当前URL:http://www.jxruijie.cn/article/jcgjch.html