温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

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

发布时间:2020-06-25 07:00:17 来源:网络 阅读:704 作者:virusswb 栏目:编程语言

设计模式-策略模式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架构设计—第三章—分层设计,初涉架构(中篇)

 

 

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI