策略模式和简单工厂
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了策略模式和简单工厂相关的知识,希望对你有一定的参考价值。
策略模式由前端触发决定使用某一组算法或逻辑,每一组算法或逻辑不相同,互相不干扰。最后交由一个上下文具体去执行,并可以增加一定公有逻辑。
比如某电商平台搞活动:
普通会员:满100减10块
白银会员:满100减15块 送积分
黄金会员:满100减18块 送积分 参与抽奖
钻石会员:满100减20块 送积分 参与抽奖 免费送货
由会员级别决定执行哪一个活动,建一个枚举:
public enum MemberLevel { /// <summary> /// 普通会员 /// </summary> Normal, /// <summary> /// 白银会员 /// </summary> Silver, /// <summary> /// 黄金会员 /// </summary> Gold, /// <summary> /// 钻石会员 /// </summary> Diamond }
一个用户类:
public class User { public int Id { get; set; } public string Name { get; set; } public MemberLevel Level { get; set; } }
我们有1个会员基类,定义一个优惠的方法:
public abstract class BaseMember { public abstract void Preferential(decimal customMoney); }
4种会员分别实现BaseMember:
/// <summary> /// 普通会员 /// </summary> public class NormalUser : BaseMember { public NormalUser() { Console.WriteLine("普通会员"); } public override void Preferential(decimal customMoney) { Console.WriteLine("满100减10块"); } } /// <summary> /// 白银会员 /// </summary> public class SilverUser: BaseMember { public SilverUser() { Console.WriteLine("白银会员"); } public override void Preferential(decimal customMoney) { Console.WriteLine("满100减15块"); Console.WriteLine("送积分100"); } } /// <summary> /// 黄金会员 /// </summary> public class GoldUser: BaseMember { public GoldUser() { Console.WriteLine("黄金会员"); } public override void Preferential(decimal customMoney) { //if (customMoney > 100) //{ Console.WriteLine("满100减18块"); Console.WriteLine("送积分100"); Console.WriteLine("参与抽奖"); //} } } /// <summary> /// 钻石会员 /// </summary> public class DiamondUser : BaseMember { public DiamondUser() { Console.WriteLine("钻石会员"); } public override void Preferential(decimal customMoney) { //if (customMoney > 100) //{ Console.WriteLine("满100减20块"); Console.WriteLine("送积分100"); Console.WriteLine("参与抽奖"); Console.WriteLine("免费送货"); //} } }
我们还需要一个简单工厂:(这里可以从配置文件去获取,通过反射来实例化)
public class SimpleFactory { public static BaseMember CreateMember(MemberLevel memberLevel) { return MemberLevelConfig.MemberDictionary[memberLevel]; } }
public static class MemberLevelConfig { public static Dictionary<MemberLevel, BaseMember> MemberDictionary = new Dictionary<MemberLevel, BaseMember>() { {MemberLevel.Normal,new NormalUser() }, {MemberLevel.Silver,new SilverUser() }, {MemberLevel.Gold,new GoldUser() }, {MemberLevel.Diamond,new DiamondUser() }, }; }
最后我们还需要一个上下文:增加一个公有的逻辑如果金额不够100则无优惠
public class Context { private BaseMember _baseMember; private decimal _customMoney; public Context(BaseMember baseMember,decimal customMoney) { _baseMember = baseMember; _customMoney = customMoney; } public void Preferential() { if (_customMoney < 100) { Console.WriteLine("金额未满100,不能参加活动"); } else { _baseMember.Preferential(_customMoney); } } }
现在我们的Main方法:
static void Main(string[] args) { User user = new User(); user.Level = MemberLevel.Silver; var member = SimpleFactory.SimpleFactory.CreateMember(user.Level); Console.WriteLine("********************"); Context.Context context = new Context.Context(member, 180); context.Preferential(); Console.ReadKey(); }
本文出自 “myself” 博客,请务必保留此出处http://ccandzy.blog.51cto.com/13163569/1968052
以上是关于策略模式和简单工厂的主要内容,如果未能解决你的问题,请参考以下文章