当前位置:网站首页>行为型模式-策略模式
行为型模式-策略模式
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,想去掉还要靠其它方式。
缺点
- 客户端必须知道所有的策略类,并自行决定使用哪一个策略类。
- 策略模式或造成产生很多个策略类,可以通过使用享元模式在一定程度上减少对象的数量。
使用场景
- 一个系统需要动态的在几种算法中选择一种时,可将每个算法封装到策略类中。
- 一个类定义了多种行为,并且这些行为在这个类的操作中可以多个条件语句的形式出现,可将每个条件分支移入它们各自的策略类找那个以代替这些条件语句。
- 系统中各算法彼此独立,且要求对客户隐藏具体的算法的实现细节。
边栏推荐
- 1对1视频源码——快速实现短视频功能提升竞争力
- Daily practice of dynamic programming (2)
- STL中list实现
- The ggline function of the R language ggpubr package visualizes grouped line graphs, the add parameter is mean_se and dotplot to visualize line graphs of different level averages, and adds error bars
- R language ggplot2 visualization: based on the fill parameter and shape parameter in the aes function, custom draw a grouped line chart and add data points (scatter points), use the legend.position fu
- R语言使用ggpubr包的ggtexttable函数可视化表格数据(直接绘制表格图或者在图像中添加表格数据)、设置theme主题参数自定义表格中表头内容的填充色(使用colnames.style参数)
- Have you ever learned about these architecture designs and architecture knowledge systems?(Architecture book recommendation)
- 日元疲软令游戏机在日本变身“理财产品”:黄牛大赚
- function call to print lua internal structure
- R语言使用zoo包中的rollapply函数以滚动的方式、窗口移动的方式将指定函数应用于时间序列、设置align参数指定结果数据中的时间标签取自窗口中的位置(参数right指定取自窗口的最右侧)
猜你喜欢

List-based queuing and calling system

李航《统计学习方法》笔记之监督学习Supervised learning

1对1视频源码——快速实现短视频功能提升竞争力

ConvNeXt论文及实现

重磅大咖来袭!阿里云生命科学与智能计算峰会精彩内容剧透

Naive Bayesian Method of Li Hang's "Statistical Learning Methods" Notes

周鸿祎称微软抄袭 360 安全模式后发文否认;英特尔CEO基辛格回应市值被AMD超越:股价下跌是咎由自取|极客头条...

阿里巴巴 CTO 程立:开源是基础软件的源头!

net start mysql MySQL 服务正在启动 . MySQL 服务无法启动。 服务没有报告任何错误。

Two-dimensional array piecemeal knowledge sorting
随机推荐
The R language uses the rollapply function in the zoo package to apply the specified function to the time series in a rolling manner and the window moves, and set the align parameter to specify that t
牛客网项目2.7开发注册功能 报错This application has no explicit mapping for /error......
李航《统计学习方法》笔记之感知机perceptron
AutoJs学习-实现谢尔宾斯基三角
ConvNeXt论文及实现
iNFTnews | Seeing the two sides of the metaverse, what is the true Internet and the Internet of value?
Smoothing of time series data in R language: smoothing time series data to remove noise using the dpill function and locpoly function of the KernSmooth package
STL中list实现
第十六章 协程
1对1视频源码——快速实现短视频功能提升竞争力
【新版干货书】深度伪造 (DeepFakes):创造,检测和影响
Getting Started with SCM from Scratch (1): Summary of Background Knowledge
One Summer of Open Source | How to Quickly Integrate Log Modules in GO Language Framework
【New Edition】DeepFakes: Creation, Detection and Influence
Daily practice of dynamic programming (2)
8月份的.NET Conf 活动 专注于 .NET MAUI
The ggbarplot function of the R language ggpubr package visualizes the grouped histogram, sets the add parameter to mean_se to visualize the histogram of the mean values of different levels and adds
qq邮箱日发5万邮件群发技术(qq邮箱怎样定时发送邮件)
使用scrapy 把爬到的数据保存到mysql 防止重复
node制作一个视频帧长图生成器