设计模式-策略模式C#版
策略模式是一种常见和常用的设计模式,策略的独立和抽象。
常见的场景就是电子商务中的打折策略。可以随着用户类型的不同,打折的策略也不同。
或者是游戏中打怪场景,怪的掉血策略,随着自己的级别,装备不同,怪的掉血不同。
今天的列子是打折策略,根据用户类型不同,打折策略不同。
需要在金额上做不同的打折策略,所以就在金额上留下一个口子,一个接口,传入不同的策略实现,每种实现都针对金额打不同的折扣。
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace DomainModel.Model
- {
- /// <summary>
- /// 打折策略
- /// </summary>
- public interface IDiscountStrategy
- {
- decimal Apply(decimal originalPrice);
- }
- public class Price
- {
- private IDiscountStrategy _discountStrategy;
- private decimal _salePrice;
- public Price(decimal salePrice, IDiscountStrategy discountStrategy)
- {
- this._salePrice = salePrice;
- this._discountStrategy = discountStrategy;
- }
- /// <summary>
- /// 应用策略
- /// </summary>
- /// <param name="discountStrategy"></param>
- public void SetDiscountStrategy(IDiscountStrategy discountStrategy)
- {
- this._discountStrategy = discountStrategy;
- }
- /// <summary>
- /// 返回打折后的价格
- /// </summary>
- public decimal SalePrice
- {
- get
- {
- return this._discountStrategy.Apply(this._salePrice);
- }
- }
- }
- public class Product
- {
- public int Id { get; set; }
- public string Name { get; set; }
- public Price Price { get; set; }
- }
- public enum CustomerType
- {
- /// <summary>
- /// 不打折
- /// </summary>
- General = 0,
- /// <summary>
- /// 6折
- /// </summary>
- Trade = 1
- }
- public class NullDiscountStrategy : IDiscountStrategy
- {
- public decimal Apply(decimal originalPrice)
- {
- return originalPrice;
- }
- }
- public class TradeDiscountStrategy : IDiscountStrategy
- {
- public decimal Apply(decimal originalPrice)
- {
- return originalPrice * 0.6m;
- }
- }
- /// <summary>
- /// 折扣策略工厂
- /// </summary>
- public sealed class DiscountStrategyFactory
- {
- public static IDiscountStrategy GetDiscountStrategy(CustomerType customerType)
- {
- switch (customerType)
- {
- case CustomerType.General:
- return new NullDiscountStrategy();
- default:
- return new TradeDiscountStrategy();
- }
- }
- }
- public interface IProductRepository
- {
- IList<Product> Find();
- }
- public static class ProductListExtensions
- {
- public static void ApplyDiscount(this IList<Product> products, IDiscountStrategy discountStrategy)
- {
- foreach (var p in products)
- {
- p.Price.SetDiscountStrategy(discountStrategy);
- }
- }
- }
- public class ProductRepository:IProductRepository
- {
- public IList<Product> Find()
- {
- return new List<Product>();
- }
- }
- public class ProductService
- {
- private IProductRepository _productRepository;
- public ProductService()
- {
- this._productRepository = new ProductRepository();
- }
- public ProductService(IProductRepository productRepository)
- {
- this._productRepository = productRepository;
- }
- public IList<Product> Find(CustomerType customerType)
- {
- var discountStrategy = DiscountStrategyFactory.GetDiscountStrategy(customerType);
- var products = this._productRepository.Find();
- products.ApplyDiscount(discountStrategy);
- return products;
- }
- }
- public class Client
- {
- private CustomerType _customerType = CustomerType.Trade;
- public void FindProducts()
- {
- var service = new ProductService();
- var products = service.Find(this._customerType);
- }
- }
- }
参考文献
1.走向.NET架构设计—第三章—分层设计,初涉架构(中篇)
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。