java设计模式使用

编程入门 行业动态 更新时间:2024-10-27 04:34:47

java设计<a href=https://www.elefans.com/category/jswz/34/1771241.html style=模式使用"/>

java设计模式使用

文章目录

      • 策略模式 + 工厂方法
      • 责任链模式

策略模式 + 工厂方法

策略模式 + 工厂方法是解决 if…else 用得非常多的方案

  • 把条件模块抽象为一个公共的接口,策略接口
public interface ActionService {void doAction();
}
  • 根据每个逻辑,定义出自己具体的策略实现类,如下:
public class ActionService1 implements ActionService{public void doAction() {//do something}
}public class ActionService2 implements ActionService{public void doAction() {//do something}
}// 省略其他策略
  • 工厂类,统一调度,用来管理这些策略,如下:
public class ActionServiceFactory {private ActionServiceFactory(){}private static class SingletonHolder{private static ActionServiceFactory instance=new ActionServiceFactory();}public static ActionServiceFactory getInstance(){return SingletonHolder.instance;}private static final Map<String,ActionService> ACTION_SERVICE_MAP = new HashMap<String, ActionService>();static {ACTION_SERVICE_MAP.put("action1",new ActionService1());ACTION_SERVICE_MAP.put("action2",new ActionService2());ACTION_SERVICE_MAP.put("action3",new ActionService3());ACTION_SERVICE_MAP.put("action4",new ActionService4());ACTION_SERVICE_MAP.put("action5",new ActionService5());}public static ActionService getActionService(String actionCode) {ActionService actionService = ACTION_SERVICE_MAP.get(actionCode);if (actionService == null) {throw new RuntimeException("非法 actionCode");}return actionService;}public void doAction(String actionCode) {getActionService(actionCode).doAction();}
}

单例模式实现工厂类。

  • 使用
ActionServiceFactory.getInstance().doAction("action1");

这种优化方式也是很优雅的,特别适合分支较多,逻辑较为复杂的代码块,这种方式将分支逻辑与业务代码解耦了,是一种很不错的方案。

责任链模式

责任链我们可以看做是一个单链表的数据结构,一个对象一个对象地过滤条件,符合的就执行,然后结束,不符合的就传递到下一个节点,如果每个对象都无法处理,一般都有一个最终的节点来统一处理。

  • 定义责任链处理请求节点
public abstract class ActionHandler {// 后继节点protected ActionHandler successor;/*** 处理请求* @param actionCode*/public void handler(String actionCode) {doHandler(actionCode);}// 设置后继节点protected ActionHandler setSuccessor(ActionHandler successor) {this.successor = successor;return this;}// 处理请求public abstract void doHandler(String actionCode);
}
  • 定义首尾节点,用于一些异常情况的处理
// 首节点,判断 actionCode 是否为空
public class HeadHandler extends ActionHandler{@Overridepublic void doHandler(String actionCode) {if (StringUtils.isBlank(actionCode)) {throw new RuntimeException("actionCode 不能为空");}successor.doHandler(actionCode);}
}// 尾节点,直接抛出异常,因为到了尾节点说明当前 code 没有处理
public class TailHandler extends ActionHandler{@Overridepublic void doHandler(String actionCode) {throw new RuntimeException("当前 code[" + actionCode + "] 没有具体的 Handler 处理");}
}
  • 定义各个节点具体的实现节点
public class ActionHandler1 extends ActionHandler{@Overridepublic void doHandler(String actionCode) {if ("action1".equals(actionCode)) {doAction1();} else {// 传递到下一个节点successor.doHandler(actionCode);}}
}public class ActionHandler2 extends ActionHandler{@Overridepublic void doHandler(String actionCode) {if ("action2".equals(actionCode)) {doAction2();} else {// 传递到下一个节点successor.doHandler(actionCode);}}
}// 省略其他节点
  • 定义工厂,来构建一条完整的责任链,并负责调度
public class ActionHandlerFactory {private ActionHandler headHandler;private ActionHandlerFactory(){headHandler = new HeadHandler();ActionHandler actionHandler1 = new ActionHandler1();ActionHandler actionHandler2 = new ActionHandler2();ActionHandler actionHandler3 = new ActionHandler3();ActionHandler actionHandler4 = new ActionHandler4();ActionHandler actionHandler5 = new ActionHandler5();ActionHandler tailHandler = new TailHandler();// 构建一条完整的责任链headHandler.setSuccessor(actionHandler1).setSuccessor(actionHandler2).setSuccessor(actionHandler3).setSuccessor(actionHandler4).setSuccessor(actionHandler5).setSuccessor(tailHandler);}private static class SingletonHolder{private static ActionHandlerFactory instance=new ActionHandlerFactory();}public static ActionHandlerFactory getInstance(){return SingletonHolder.instance;}public void doAction(String actionCode) {headHandler.doHandler(actionCode);}
}
  • 使用
ActionHandlerFactory.getInstance().doAction("action1");

更多推荐

java设计模式使用

本文发布于:2023-11-17 13:59:26,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1643736.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:模式   java

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!