博客原文地址

简介

在策略模式(Strategy Pattern)中,一个类的行为或其算法可以在运行时更改。这种类型的设计模式属于行为型模式。简单理解就是一组算法,可以互换,再简单点策略就是封装算法。

意图 定义一系列的算法,把它们一个个封装起来, 并且使它们可相互替换。

主要解决 在有多种算法相似的情况下,使用 if...else 所带来的复杂和难以维护。

何时使用 一个系统有许多许多类,而区分它们的只是他们直接的行为。

如何解决 将这些算法封装成一个一个的类,任意地替换。

主要角色

  • 上下文Context,拥有一个Strategy的引用
  • 抽象策略Strategy,往往是一个接口(占大部分情况)或者抽象类,通常提供各种具体策略的接口
  • 具体策略,这就是重点了,封装了各种具体的算法

UML

应用实例

  • 诸葛亮的锦囊妙计,每一个锦囊就是一个策略;
  • 旅行的出游方式,选择骑自行车、坐汽车,每一种旅行方式都是一个策略;
  • JAVA AWT 中的 LayoutManager;

优点 1、算法可以自由切换。 2、避免使用多重条件判断。 3、扩展性良好。

缺点 1、策略类会增多。 2、所有策略类都需要对外暴露。

使用场景

  1. 如果在一个系统里面有许多类,它们之间的区别仅在于它们的行为,那么使用策略模式可以动态地让一个对象在许多行为中选择一种行为。
  2. 一个系统需要动态地在几种算法中选择一种。
  3. 如果一个对象有很多的行为,如果不用恰当的模式,这些行为就只好使用多重的条件选择语句来实现。

注意事项: 如果一个系统的策略多于四个,就需要考虑使用混合模式,解决策略类膨胀的问题。

项目描述

跳转到我的策略模式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));
}
}

执行结果

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("老李");
}
}

执行结果

策略上下文

@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
*/
void travel(String name);
}

参考

策略模式

维基里的策略模式

南乡清水的实际项目运用之Strategy模式

折腾Java设计模式之策略模式的更多相关文章

  1. 折腾Java设计模式之状态模式

    原文地址 折腾Java设计模式之状态模式 状态模式 在状态模式(State Pattern)中,类的行为是基于它的状态改变的.这种类型的设计模式属于行为型模式.在状态模式中,我们创建表示各种状态的对象 ...

  2. 折腾Java设计模式之模板方法模式

    博客原文地址:折腾Java设计模式之模板方法模式 模板方法模式 Define the skeleton of an algorithm in an operation, deferring some ...

  3. 折腾Java设计模式之建造者模式

    博文原址:折腾Java设计模式之建造者模式 建造者模式 Separate the construction of a complex object from its representation, a ...

  4. 折腾Java设计模式之备忘录模式

    原文地址:折腾Java设计模式之备忘录模式 备忘录模式 Without violating encapsulation, capture and externalize an object's int ...

  5. 折腾Java设计模式之访问者模式

    博客原文地址:折腾Java设计模式之访问者模式 访问者模式 Represent an operation to be performed on the elements of an object st ...

  6. 折腾Java设计模式之命令模式

    博客原文地址 折腾Java设计模式之命令模式 命令模式 wiki上的描述 Encapsulate a request as an object, thereby allowing for the pa ...

  7. Java设计模式1——策略模式(Strategy Pattern)

    最近觅得一本好书<您的设计模式>,读完两章后就能断言,一定是一头极品屌丝写的,而且是专写给开发屌丝男的智慧枕边书,小女子就委屈一下,勉强看看,人笨,谁让他写得这么通俗易懂呢!为了加深理解, ...

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

    一. 定义 设计模式定义了算法族,分别封装起来,让他们之间可以互相替代,此模式让算法的变化独立于使用算法的客户(该定义来自于Head First 设计模式). 二. 应用场景 当我们在应用程序中完成一 ...

  9. Java设计模式之策略模式(一)

    今年寒假没有回家,打算利用这个假期的时间进行学习设计模式,这一个人感觉比较牛的知识,前一段时间一直在忙着搞自己的专业课,还有就是捣鼓了一下Linux系统,没有好好的学习关于Java还有Android方 ...

随机推荐

  1. 使用AOP实现方法执行时间和自定义注解

    环境:IDEA2018+JDK1.8+SpringBoot 第一步:在pom文件中引入依赖(度娘有很多(*^▽^*)): <!--引入AOP的依赖--><dependency> ...

  2. 已知一个字符串S 以及长度为n的字符数组a,编写一个函数,统计a中每个字符在字符串中的出现次数

    import java.util.Scanner; /** * @author:(LiberHome) * @date:Created in 2019/3/6 21:04 * @description ...

  3. request,reponse对象中的方法

    1.request对象 客户端的请求信息被封装在request对象中,通过它才能了解到客户的需求,然后做出响应.它是HttpServletRequest类的实例. 序号 方 法 说 明 1   obj ...

  4. [Swift]LeetCode158. 用Read4来读取N个字符II $ Read N Characters Given Read4 II

    The API: int read4(char *buf) reads 4 characters at a time from a file. The return value is the actu ...

  5. [Swift]LeetCode424. 替换后的最长重复字符 | Longest Repeating Character Replacement

    Given a string that consists of only uppercase English letters, you can replace any letter in the st ...

  6. [Swift]LeetCode628. 三个数的最大乘积 | Maximum Product of Three Numbers

    Given an integer array, find three numbers whose product is maximum and output the maximum product. ...

  7. [Swift]LeetCode785. 判断二分图 | Is Graph Bipartite?

    Given an undirected graph, return true if and only if it is bipartite. Recall that a graph is bipart ...

  8. [Swift]LeetCode932. 漂亮数组 | Beautiful Array

    For some fixed N, an array A is beautiful if it is a permutation of the integers 1, 2, ..., N, such ...

  9. 【mysql】Date和String的互相转换(DATE_FORMAT & STR_TO_DATE)

    1.Date  ——>  String 使用的函数:DATE_FORMAT(date,format)     date:需要转换的日期       format:格式化的样式 format样式整 ...

  10. JVM内存知识备忘

    又是一篇备忘... 主要记录一些知识,进行一些资源的汇总. 先来群里liufor大大提供的两张图,清晰易懂: Dockerized Java https://www.youtube.com/watch ...