简介
  
  在策略模式(Strategy Pattern)中,一个类的行为或其算法可以在运行时更改。这种类型的设计模式属于行为型模式。简单理解就是一组算法,可以互换,再简单点策略就是封装算法。
  
  意图 定义一系列的算法,把它们一个个封装起来, 并且使它们可相互替换。
  
  主要解决 在有多种算法相似的情况下,使用 if...else 所带来的复杂和难以维护。
  
  何时使用 一个系统有许多许多类,而区分它们的只是他们直接的行为。
  
  如何解决 将这些算法封装成一个一个的类,任意地替换。
  
  主要角色
  
  上下文Context,拥有一个Strategy的引用
  
  抽象策略Strategy,往往是一个接口(占大部分情况)或者抽象类,通常提供各种具体策略的接口
  
  具体策略,这就是重点了,封装了各种具体的算法
  
  UML
  
  img
  
  应用实例
  
  诸葛亮的锦囊妙计,每一个锦囊就是一个策略;
  
  旅行的出游方式,选择骑自行车、坐汽车,每一种旅行方式都是一个策略;
  
  JAVA AWT 中的 LayoutManager;
  
  优点 1、算法可以自由切换。 2、避免使用多重条件判断。 3、扩展性良好。
  
  缺点 1、策略类会增多。 2、所有策略类都需要对外暴露。
  
  使用场景
  
  如果在一个系统里面有许多类,它们之间的区别仅在于它们的行为,那么使用策略模式可以动态地让一个对象在许多行为中选择一种行为。
  
  一个系统需要动态地在几种算法中选择一种。
  
  如果一个对象有很多的行为,如果不用恰当的模式,这些行为就只好使用多重的条件选择语句来实现。
  
  注意事项: 如果一个系统的策略多于四个,就需要考虑使用混合模式,解决策略类膨胀的问题。
  
  项目描述
  
  跳转到我的策略模式GitHub
  
  1.操作行为
  
  simple1包,主要对操作行为包装了加减乘除方法。
  
  @Slf4j
  
  public class Application {
  
  public static void main(String[] args) {
  
  Context context = new Context(new AddStrategy());
  
  log.info("10 + 5 = {}", context.executeStrategy(10, 5));
  
  context.setStrategy(new SubstractStrategy());
  
  log.info("10 - 5 = {}", context.executeStrategy(10, 5));
  
  context.setStrategy(new MultiplyStrategy());
  
  log.info("10 * 5 = {}", context.executeStrategy(10, 5));
  
  context.setStrategy(new DivideStrategy());
  
  log.info("10 / 5 = {}", context.executeStrategy(10, 5));
  
  }
  
  }
  
  执行结果
  
  img
  
  2.出现方式
  
  simple2包描述,主要对出行方式的包装,包装了3种出行方式,
  
  执行类
  
  public class TravelApplication {
  
  public static void main(String[] args) {
  
  Context context = new Context(new BusStrategy());
  
  context.executeStrategy("老王");
  
  context.setStrategy(new BicycleStrategy());
  
  context.executeStrategy("老张");
  
  context.setStrategy(new WalkStrategy());
  
  context.executeStrategy("老李");
  
  }
  
  }
  
  执行结果
  
  <u>image-20181222104657926</u>
  
  策略上下文
  
  @Data
  
  public class Context {
  
  private Strategy strategy;
  
  public Context(Strategy strategy) {
  
  this.strategy = strategy;
  
  }
  
  /**
  
  * 出行
  
  *
  
  * @return
  
  */
  
  public void executeStrategy(String name) {
  
  strategy.travel(name);
  
  }
  
  }
  
  抽象策略
  
  public interface Strategy {
  
  /**
  
  * 出现方法
  
  *
  
  * @return
  
  */
private[spark] class Executor(
executorId: String,
executorHostname: String,
env: SparkEnv,
userClassPath: Seq[URL] = Nil,
isLocal: Boolean = false)
extends Logging {
......
// must be initialized before running startDriverHeartbeat()
//创建心跳的 EndpointRef
private val heartbeatReceiverRef = RpcUtils.makeDriverRef(www.haomem178.cn HeartbeatReceiver.ENDPOINT_NAME, conf, env.rpcEnv)
......
startDriverHeartbeater()
......
/**
* Schedules a task to report heartbeat and partial metrics for active tasks to driver.
* 用一个 task 来报告活跃任务的信息以及发送心跳。
*/
private def startDriverHeartbeater(): Unit = {
val intervalMs = conf.getTimeAsMs("spark.executor.heartbeatInterval", "10s")

// Wait a random interval so the heartbeats don't end up in sync
val initialDelay = intervalMs + (math.random * intervalMs).asInstanceOf[Int]

val heartbeatTask = new Runnable() {
override def run(): Unit = Utils.logUncaughtExceptions(reportHeartBeat())
}
//heartbeater是一个单线程线程池,scheduleAtFixedRate 是定时执行任务用的,和 schedule 类似,只是一些策略不同。
heartbeater.scheduleAtFixedRate(heartbeatTask, initialDelay, intervalMs, TimeUnit.MILLISECONDS)
}
......
}
可以看到,在 Executor 中会创建心跳的 EndpointRef ,变量名为 heartbeatReceiverRef 。

然后我们主要看 startDriverHeartbeater() 这个方法,它是关键。
我们可以看到最后部分代码

val heartbeatTask = new Runnable(www.fengshen157.com/) {
override def run(): Unit = Utils.logUncaughtExceptions(reportHeartBeat())
}
heartbeater.scheduleAtFixedRate(heartbeatTask, initialDelay, intervalMs, TimeUnit.MILLISECONDS)
heartbeatTask 是一个 Runaable,即一个线程任务。scheduleAtFixedRate 则是 java concurrent 包中用来执行定时任务的一个类,这里的意思是每隔 10s 跑一次 heartbeatTask 中的线程任务,超时时间 30s 。

为什么到这里还是没看到 heartbeatReceiverRef 呢,说好的发送心跳呢?别急,其实在 heartbeatTask 线程任务中又调用了另一个方法,我们到里面去一探究竟。

private[spark] class Executor(
executorId: String,
executorHostname: String,
env: SparkEnv,
userClassPath: Seq[URL] = Nil,
isLocal: Boolean = false)
extends Logging {
......
private def reportHeartBeat(): Unit = {
// list of (task id, accumUpdates) to send back to the driver
val accumUpdates = new ArrayBuffer[(Long, Seq[AccumulatorV2[_, _]])]()
val curGCTime = computeTotalGcTime()

for (taskRunner <- runningTasks.values().asScala) {
if (taskRunner.task != null) {
taskRunner.task.metrics.mergeShuffleReadMetrics()
taskRunner.task.metrics.setJvmGCTime(curGCTime - taskRunner.startGCTime)
accumUpdates += ((taskRunner.taskId, taskRunner.task.metrics.accumulators()))
}
}

val message = Heartbeat(executorId, accumUpdates.toArray, env.blockManager.blockManagerId)
try {
//终于看到 heartbeatReceiverRef 的身影了
val response = heartbeatReceiverRef.askWithRetry[HeartbeatResponse](
message, RpcTimeout(conf, "spark.executor.heartbeatInterval", "10s"))
if (response.reregisterBlockManager) {
logInfo("Told to re-register on heartbeat")
env.blockManager.reregister()
}
heartbeatFailures = 0
} catch {
case NonFatal(e) =>
logWarning(www.gxgjpt1.com "Issue communicating www.078881.cn/ with driver in heartbeater", e)
heartbeatFailures += 1
if (heartbeatFailures >= HEARTBEAT_MAX_FAILURES) {
logError(s"Exit as unable to send heartbeats to driver " +
s"more than $HEARTBEAT_MAX_FAILURES times")
System.exit(ExecutorExitCode.HEARTBEAT_FAILURE)
  
  }
  
  参考
  
  策略模式
  
  维基里的策略模式
  
  南乡清水的实际项目运用之Strategy模式

Strategy Pattern ava设计模式之策略模式的更多相关文章

  1. 乐在其中设计模式(C#) - 策略模式(Strategy Pattern)

    原文:乐在其中设计模式(C#) - 策略模式(Strategy Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 策略模式(Strategy Pattern) 作者:webabc ...

  2. 设计模式之策略模式和状态模式(strategy pattern & state pattern)

    本文来讲解一下两个结构比较相似的行为设计模式:策略模式和状态模式.两者单独的理解和学习都是比较直观简单的,但是实际使用的时候却并不好实践,算是易学难用的设计模式吧.这也是把两者放在一起介绍的原因,经过 ...

  3. 设计模式 (一)——策略模式(Strategy,行为型)

    1.概述 使用设计模式可以提高代码的可复用性.可扩充性和可维护性.策略模式(Strategy Pattern)属于行为型模式,其做法是将类所需的行为或者算法一个个封装成单独的类,并将其作为类的数据成员 ...

  4. 设计模式:策略模式(Strategy)

    定   义:它定义了算法家族,分别封装起来,让它们之间可以互相替换,此模式让算法的变化, 不会影响到使用算法的客户. 示例:商场收银系统,实现正常收费.满300返100.打8折.......等不同收费 ...

  5. JavaScript设计模式之策略模式(学习笔记)

    在网上搜索“为什么MVC不是一种设计模式呢?”其中有解答:MVC其实是三个经典设计模式的演变:观察者模式(Observer).策略模式(Strategy).组合模式(Composite).所以我今天选 ...

  6. 【设计模式】【应用】使用模板方法设计模式、策略模式 处理DAO中的增删改查

    原文:使用模板方法设计模式.策略模式 处理DAO中的增删改查 关于模板模式和策略模式参考前面的文章. 分析 在dao中,我们经常要做增删改查操作,如果每个对每个业务对象的操作都写一遍,代码量非常庞大. ...

  7. python设计模式之策略模式

    每次看到项目中存在大量的if else代码时,都会心生一丝不安全感. 特别是产品给的需求需要添加或者更改一种if条件时,生怕会因为自己的疏忽而使代码天崩地裂,哈哈,本文的目的就是来解决这种不安全感的, ...

  8. PHP设计模式之策略模式

    前提: 在软件开发中也常常遇到类似的情况,实现某一个功能有多种算法或者策略,我们可以根据环境或者条件的不同选择不同的算法或者策略来完成该功能.如查 找.排序等,一种常用的方法是硬编码(Hard Cod ...

  9. JavaScript设计模式之策略模式

    所谓"条条道路通罗马",在现实中,为达到某种目的往往不是只有一种方法.比如挣钱养家:可以做点小生意,可以打分工,甚至还可以是偷.抢.赌等等各种手段.在程序语言设计中,也会遇到这种类 ...

随机推荐

  1. Vue视图

    1. 基本模板语法 1.1 插值 文本 数据绑定最常见的形式就是使用“Mustache”语法 (双大括号) 的文本插值 v-text 指令也可以用于数据绑定,如果要更新部分的 textContent ...

  2. C#Framework4.0支持异步async/await语法

    由于用户使用的是XP系统,但是程序里异步都是通过async/await代码来实现的,然而async/await需要Framework4.5版本才可以,而XP系统最高只能支持到Framework4.0, ...

  3. 个人安装ss的一个记录

    在ubuntu16.04安装ss服务.由于lantern最近极其不稳定(我还花钱的qaq),经常断联以至于几乎废了,莫得办法,只好花钱搭一个了orz...呵,贫穷.... 安装shadowsocks ...

  4. 433. Number of Islands【LintCode java】

    Description Given a boolean 2D matrix, 0 is represented as the sea, 1 is represented as the island. ...

  5. 2.airflow参数简介

    比较重要的参数: 参数 默认值 说明 airflow_home /home/airflow/airflow01 airflow home,由环境变量$AIRFLOW_HOME决定 dags_folde ...

  6. python2/3 发送https请求时,告警关闭方法

    问题: 使用Python3 requests发送HTTPS请求,已经关闭认证(verify=False)情况下,控制台会输出以下错误: InsecureRequestWarning: Unverifi ...

  7. Farm Irrigation ZOJ 2412(DFS连通图)

    Benny has a spacious farm land to irrigate. The farm land is a rectangle, and is divided into a lot ...

  8. Bate版本控制报告

    报告beta阶段2周中,项目的版本控制情况,不包括未在coding.net的部分. 包括不限于:check in (不是push)次数; 总词数为29次 check in log(时间.人员.mess ...

  9. mysql You can't specify target table 'xxx' for update in FROM clause

    含义:您不能在子句中为更新指定目标表'xxx'. 错误描述:删除语句中直接含select,如下: DELETE FROM meriadianannotation WHERE SeriesID IN ( ...

  10. 【树上DFS】Tree and Polynomials

    http://codeforces.com/gym/101372 D push1[i][k]:所有操作1总共要让节点i下推多少系数k push2[i][k]:所有操作2总共要让节点i上推多少系数k s ...