Strategy 定义一系列算法或策略,把它们封闭起来,并且使它们相互可以替换。各算法或策略可以独立于客户程序而变化。
产品类
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 Name
14 {
15 get
16 {
17 return _name;
18 }
19 set
20 {
21 _name = value;
22 }
23 }
24 private string _name;
25
26 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 class FirWork
8 {
9 public FirWork(string name)
10 {
11 _name = name;
12 }
13 public string Name
14 {
15 get
16 {
17 return _name;
18 }
19 set
20 {
21 _name = value;
22 }
23 }
24 private string _name;
25
26 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 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();
10
11 IAdvisor 成员#region IAdvisor 成员
12
13 public FirWork Recommentd(Customer c)
14 {
15 return (FirWork) Rel8.Advise(c);
16 }
17
18 #endregion
19 }
20}
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();
10
11 IAdvisor 成员#region IAdvisor 成员
12
13 public FirWork Recommentd(Customer c)
14 {
15 return (FirWork) Rel8.Advise(c);
16 }
17
18 #endregion
19 }
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 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();
10
11 IAdvisor 成员#region IAdvisor 成员
12
13 public FirWork Recommentd(Customer c)
14 {
15 return (FirWork)LikeMyStuff.Advise(c);//适配到相应推荐隐形
16 }
17
18 #endregion
19 }
20}
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();
10
11 IAdvisor 成员#region IAdvisor 成员
12
13 public FirWork Recommentd(Customer c)
14 {
15 return (FirWork)LikeMyStuff.Advise(c);//适配到相应推荐隐形
16 }
17
18 #endregion
19 }
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 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();
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 #endregion
31 }
32}
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();
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 #endregion
31 }
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();
10
11 IAdvisor 成员#region IAdvisor 成员
12
13 public FirWork Recommentd(Customer c)
14 {
15 return FirWork.GetRandom();
16 }
17
18 #endregion
19 }
20}
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();
10
11 IAdvisor 成员#region IAdvisor 成员
12
13 public FirWork Recommentd(Customer c)
14 {
15 return FirWork.GetRandom();
16 }
17
18 #endregion
19 }
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 Name
10 {
11 get
12 {
13 return _name;
14 }
15 set
16 {
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}
The Strategy Pattern defines a family of algorithms, encapsulates each one, and makes them interchangeable. Strategy lets algorithm vary independently from clients use it. 1using System;
2using System.Collections.Generic;
3using System.Text;
4
5namespace Gof.Test.Strategy
6{
7 public class Customer
8 {
9 public string Name
10 {
11 get
12 {
13 return _name;
14 }
15 set
16 {
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?