您好,登錄后才能下訂單哦!
設計模式-策略模式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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。