当前位置: 首页 > news >正文

《设计模式》之策略模式

策略模式定义

比如对象的某个行为,在不同场景有不同实现方式,可以将这些行为的具体实现定义为一组策略,每个实现类实现种策略,在不同场景使用不同的实现,并且可以自由切换策略。

策略模式结构

策略模式需要一个策略接口,不同的策略实现不同的实现类,在具体业务环境中仅持有该策略接口,根据不同的场景使用不同的实现类即可。
在这里插入图片描述

面向接口编程,而不是面向实现。

优点

1、干掉繁琐的 if、switch 判断逻辑;

2、代码优雅、可复用、可读性好;

3、符合开闭原则(对修改关闭, 对扩展开放),扩展性好、便于维护;

缺点

1、策略如果很多的话,会造成策略类膨胀;

2、使用者必须清楚所有的策略类及其用途;

策略模式代码示例

  • 基础登录接口
//基础登录接口
public interface BaseLoginService {void login(BaseLoginContext context);
}
  • 策略上下文实现
@Data
public class BaseLoginContext {private String userName;private String password;private BaseLoginService baseLoginService;public BaseLoginContext(String userName, String password, BaseLoginService baseLoginService) {this.userName = userName;this.password = password;this.baseLoginService = baseLoginService;}public void login(){baseLoginService.login(this);}
}
  • 定义具体的策略实现类
  1. 实现类1
public class SimpleLoginServiceImpl implements BaseLoginService {@Overridepublic void login(BaseLoginContext context) {System.out.println("简单登录,当前登录的人:" + context.getUserName() + ",密码:" + context.getPassword());}
}
  1. 实现类2
public class HardLoginServiceImpl implements BaseLoginService {@Overridepublic void login(BaseLoginContext context) {System.out.println("复杂登录,当前登录的人:" + context.getUserName() + ",密码:" + context.getPassword());}
}
  • 调用
public static void main(String[] args) {BaseLoginService simple = new SimpleLoginServiceImpl();BaseLoginContext simpleContext = new BaseLoginContext("simple","simple",simple);simpleContext.login();BaseLoginService hard = new HardLoginServiceImpl();BaseLoginContext hardContext = new BaseLoginContext("hard","hard",hard);hardContext.login();}
  • 结果
    在这里插入图片描述

扩展策略

方式1

在策略的算法实现上添加自己需要的数据的方式

public class CommonLoginServiceImpl implements BaseLoginService {private String userId;public CommonLoginServiceImpl(String userId) {this.userId = userId;}public String getUserId() {return userId;}@Overridepublic void login(BaseLoginContext context) {System.out.println("复杂登录,当前登录的人:" + context.getUserName() + ",账号:" + getUserId() + ",密码:" + context.getPassword());}
}
  • 调用
public static void main(String[] args) {BaseLoginService simple = new SimpleLoginServiceImpl();BaseLoginContext simpleContext = new BaseLoginContext("simple","simple",simple);simpleContext.login();BaseLoginService hard = new HardLoginServiceImpl();BaseLoginContext hardContext = new BaseLoginContext("hard","hard",hard);hardContext.login();BaseLoginService common = new CommonLoginServiceImpl("001");BaseLoginContext commonContext = new BaseLoginContext("common","common",common);commonContext.login();}
  • 结果
    在这里插入图片描述

方式2

扩展上下文的方式

public class NewLoginContext extends BaseLoginContext{private String userId;public String getUserId() {return userId;}public void setUserId(String userId) {this.userId = userId;}public NewLoginContext(String userName, String password, String userId, BaseLoginService baseLoginService) {super(userName, password, baseLoginService);this.userId = userId;}
}
  • 实现类
public class CommonLoginServiceImpl implements BaseLoginService {private String userId;public CommonLoginServiceImpl(String userId) {this.userId = userId;}public String getUserId() {return userId;}@Overridepublic void login(BaseLoginContext context) {NewLoginContext newLoginContext = (NewLoginContext)context;
//        System.out.println("common1登录,当前登录的人:" + context.getUserName() + ",账号:" + getUserId() + ",密码:" + context.getPassword());System.out.println("common2登录,当前登录的人:" + context.getUserName() + ",账号:" + newLoginContext.getUserId() + ",密码:" + context.getPassword());}
}
  • 调用
public static void main(String[] args) {BaseLoginService simple = new SimpleLoginServiceImpl();BaseLoginContext simpleContext = new BaseLoginContext("simple","simple",simple);simpleContext.login();BaseLoginService hard = new HardLoginServiceImpl();BaseLoginContext hardContext = new BaseLoginContext("hard","hard",hard);hardContext.login();//        BaseLoginService common1 = new CommonLoginServiceImpl("001");
//        BaseLoginContext commonContext1 = new BaseLoginContext("common","common",common1);
//        commonContext1.login();BaseLoginService common2 = new CommonLoginServiceImpl("001");NewLoginContext commonContext2 = new NewLoginContext("common","common","002",common2);commonContext2.login();}
  • 结果
    在这里插入图片描述
http://www.lryc.cn/news/273742.html

相关文章:

  • Django文章标签推荐
  • Git、TortoiseGit进阶
  • 山区老人爱的礼物丨守护银龄,情暖寒冬
  • 【计算机算法设计与分析】n皇后问题(C++_回溯法)
  • Calendar日历类型常见方法
  • Docker-Compose部署Redis(v7.2)主从模式
  • Spring国际化的应用及原理详解
  • Existing installation is up to date
  • windows安装kafka以及kafka管理工具推荐
  • 面向对象的三大特征之一多态
  • vue3中标签form插件
  • 企业数字化转型:1个核心、2种力量、3个关键点、4大转型、5大平台
  • Agilent安捷伦E4990A阻抗分析仪20Hz
  • 性能优化-OpenMP概述(一)-宏观全面理解OpenMP
  • Prometheus实战篇:Prometheus监控nginx
  • JVM加载class文件的原理机制
  • 如何使用CapSolver解决Web爬虫中遇到的CAPTCHA问题
  • 杰发科技AC7801——IO模拟IIC注意事项
  • 展台搭建与设计都有哪些思路
  • 解决mock单元测试中 无法获取实体类xxx对应的表名
  • arm64虚拟化技术与kvm实现原理分享
  • 选择 省市区 组件数据 基于vue3 + elment-plus
  • 了解 nextTick
  • C++精进之路(十六)string类和标准模板库
  • 【23.12.29期--Redis缓存篇】谈一谈Redis的集群模式
  • 【算法挨揍日记】day34——647. 回文子串、5. 最长回文子串
  • 欧科云链研究院:奔赴2024,Web3与AI共振引爆数字时代潘多拉魔盒
  • 【Py/Java/C++三种语言OD2023C卷真题】20天拿下华为OD笔试之【数学】2023C-素数之积【欧弟算法】全网注释最详细分类最全的华为OD真题题解
  • uniapp路由
  • 湖南大学-数据库系统-2023期末考试【原题】