当前位置:网站首页>行为型模式-策略模式
行为型模式-策略模式
2022-08-02 09:50:00 【vbirdbest】
定义
角色
- 抽象策略(Strategy)类:这是一个抽象角色,通常由一个接口或抽象类实现。此角色给出所有的具体策略类所需的接口。
- 具体策略(Concrete Strategy)类:实现了抽象策略定义的接口,提供具体的算法实现或行为。
- 环境(Context)类:持有一个策略类的应用,最终给客户端调用。
案例
针对不同的节日退出不同的促销活动。
public interface Strategy {
public void show();
}
/** * 具体策略类 */
public class StrategyA implements Strategy {
@Override
public void show() {
System.out.println("买一赠一");
}
}
/** * 具体策略类 */
public class StrategyB implements Strategy {
@Override
public void show() {
System.out.println("满100减50");
}
}
/** * 上下文环境 */
public class SalesMan {
// 聚合策略类对象
private Strategy strategy;
public SalesMan(Strategy strategy) {
this.strategy = strategy;
}
// 调用具体的策略
public void showPromotion() {
strategy.show();
}
public Strategy getStrategy() {
return strategy;
}
public void setStrategy(Strategy strategy) {
this.strategy = strategy;
}
}
public class Client {
public static void main(String[] args) {
SalesMan salesMan = new SalesMan(new StrategyA());
salesMan.showPromotion();
}
}
优缺点
优点:
策略类之间可以自由切换:由于策略类都实现了同一个接口,所以他们之间可以自由切换。(本来就是实现类实现接口,多态本来都可以自由切换,这也不只是策略特有的)。
易于扩展:增加一个新的实现只需要添加一个具体的策略类即可,基本上不需要改变原有的代码,符合开闭原则。(增加新的实现类对原程序本来影响就小,这就是多态的特性)
避免使用多重条件选择语句(if else),充分体现面向对象设计思想。只是将原本的if else 分支的代码进一步向上抽象到类中,使得整体if else 代码简洁了而已,仍然去不掉if else,想去掉还要靠其它方式。
缺点
- 客户端必须知道所有的策略类,并自行决定使用哪一个策略类。
- 策略模式或造成产生很多个策略类,可以通过使用享元模式在一定程度上减少对象的数量。
使用场景
- 一个系统需要动态的在几种算法中选择一种时,可将每个算法封装到策略类中。
- 一个类定义了多种行为,并且这些行为在这个类的操作中可以多个条件语句的形式出现,可将每个条件分支移入它们各自的策略类找那个以代替这些条件语句。
- 系统中各算法彼此独立,且要求对客户隐藏具体的算法的实现细节。
边栏推荐
- HikariCP database connection pool, too fast!
- R language ggplot2 visualization: use the ggbarplot function of the ggpubr package to visualize the horizontal column chart (bar chart), use the orientation parameter to set the column chart to be tra
- mysql连接池的实现
- AutoJs学习-密码生成器
- Spearman's correlation coefficient
- 享年94岁,图灵奖得主、计算复杂性理论先驱Juris Hartmanis逝世
- 【云原生】快出数量级的性能是怎样炼成的?就提升了亿点点
- The love-hate relationship between C language volatile keyword, inline assembly volatile and compiler
- QT专题:自定义部件
- AutoJs学习-AES加解密
猜你喜欢

迭代器失效问题

It's time for bank data people who are driven crazy by reporting requirements to give up using Excel for reporting

QT专题:事件机制event基础篇

Supervised learning of Li Hang's "Statistical Learning Methods" Notes

LeetCode第三题(Longest Substring Without Repeating Characters)三部曲之一:解题思路

曲折的tensorflow安装过程(Tensorflow 安装问题的解决)

Re23:读论文 How Does NLP Benefit Legal System: A Summary of Legal Artificial Intelligence

稳定币:对冲基金做空 Tether 的结局会是什么?

iNFTnews | Seeing the two sides of the metaverse, what is the true Internet and the Internet of value?

QT专题:自定义部件
随机推荐
软件测试X模型
适配器模式适配出栈和队列及优先级队列
leetcode 62. Unique Paths(独特的路径)
8月份的.NET Conf 活动 专注于 .NET MAUI
8月份的.NET Conf 活动 专注于 .NET MAUI
QT专题:事件机制event基础篇
Weak yen turns game consoles into "financial products" in Japan: scalpers make big profits
MySql tens of millions of paging optimization, fast insertion method of tens of millions of data
李航《统计学习方法》笔记之朴素贝叶斯法
ConvNeXt论文及实现
一文带你了解推荐系统常用模型及框架
LeetCode第三题(Longest Substring Without Repeating Characters)三部曲之一:解题思路
R language ggplot2 visualization: use the ggtexttable function of the ggpubr package to visualize tabular data (directly draw tabular graphs or add tabular data to images), use tbody_add_border to add
第十五章 多线程
Implementation of mysql connection pool
node封装一个图片拼接插件
Unknown content monitoring
armv7与armv8的区别(v8和w12的区别)
R语言ggpubr包的ggline函数可视化分组折线图、add参数为mean_se和dotplot可视化不同水平均值的折线图并为折线图添加误差线(se标准误差)和点阵图、自定义palette设置颜色
win10打印服务无法启动(运行时错误automation)