设计模式-策略模式C#版
设计模式-策略模式C#版
创新互联一直秉承“诚信做人,踏实做事”的原则,不欺瞒客户,是我们最起码的底线! 以服务为基础,以质量求生存,以技术求发展,成交一个客户多一个朋友!为您提供成都做网站、网站设计、成都网页设计、小程序定制开发、成都网站开发、成都网站制作、成都软件开发、成都app软件开发公司是成都本地专业的网站建设和网站设计公司,等你一起来见证!
策略模式是一种常见和常用的设计模式,策略的独立和抽象。
常见的场景就是电子商务中的打折策略。可以随着用户类型的不同,打折的策略也不同。
或者是游戏中打怪场景,怪的掉血策略,随着自己的级别,装备不同,怪的掉血不同。
今天的列子是打折策略,根据用户类型不同,打折策略不同。
需要在金额上做不同的打折策略,所以就在金额上留下一个口子,一个接口,传入不同的策略实现,每种实现都针对金额打不同的折扣。
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace DomainModel.Model
- {
- ///
- /// 打折策略
- ///
- 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;
- }
- ///
- /// 应用策略
- ///
- ///
- public void SetDiscountStrategy(IDiscountStrategy discountStrategy)
- {
- this._discountStrategy = discountStrategy;
- }
- ///
- /// 返回打折后的价格
- ///
- 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
- {
- ///
- /// 不打折
- ///
- General = 0,
- ///
- /// 6折
- ///
- 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;
- }
- }
- ///
- /// 折扣策略工厂
- ///
- 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
Find(); - }
- public static class ProductListExtensions
- {
- public static void ApplyDiscount(this IList
products, IDiscountStrategy discountStrategy) - {
- foreach (var p in products)
- {
- p.Price.SetDiscountStrategy(discountStrategy);
- }
- }
- }
- public class ProductRepository:IProductRepository
- {
- public IList
Find() - {
- return new List
(); - }
- }
- public class ProductService
- {
- private IProductRepository _productRepository;
- public ProductService()
- {
- this._productRepository = new ProductRepository();
- }
- public ProductService(IProductRepository productRepository)
- {
- this._productRepository = productRepository;
- }
- public IList
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架构设计—第三章—分层设计,初涉架构(中篇)
本文标题:设计模式-策略模式C#版
路径分享:http://ybzwz.com/article/jcgjch.html