产品类
1using System; 2using System.Collections.Generic; 3using System.Text; 4 5namespace Gof.Test.Strategy 6{ 7 public class FirWork 8 { 9 public FirWork(string name)10 {11 _name = name;12 }13 public string Name14 {15 get16 {17 return _name;18 }19 set20 {21 _name = value;22 }23 }24 private string _name;2526 public static FirWork GetRandom()27 {28 return new FirWork("随机产品:大地绿");29 }30 }31}
接口,定义策略操作
1using System; 2using System.Collections.Generic; 3using System.Text; 4 5namespace Gof.Test.Strategy 6{ 7 public interface IAdvisor 8 { 9 FirWork Recommentd(Customer c);10 }11}
如果用户没有注册,根据相似性推荐产品
1using System; 2using System.Collections.Generic; 3using System.Text; 4 5namespace Gof.Test.Strategy 6{ 7 public class GroupAdvisor : IAdvisor 8 { 9 public static readonly GroupAdvisor singleton = new GroupAdvisor();1011 IAdvisor 成员#region IAdvisor 成员1213 public FirWork Recommentd(Customer c)14 {15 return (FirWork) Rel8.Advise(c);16 }1718 #endregion19 }20}
根据相似性推荐产品的隐形
1using System; 2using System.Collections.Generic; 3using System.Text; 4 5namespace Gof.Test.Strategy 6{ 7 public class Rel8 8 { 9 public static object Advise(Customer c)10 {11 return new FirWork(c.Name);//根据客户的相似性为客户推荐产品12 }13 }14}
根据客户最近的购买情况,推荐产品
1using System; 2using System.Collections.Generic; 3using System.Text; 4 5namespace Gof.Test.Strategy 6{ 7 public class ItemAdvisor : IAdvisor 8 { 9 public static readonly ItemAdvisor singleton = new ItemAdvisor();1011 IAdvisor 成员#region IAdvisor 成员1213 public FirWork Recommentd(Customer c)14 {15 return (FirWork)LikeMyStuff.Advise(c);//适配到相应推荐隐形16 }1718 #endregion19 }20}
购买情况推荐产品隐形
1using System; 2using System.Collections.Generic; 3using System.Text; 4 5namespace Gof.Test.Strategy 6{ 7 public class LikeMyStuff 8 { 9 public static object Advise(Customer c)10 {11 return new FirWork(c.Name);//根据客户最近的购买情况推荐产品12 }13 }14}
公司正在促销
1using System; 2using System.Collections.Generic; 3using System.Text; 4 5namespace Gof.Test.Strategy 6{ 7 public class PromotionAdvisor : IAdvisor 8 { 9 public static readonly PromotionAdvisor singleton = new PromotionAdvisor();1011 private FirWork _promoted;1213 private PromotionAdvisor()14 {15 _promoted = new FirWork("促销产品:在地红");//公司正在促销产品?(可以从数据库或配置文件中确定)16 }1718 public bool HasItem()19 {20 return _promoted != null;21 }2223 IAdvisor 成员#region IAdvisor 成员2425 public FirWork Recommentd(Customer c)26 {27 return _promoted;28 }2930 #endregion31 }32}
随机推荐
1using System; 2using System.Collections.Generic; 3using System.Text; 4 5namespace Gof.Test.Strategy 6{ 7 public class RandomAdvisor : IAdvisor 8 { 9 public static readonly RandomAdvisor singleton = new RandomAdvisor();1011 IAdvisor 成员#region IAdvisor 成员1213 public FirWork Recommentd(Customer c)14 {15 return FirWork.GetRandom();16 }1718 #endregion19 }20}
Customer类,根据条件选择推荐隐形
1using System; 2using System.Collections.Generic; 3using System.Text; 4 5namespace Gof.Test.Strategy 6{ 7 public class Customer 8 { 9 public string Name10 {11 get12 {13 return _name;14 }15 set16 {17 _name = value;18 }19 }private string _name;2021 //根据特定条件,选择推荐策略22 public IAdvisor GetAdvisor()23 {24 if (_advisor == null)//还没有一种策略25 {26 if (PromotionAdvisor.singleton.HasItem())//是否在促销27 {28 _advisor = PromotionAdvisor.singleton;29 }30 else if (IsRegistered())//用户是否有注册31 {32 _advisor = GroupAdvisor.singleton;33 }34 else if (IsBigSpender())//是否是在买家35 {36 _advisor = ItemAdvisor.singleton;37 }38 else//以上全不是,刚随机39 {40 _advisor = RandomAdvisor.singleton;41 }42 }43 return _advisor;44 }private IAdvisor _advisor;4546 private bool IsRegistered()47 {48 return false;49 }5051 private bool IsBigSpender()52 {53 return false;54 }5556 public FirWork GetRecommended()57 {58 return GetAdvisor().Recommentd(this);59 }60 }61}
The Strategy Pattern defines a family of algorithms, encapsulates each one, and makes them interchangeable. Strategy lets algorithm vary independently from clients use it.
We can see three design principle:
1.Identify the aspects of your application that vary and separate them from what stays the same.
2.Program to an interface, not an implemention.
3.Favor composition over inheritance.
Can you see it?
转载于:https://www.cnblogs.com/nanshouyong326/archive/2007/01/11/617851.html