哈希算法的特点不包括,Strategy 定义一系列算法或策略,把它们封闭起来,并且使它们相互可以替换。各算法或策略可以独立于客户程序而变化。...

 2023-09-21 阅读 19 评论 0

摘要:产品类1usingSystem;2usingSystem.Collections.Generic;3usingSystem.Text;45namespaceGof.Test.Strategy6{7publicclassFirWork8{9publicFirWork(stringname)10{11_name=name;12}13publicstringName14{15get16{17return_name;18}19set20{21_name=value;22}23}24priva
ContractedBlock.gifExpandedBlockStart.gif产品类
 1None.gifusing System;
 2None.gifusing System.Collections.Generic;
 3None.gifusing System.Text;
 4None.gif
 5None.gifnamespace Gof.Test.Strategy
 6ExpandedBlockStart.gifContractedBlock.gifdot.gif{
 7InBlock.gif    public class FirWork
 8ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 9InBlock.gif        public FirWork(string name)
10ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
11InBlock.gif            _name = name;
12ExpandedSubBlockEnd.gif        }

13InBlock.gif        public string Name
14ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
15InBlock.gif            get
16ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
17InBlock.gif                return _name;
18ExpandedSubBlockEnd.gif            }

19InBlock.gif            set
20ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
21InBlock.gif                _name = value;
22ExpandedSubBlockEnd.gif            }

23ExpandedSubBlockEnd.gif        }

24InBlock.gif        private string _name;
25InBlock.gif
26InBlock.gif        public static FirWork GetRandom()
27ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
28InBlock.gif            return new FirWork("随机产品:大地绿");
29ExpandedSubBlockEnd.gif        }

30ExpandedSubBlockEnd.gif    }

31ExpandedBlockEnd.gif}
ContractedBlock.gifExpandedBlockStart.gif接口,定义策略操作
 1None.gifusing System;
 2None.gifusing System.Collections.Generic;
 3None.gifusing System.Text;
 4None.gif
 5None.gifnamespace Gof.Test.Strategy
 6ExpandedBlockStart.gifContractedBlock.gifdot.gif{
 7InBlock.gif    public interface IAdvisor
 8ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 9InBlock.gif        FirWork Recommentd(Customer c);
10ExpandedSubBlockEnd.gif    }

11ExpandedBlockEnd.gif}
ContractedBlock.gifExpandedBlockStart.gif如果用户没有注册,根据相似性推荐产品
 1None.gifusing System;
 2None.gifusing System.Collections.Generic;
 3None.gifusing System.Text;
 4None.gif
 5None.gifnamespace Gof.Test.Strategy
 6ExpandedBlockStart.gifContractedBlock.gifdot.gif{
 7InBlock.gif    public class GroupAdvisor : IAdvisor
 8ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 9InBlock.gif        public static readonly GroupAdvisor singleton = new GroupAdvisor();
10InBlock.gif
11ContractedSubBlock.gifExpandedSubBlockStart.gif        IAdvisor 成员#region IAdvisor 成员
12InBlock.gif
13InBlock.gif        public FirWork Recommentd(Customer c)
14ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
15InBlock.gif            return (FirWork) Rel8.Advise(c);
16ExpandedSubBlockEnd.gif        }

17InBlock.gif
18ExpandedSubBlockEnd.gif        #endregion

19ExpandedSubBlockEnd.gif    }

20ExpandedBlockEnd.gif}
ContractedBlock.gifExpandedBlockStart.gif根据相似性推荐产品的隐形
 1None.gifusing System;
 2None.gifusing System.Collections.Generic;
 3None.gifusing System.Text;
 4None.gif
 5None.gifnamespace Gof.Test.Strategy
 6ExpandedBlockStart.gifContractedBlock.gifdot.gif{
 7InBlock.gif    public class Rel8
 8ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 9InBlock.gif        public static object Advise(Customer c)
10ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
11InBlock.gif            return new FirWork(c.Name);//根据客户的相似性为客户推荐产品
12ExpandedSubBlockEnd.gif        }

13ExpandedSubBlockEnd.gif    }

14ExpandedBlockEnd.gif}
ContractedBlock.gifExpandedBlockStart.gif根据客户最近的购买情况,推荐产品
 1None.gifusing System;
 2None.gifusing System.Collections.Generic;
 3None.gifusing System.Text;
 4None.gif
 5None.gifnamespace Gof.Test.Strategy
 6ExpandedBlockStart.gifContractedBlock.gifdot.gif{
 7InBlock.gif    public class ItemAdvisor : IAdvisor
 8ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 9InBlock.gif        public static readonly ItemAdvisor singleton = new ItemAdvisor();
10InBlock.gif
11ContractedSubBlock.gifExpandedSubBlockStart.gif        IAdvisor 成员#region IAdvisor 成员
12InBlock.gif
13InBlock.gif        public FirWork Recommentd(Customer c)
14ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
15InBlock.gif            return (FirWork)LikeMyStuff.Advise(c);//适配到相应推荐隐形
16ExpandedSubBlockEnd.gif        }

17InBlock.gif
18ExpandedSubBlockEnd.gif        #endregion

19ExpandedSubBlockEnd.gif    }

20ExpandedBlockEnd.gif}
ContractedBlock.gifExpandedBlockStart.gif购买情况推荐产品隐形
 1None.gifusing System;
 2None.gifusing System.Collections.Generic;
 3None.gifusing System.Text;
 4None.gif
 5None.gifnamespace Gof.Test.Strategy
 6ExpandedBlockStart.gifContractedBlock.gifdot.gif{
 7InBlock.gif    public class LikeMyStuff
 8ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 9InBlock.gif        public static object Advise(Customer c)
10ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
11InBlock.gif            return new FirWork(c.Name);//根据客户最近的购买情况推荐产品
12ExpandedSubBlockEnd.gif        }

13ExpandedSubBlockEnd.gif    }

14ExpandedBlockEnd.gif}
ContractedBlock.gifExpandedBlockStart.gif公司正在促销
 1None.gifusing System;
 2None.gifusing System.Collections.Generic;
 3None.gifusing System.Text;
 4None.gif
 5None.gifnamespace Gof.Test.Strategy
 6ExpandedBlockStart.gifContractedBlock.gifdot.gif{
 7InBlock.gif    public class PromotionAdvisor : IAdvisor
 8ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 9InBlock.gif        public static readonly PromotionAdvisor singleton = new PromotionAdvisor();
10InBlock.gif
11InBlock.gif        private FirWork _promoted;
12InBlock.gif
13InBlock.gif        private PromotionAdvisor()
14ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
15InBlock.gif            _promoted = new FirWork("促销产品:在地红");//公司正在促销产品?(可以从数据库或配置文件中确定)
16ExpandedSubBlockEnd.gif        }

17InBlock.gif
18InBlock.gif        public bool HasItem()
19ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
20InBlock.gif            return _promoted != null;
21ExpandedSubBlockEnd.gif        }

22InBlock.gif
23ContractedSubBlock.gifExpandedSubBlockStart.gif        IAdvisor 成员#region IAdvisor 成员
24InBlock.gif
25InBlock.gif        public FirWork Recommentd(Customer c)
26ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
27InBlock.gif            return _promoted;
28ExpandedSubBlockEnd.gif        }

29InBlock.gif
30ExpandedSubBlockEnd.gif        #endregion

31ExpandedSubBlockEnd.gif    }

32ExpandedBlockEnd.gif}
ContractedBlock.gifExpandedBlockStart.gif随机推荐
 1None.gifusing System;
 2None.gifusing System.Collections.Generic;
 3None.gifusing System.Text;
 4None.gif
 5None.gifnamespace Gof.Test.Strategy
 6ExpandedBlockStart.gifContractedBlock.gifdot.gif{
 7InBlock.gif    public class RandomAdvisor : IAdvisor
 8ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 9InBlock.gif        public static readonly RandomAdvisor singleton = new RandomAdvisor();
10InBlock.gif
11ContractedSubBlock.gifExpandedSubBlockStart.gif        IAdvisor 成员#region IAdvisor 成员
12InBlock.gif
13InBlock.gif        public FirWork Recommentd(Customer c)
14ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
15InBlock.gif            return FirWork.GetRandom();
16ExpandedSubBlockEnd.gif        }

17InBlock.gif
18ExpandedSubBlockEnd.gif        #endregion

19ExpandedSubBlockEnd.gif    }

20ExpandedBlockEnd.gif}
ContractedBlock.gifExpandedBlockStart.gifCustomer类,根据条件选择推荐隐形
 1None.gifusing System;
 2None.gifusing System.Collections.Generic;
 3None.gifusing System.Text;
 4None.gif
 5None.gifnamespace Gof.Test.Strategy
 6ExpandedBlockStart.gifContractedBlock.gifdot.gif{
 7InBlock.gif    public class Customer
 8ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 9InBlock.gif        public string Name
10ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
11InBlock.gif            get
12ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
13InBlock.gif                return _name;
14ExpandedSubBlockEnd.gif            }

15InBlock.gif            set
16ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
17InBlock.gif                _name = value;
18ExpandedSubBlockEnd.gif            }

19ExpandedSubBlockEnd.gif        }
private string _name;
20InBlock.gif
21InBlock.gif        //根据特定条件,选择推荐策略
22InBlock.gif        public IAdvisor GetAdvisor()
23ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
24InBlock.gif            if (_advisor == null)//还没有一种策略
25ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
26InBlock.gif                if (PromotionAdvisor.singleton.HasItem())//是否在促销
27ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
28InBlock.gif                    _advisor = PromotionAdvisor.singleton;
29ExpandedSubBlockEnd.gif                }

30InBlock.gif                else if (IsRegistered())//用户是否有注册
31ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
32InBlock.gif                    _advisor = GroupAdvisor.singleton;
33ExpandedSubBlockEnd.gif                }

34InBlock.gif                else if (IsBigSpender())//是否是在买家
35ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
36InBlock.gif                    _advisor = ItemAdvisor.singleton;
37ExpandedSubBlockEnd.gif                }

38InBlock.gif                else//以上全不是,刚随机
39ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
40InBlock.gif                    _advisor = RandomAdvisor.singleton;
41ExpandedSubBlockEnd.gif                }

42ExpandedSubBlockEnd.gif            }

43InBlock.gif            return _advisor;
44ExpandedSubBlockEnd.gif        }
private IAdvisor _advisor;
45InBlock.gif
46InBlock.gif        private bool IsRegistered()
47ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
48InBlock.gif            return false;
49ExpandedSubBlockEnd.gif        }

50InBlock.gif
51InBlock.gif        private bool IsBigSpender()
52ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
53InBlock.gif            return false;
54ExpandedSubBlockEnd.gif        }

55InBlock.gif
56InBlock.gif        public FirWork GetRecommended()
57ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
58InBlock.gif            return GetAdvisor().Recommentd(this);
59ExpandedSubBlockEnd.gif        }

60ExpandedSubBlockEnd.gif    }

61ExpandedBlockEnd.gif}
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

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/1/81254.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息