当前位置:网站首页>策略模式_
策略模式_
2022-07-25 23:11:00 【wfsm】
引用:https://blog.csdn.net/qq_51165184/article/details/123997917
策略模式 Stragery : 针对一组算法,将每一个算法封装到具有公共接口的独立类中,使得他们可以互换
本质: 分离算法,选择实现
策略模式可以把行为和环境分割开来,环境类Context负责查询要做什么,各种算法则在具体策略类(ConcreteStrategy)中提供
策略模式的特点
- 具体算法从具体业务中独立
- 多个
if-else出现考虑使用策略模式 - 策略算法是相同行为的不同实现(多态)
- 客户端选择。上下文来具体实现策略算法
优缺点
优点:
- 避免让客户端涉及到重要算法和数据
- 避免使用难以维护的多重条件选择语句
- 易扩展
缺点
- 判断逻辑在客户端,需求改变时,要更改客户端的程序
- 客户端必须知道所有策略类,并自行决定使用哪一个策略类,这就意味着客户端必须理解这些算法的区别,以便适时的选择恰当的算法类
- 增加了对象的数目
- 只适合扁平的算法结构
代码:
折扣策略接口:
public interface DiscountStrategy {
BigDecimal getDiscount(BigDecimal total);
}
两种打折策略:
public class OverDiscountStrategy implements DiscountStrategy{
@Override
public BigDecimal getDiscount(BigDecimal total) {
// 满100减20
return total.compareTo(BigDecimal.valueOf(100)) >= 0?BigDecimal.valueOf(20): BigDecimal.ZERO;
}
}
public class UserDiscountStrategy implements DiscountStrategy{
@Override
public BigDecimal getDiscount(BigDecimal total) {
// 会员打九折
return total.multiply(new BigDecimal("0.1")).setScale(2, RoundingMode.HALF_UP);
}
}
环境设置默认使用的策略,,并且可以切换策略:
public class DiscountContext {
// 持有某个策略
private DiscountStrategy strategy = new UserDiscountStrategy();
// 允许客户端设置新策略
public void setStrategy(DiscountStrategy discountStrategy){
this.strategy = discountStrategy;
}
public BigDecimal calculatePrice(BigDecimal total){
return total.subtract(strategy.getDiscount(total));
}
}
测试:
public static void main(String[] args) {
DiscountContext context = new DiscountContext();
context.setStrategy(new OverDiscountStrategy());
BigDecimal result = context.calculatePrice(BigDecimal.valueOf(200));
System.out.println("result = " + result);
}
边栏推荐
- anaconda安装教程环境变量(如何配置环境变量)
- How to set pseudo static for WordPress fixed links
- Review of static routing
- Network Security Learning (11) scanning and blasting
- r语言绘图参数(R语言plot画图)
- 多模态——Deep Multi-Modal Sets
- Mongodb的特点、与MySQL的差别、以及应用场景
- The difference between "= =" and equals
- Learning notes of technical art hundred people plan (2) -- vector
- MathType installation and solution cannot solve the problem of crtl+v
猜你喜欢

Enterprise level inventory management system of code audit

第二周学习:卷积神经网络

WordPress removes the website publishing time

About using NPM command under the terminal, the installation error problem is solved (my own experience)

The third experiment OSPF

Secure code warrior learning record (IV)

Stack and stack class

wordpress去掉网站发布时间

Apple CMS V10 template /mxone Pro adaptive film and television website template

AI首席架构师12-AICA-工业生产过程优化场景下产业落地解析
随机推荐
PHP wechat scan code, follow official account and authorize login source code
Secure code warrior learning record (III)
栈与Stack类
Zero crossing position search of discrete data (array)
Ffmpeg first learning (only for coding)
ETL工具(数据同步) 二
QT operation to solve large amount of duplicate data
How painful is it to write unit tests?
Mongodb的特点、与MySQL的差别、以及应用场景
CSV generated by PHP cannot completely display the number with leading 0
ETL tool (data synchronization) II
CTS测试方法「建议收藏」
QT string operation
Network Security Learning (16)
Network Security Learning (XII) OSI and TCP
AI chief architect 12 AICA industrial landing analysis under the industrial production process optimization scenario
uvm_hdl——DPI在UVM中的实现(四)
Summary of my 2020 online summer camp
通用分页功能
Take away applet with main version of traffic / repair to add main access function of traffic