首页 / 设计模式 / 行为型设计模式:策略模式
行为型设计模式:策略模式
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了行为型设计模式:策略模式,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4100字,纯文字阅读大概需要6分钟。
内容图文
策略模式是让策略的实现与策略的使用解耦,在我们日常的开发中,经常用来重构和优化if-else和switch语句。当然,策略模式并不是为了取代if-else和switch语句而生的,这里好多人有一个误区,它的思想是接口的一组实现可以相互替换,让使用和实现解耦。
策略的使用分为2种,一种是有状态的,每个if-else分支的策略使用都需要new一个新的策略对象;一种是无状态的,每个if-else分支的策略使用都用一个公共的策略对象。
策略类的一个demo如下:
public interface Strategy { void doStrategy(); } public class StrategyA implements Strategy { private Logger logger = LoggerFactory.getLogger(getClass()); public void doStrategy() { logger.info("doStrategy StrategyA"); } } public class StrategyB implements Strategy{ private Logger logger = LoggerFactory.getLogger(getClass()); public void doStrategy() { logger.info("doStrategy StrategyB"); } }
有状态的策略模式使用:
public class StrategyStatefulFactory { public static Strategy getStatefulStrategy(String type){ if (StringUtils.isEmpty(type)){ throw new IllegalArgumentException("type must be not null"); } if(StringUtils.pathEquals(type, "strategyA")){ return new StrategyA(); }else if(StringUtils.pathEquals(type, "strategyB")){ return new StrategyB(); }else{ return null; } } }
无状态的策略模式使用
public class StrategyNonStatefulFactory { private static Map<String, Strategy> strategyMap = new HashMap<>(3); static { strategyMap.put("strategyA", new StrategyA()); strategyMap.put("strategyB", new StrategyB()); } public static Strategy getStrategy(String type){ if (StringUtils.isEmpty(type)){ throw new IllegalArgumentException("type must be not null"); } return strategyMap.get(type); } }
上面的策略工厂,当我们新加入一个策略类时,都需要修改工厂类,这有点不符合开闭原则,那怎么来处理呢?一种方式是在配置文件中增加配置,工厂初始化的时候读配置文件来初始化工厂中map,第二种方式是通过注解来标注策略类,使用java反射机制来初始化工厂中map。这里我给出一个第二种方式实现的demo
@Retention(RetentionPolicy.RUNTIME) @Target(ElementType.TYPE) public @interface Strategic { String value(); } public interface AnnotationStrategy { void doStrategy(); } @Strategic(value = "strategyA") public class AnnotationStrategyA implements AnnotationStrategy{ private Logger logger = LoggerFactory.getLogger(getClass()); public void doStrategy(){ logger.info("doStrategy AnnotationStrategyA"); } } @Strategic(value = "strategyB") public class AnnotationStrategyB implements AnnotationStrategy{ private Logger logger = LoggerFactory.getLogger(getClass()); public void doStrategy(){ logger.info("doStrategy AnnotationStrategyB"); } }
工厂类代码如下:
public class AnnotationStrategyFactory { private static Logger logger = LoggerFactory.getLogger(AnnotationStrategyFactory.class); static Map<String, AnnotationStrategy> map = new HashMap<>(3); static { File classPath = new File("./target/classes/design/behavioral/strategy"); String clsspath = "design.behavioral.strategy"; if (classPath.exists()) { for (File file : classPath.listFiles()){ String fileName = file.getName(); if (fileName.endsWith(".class")) { fileName = fileName.replace(".class", ""); try { Class<?> classZ = Class.forName(clsspath + "." + fileName); if (classZ.isAnnotationPresent(Strategic.class)){ map.put(classZ.getAnnotation(Strategic.class).value(), (AnnotationStrategy)classZ.newInstance()); } } catch (Exception e) { logger.error("exception:", e); } } } } } public static AnnotationStrategy getAnnotationStrategy(String type) { return map.get(type); } }
源码地址:
https://github.com/jinjunzhu/design-pattern.git
欢迎关注个人微信公众号,共同学习成长
内容总结
以上是互联网集市为您收集整理的行为型设计模式:策略模式全部内容,希望文章能够帮你解决行为型设计模式:策略模式所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。