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 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 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 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 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 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.
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
Strategy 定义一系列算法或策略,把它们封闭起来,并且使它们相互可以替换。各算法或策略可以独立于客户程序而变化。...相关推荐
- 总结 贪心算法_这几道经典例题帮你轻松搞透贪心算法
贪心算法概念叙述 运用贪心算法求解问题时,会将问题分为若干个子问题,可以将其想象成俄罗斯套娃,利用贪心的原则从内向外依次求出当前子问题的最优解,也就是该算法不会直接从整体考虑问题,而是想要达到局部最优 ...
- 策略模式和工厂模式的区别_java设计模式之状态模式,策略模式孪生兄弟
状态模式 状态模式(State Pattern)中,类的行为是基于它的状态改变的,状态之间的切换,在状态A执行完毕后自己控制状态指向状态B,状态模式是不停的切换状态执行.这种类型的设计模式属于行为型模 ...
- java jvm垃圾回收算法_深入理解JVM虚拟机2:JVM垃圾回收基本原理和算法
本文转自互联网,侵删 本系列文章将整理到我在GitHub上的<Java面试指南>仓库,更多精彩内容请到我的仓库里查看 喜欢的话麻烦点下Star哈 文章将同步到我的个人博客: www.how ...
- 一周刷爆LeetCode,算法da神左神(左程云)耗时100天打造算法与数据结构基础到高级全家桶教程,直击BTAJ等一线大厂必问算法面试题真题详解 笔记
一周刷爆LeetCode,算法大神左神(左程云)耗时100天打造算法与数据结构基础到高级全家桶教程,直击BTAJ等一线大厂必问算法面试题真题详解 笔记 教程与代码地址 P1 出圈了!讲课之外我们来聊聊 ...
- python一元线性回归算法_6一元线性回归_清华编程高手尹成带你基于算法实践python量化交易_其他视频-51CTO学院...
清华编程高手尹成带你基于算法实践python量化交易 量化交易是指以先进的数学模型替代人为的主观判断,利用计算机技术从庞大的历史数据中海选能带来超额收益的多种"大概率"事件以制定策 ...
- 人工智能算法之梯度下降法、协同过滤、相似度技术、ALS算法(附案例分析)、模型存储与加载、推荐系统的冷启动问题
梯度下降法 求解机器学习算法的模型参数,即无约束优化问题时,梯度下降法是最常采用的方法之一,另一种常用的方法是最小二乘法.这里对梯度下降法做简要介绍. 最小二乘法法适用于模型方程存在解析解的情况.如果 ...
- matlab 随机森林算法_(六)如何利用Python从头开始实现随机森林算法
博客地址:https://blog.csdn.net/CoderPai/article/details/96499505 点击阅读原文,更好的阅读体验 CoderPai 是一个专注于人工智能在量化交易 ...
- halcon机器视觉算法原理与编程实战_快速弄懂机器学习里的集成算法:原理、框架与实战...
作者: 博观厚积 简书专栏:https://www.jianshu.com/u/2f376f777ef1 1. 关于集成学习算法 集成学习算法,通俗地讲就是:三个臭皮匠,顶个诸葛亮,这在很多地方都有 ...
- labuladong的算法小抄pdf_真漂亮!这份GitHub上爆火的算法面试笔记,助你圆满大厂梦...
前言 Github作为程序员们的后花园,一直以来都是程序员最喜欢逛逛.学习的地方,小编也不例外,最近看到一份对标BAT等一线大厂的算法面试笔记,已经标星68+K了,很是惊讶,看了一下,觉得知识点整理得 ...
最新文章
- basequickadapter详解_在kotlin中如何使用BaseQuickAdapter适配器
- poj 1698 Alice's Chance 最大流
- 解决因删除外键引用的时候导致相应字段被删除了的问题
- 飞鸽传书(IPMSG)协议(翻译稿)
- 左手菲尔兹右手突破奖,这个中国女婿其实是英国贵族?拿到300万奖金后他这样说……...
- 论文浅尝 | 基于属性嵌入的知识图谱实体对齐
- 如何解决SVN 清理失败
- 死锁(python 版)
- datables自定义ajax,JQuery DataTables.net自定义列宽度在ajax加载后不起作用
- SpringBoot2.0系列(03)---SpringBoot之使用freemark视图模板
- 关于java AudioInputStream播放短音频没声音的问题
- 《深度学习笔记》——初识PyTorch的学习笔记
- 日本研发圆滚滚的球形无人机,被LED屏团团包围
- rdd分组聚合算子xxByKey,xxBy
- JZOJ4991. Skyfall
- java 自动化报表_SpringMvc JDBC jQWidgets项目案例JasperReport自动化报表系统
- 禁止chrome更新呢
- 聚合供应链,Saas系统,商品供应链
- 序列1 - 以太坊的诞生,史上最大ICO之一
- Qt5.4.1播放视频语音文件