Strategy 定义一系列算法或策略,把它们封闭起来,并且使它们相互可以替换。各算法或策略可以独立于客户程序而变化。
1
using System;2
using System.Collections.Generic;3
using System.Text;4

5
namespace Gof.Test.Strategy6


{7
public class FirWork8

{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;25

26
public static FirWork GetRandom()27

{28
return new FirWork("随机产品:大地绿");29
}30
}31
}1
using System;2
using System.Collections.Generic;3
using System.Text;4

5
namespace Gof.Test.Strategy6


{7
public interface IAdvisor8

{9
FirWork Recommentd(Customer c);10
}11
}1
using System;2
using System.Collections.Generic;3
using System.Text;4

5
namespace Gof.Test.Strategy6


{7
public class GroupAdvisor : IAdvisor8

{9
public static readonly GroupAdvisor singleton = new GroupAdvisor();10

11

IAdvisor 成员#region IAdvisor 成员12

13
public FirWork Recommentd(Customer c)14

{15
return (FirWork) Rel8.Advise(c);16
}17

18
#endregion19
}20
}1
using System;2
using System.Collections.Generic;3
using System.Text;4

5
namespace Gof.Test.Strategy6


{7
public class Rel88

{9
public static object Advise(Customer c)10

{11
return new FirWork(c.Name);//根据客户的相似性为客户推荐产品12
}13
}14
}1
using System;2
using System.Collections.Generic;3
using System.Text;4

5
namespace Gof.Test.Strategy6


{7
public class ItemAdvisor : IAdvisor8

{9
public static readonly ItemAdvisor singleton = new ItemAdvisor();10

11

IAdvisor 成员#region IAdvisor 成员12

13
public FirWork Recommentd(Customer c)14

{15
return (FirWork)LikeMyStuff.Advise(c);//适配到相应推荐隐形16
}17

18
#endregion19
}20
}1
using System;2
using System.Collections.Generic;3
using System.Text;4

5
namespace Gof.Test.Strategy6


{7
public class LikeMyStuff8

{9
public static object Advise(Customer c)10

{11
return new FirWork(c.Name);//根据客户最近的购买情况推荐产品12
}13
}14
}1
using System;2
using System.Collections.Generic;3
using System.Text;4

5
namespace Gof.Test.Strategy6


{7
public class PromotionAdvisor : IAdvisor8

{9
public static readonly PromotionAdvisor singleton = new PromotionAdvisor();10

11
private FirWork _promoted;12

13
private PromotionAdvisor()14

{15
_promoted = new FirWork("促销产品:在地红");//公司正在促销产品?(可以从数据库或配置文件中确定)16
}17

18
public bool HasItem()19

{20
return _promoted != null;21
}22

23

IAdvisor 成员#region IAdvisor 成员24

25
public FirWork Recommentd(Customer c)26

{27
return _promoted;28
}29

30
#endregion31
}32
}1
using System;2
using System.Collections.Generic;3
using System.Text;4

5
namespace Gof.Test.Strategy6


{7
public class RandomAdvisor : IAdvisor8

{9
public static readonly RandomAdvisor singleton = new RandomAdvisor();10

11

IAdvisor 成员#region IAdvisor 成员12

13
public FirWork Recommentd(Customer c)14

{15
return FirWork.GetRandom();16
}17

18
#endregion19
}20
}1
using System;2
using System.Collections.Generic;3
using System.Text;4

5
namespace Gof.Test.Strategy6


{7
public class Customer8

{9
public string Name10

{11
get12

{13
return _name;14
}15
set16

{17
_name = value;18
}19
}private string _name;20

21
//根据特定条件,选择推荐策略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;45

46
private bool IsRegistered()47

{48
return false;49
}50

51
private bool IsBigSpender()52

{53
return false;54
}55

56
public FirWork GetRecommended()57

{58
return GetAdvisor().Recommentd(this);59
}60
}61
}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?

浙公网安备 33010602011771号