《设计模式》之策略模式

策略模式定义

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

策略模式结构

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

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

优点

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 {
    @Override
    public void login(BaseLoginContext context) {
    System.out.println("简单登录,当前登录的人:" + context.getUserName() + ",密码:" + context.getPassword());
    }
    }

  2. 实现类2

    public class HardLoginServiceImpl implements BaseLoginService {
    @Override
    public 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;
    }

    @Override
    public 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;
      }
    
      @Override
      public 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();
      }
  • 结果

相关推荐
abluckyboy几秒前
Java 实现求 n 的 n^n 次方的最后一位数字
java·python·算法
2301_818732062 分钟前
前端调用控制层接口,进不去,报错415,类型不匹配
java·spring boot·spring·tomcat·intellij-idea
2501_9419820515 分钟前
深度对比:Java、Go、Python 实现企微外部群推送,哪个效率更高?
java·golang·企业微信
马猴烧酒.40 分钟前
【面试八股|JAVA多线程】JAVA多线程常考面试题详解
java·服务器·数据库
sino爱学习1 小时前
高性能线程池实践:Dubbo EagerThreadPool 设计与应用
java·后端
风生u2 小时前
activiti7 详解
java
岁岁种桃花儿2 小时前
SpringCloud从入门到上天:Nacos做微服务注册中心(二)
java·spring cloud·微服务
Word码2 小时前
[C++语法] 继承 (用法详解)
java·jvm·c++
TT哇2 小时前
【实习 】银行经理端两个核心功能的开发与修复(银行经理绑定逻辑修复和线下领取扫码功能开发)
java·vue.js
逝水如流年轻往返染尘2 小时前
Java中的数组
java