折腾Java设计模式之策略模式
简介
在策略模式(Strategy Pattern)中,一个类的行为或其算法可以在运行时更改。这种类型的设计模式属于行为型模式。简单理解就是一组算法,可以互换,再简单点策略就是封装算法。
意图 定义一系列的算法,把它们一个个封装起来, 并且使它们可相互替换。
主要解决 在有多种算法相似的情况下,使用 if...else 所带来的复杂和难以维护。
何时使用 一个系统有许多许多类,而区分它们的只是他们直接的行为。
如何解决 将这些算法封装成一个一个的类,任意地替换。
主要角色
- 上下文Context,拥有一个Strategy的引用
- 抽象策略Strategy,往往是一个接口(占大部分情况)或者抽象类,通常提供各种具体策略的接口
- 具体策略,这就是重点了,封装了各种具体的算法
UML
应用实例
- 诸葛亮的锦囊妙计,每一个锦囊就是一个策略;
- 旅行的出游方式,选择骑自行车、坐汽车,每一种旅行方式都是一个策略;
- JAVA AWT 中的 LayoutManager;
优点 1、算法可以自由切换。 2、避免使用多重条件判断。 3、扩展性良好。
缺点 1、策略类会增多。 2、所有策略类都需要对外暴露。
使用场景
- 如果在一个系统里面有许多类,它们之间的区别仅在于它们的行为,那么使用策略模式可以动态地让一个对象在许多行为中选择一种行为。
- 一个系统需要动态地在几种算法中选择一种。
- 如果一个对象有很多的行为,如果不用恰当的模式,这些行为就只好使用多重的条件选择语句来实现。
注意事项: 如果一个系统的策略多于四个,就需要考虑使用混合模式,解决策略类膨胀的问题。
项目描述
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);
}
参考
折腾Java设计模式之策略模式的更多相关文章
- 折腾Java设计模式之状态模式
原文地址 折腾Java设计模式之状态模式 状态模式 在状态模式(State Pattern)中,类的行为是基于它的状态改变的.这种类型的设计模式属于行为型模式.在状态模式中,我们创建表示各种状态的对象 ...
- 折腾Java设计模式之模板方法模式
博客原文地址:折腾Java设计模式之模板方法模式 模板方法模式 Define the skeleton of an algorithm in an operation, deferring some ...
- 折腾Java设计模式之建造者模式
博文原址:折腾Java设计模式之建造者模式 建造者模式 Separate the construction of a complex object from its representation, a ...
- 折腾Java设计模式之备忘录模式
原文地址:折腾Java设计模式之备忘录模式 备忘录模式 Without violating encapsulation, capture and externalize an object's int ...
- 折腾Java设计模式之访问者模式
博客原文地址:折腾Java设计模式之访问者模式 访问者模式 Represent an operation to be performed on the elements of an object st ...
- 折腾Java设计模式之命令模式
博客原文地址 折腾Java设计模式之命令模式 命令模式 wiki上的描述 Encapsulate a request as an object, thereby allowing for the pa ...
- Java设计模式1——策略模式(Strategy Pattern)
最近觅得一本好书<您的设计模式>,读完两章后就能断言,一定是一头极品屌丝写的,而且是专写给开发屌丝男的智慧枕边书,小女子就委屈一下,勉强看看,人笨,谁让他写得这么通俗易懂呢!为了加深理解, ...
- JAVA设计模式 之 策略模式
一. 定义 设计模式定义了算法族,分别封装起来,让他们之间可以互相替代,此模式让算法的变化独立于使用算法的客户(该定义来自于Head First 设计模式). 二. 应用场景 当我们在应用程序中完成一 ...
- Java设计模式之策略模式(一)
今年寒假没有回家,打算利用这个假期的时间进行学习设计模式,这一个人感觉比较牛的知识,前一段时间一直在忙着搞自己的专业课,还有就是捣鼓了一下Linux系统,没有好好的学习关于Java还有Android方 ...
随机推荐
- position 几个属性的作用
//定位一般都会配合left 和 top 一起使用; //静态定位 : 元素默认位置; 不脱标 不常用position:static; //相对定位 : 相对于元素本身之前的位置进行定位;不脱标pos ...
- Winform 窗体获得焦点
给窗体添加Shown事件 public void Form_Shown(object sender, EventArgs e) { this.Activate(); this.Focus(); //定 ...
- Task 的用法
Task的功能喝Thread类似,写法也很简单: 两种方式: 第一 Task t1=new Task(()=>{}); t1.Start();//启动Task t1.Wait();//若调用Wa ...
- .NET 应用架构电子书中文版
<.NET 微服务:容器化 .NET 应用架构指南> 本书主要介绍了基于容器和微服务的应用架构和设计原则,以及基于 .NET Core 和 Docker 容器的应用程序开发实践.为了让大家 ...
- 事件派发dispatchEvent
1.什么是dispatchEvent? dispatch意为"调度"."派遣",event为"事件".所以dispatchEvent即向指定 ...
- [Swift]LeetCode150. 逆波兰表达式求值 | Evaluate Reverse Polish Notation
Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...
- [Swift]LeetCode547. 朋友圈 | Friend Circles
There are N students in a class. Some of them are friends, while some are not. Their friendship is t ...
- [Swift]LeetCode770. 基本计算器 IV | Basic Calculator IV
Given an expression such as expression = "e + 8 - a + 5" and an evaluation map such as {&q ...
- [Swift]LeetCode934. 最短的桥 | Shortest Bridge
In a given 2D binary array A, there are two islands. (An island is a 4-directionally connected grou ...
- Python Bs4 回顾
BeautifulSoup bs4主要使用find()方法和find_all()方法来搜索文档. find()用来搜索单一数据,find_all()用来搜索多个数据 find_all()与find() ...