设计模式-策略模式
Posted meijsuger
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了设计模式-策略模式相关的知识,希望对你有一定的参考价值。
设计模式-策略模式
使用场景:
应用于某些需要使用不同方式实现不同情况下的计算策略
举例情况:
去某地时有许多出行方式,每个方式的出行成本都不一样,需要计算每个出行方式的成本:
定义算法接口:
package com.zang.schema.strategy.total; /** * 出行方式接口 * 根据出行方式选择计算方法 * * @author Zhang Qiang * @Date 2019/8/30 16:46 */ public interface Travel /** * 计算方法 * * @param destination * @return */ String travelWay(String destination);
具体的算法策略(多个):
package com.zang.schema.strategy.total; /** * @author Zhang Qiang * @Date 2019/8/30 16:57 */ public class TravelFerryBoatTotal implements Travel @Override public String travelWay(String destination) // 这里写具体算法,传参根据需求传 String t = " TravelFerryBoatTotal -> " + destination + ", total: 530"; System.out.println(t); return t;
package com.zang.schema.strategy.total; /** * @author Zhang Qiang * @Date 2019/8/30 16:56 */ public class TravelTrainTotal implements Travel @Override public String travelWay(String destination) String t = " TravelTrainTotal -> " + destination + ", total: 679"; System.out.println(t); return t;
其它算法策略省略。。
具体算法工厂,通过工厂使用对应的算法实例去应对不同情况:
package com.zang.schema.strategy; import com.zang.schema.strategy.total.*; import org.apache.commons.configuration.XMLConfiguration; import org.apache.commons.configuration.ConfigurationException; /** * 算法工厂 * * @author Zhang Qiang * @Date 2019/8/30 16:43 */ public class StrategyFactory /** * switch创建 * * @param strategyEnum * @return */ public static Travel createTravel(StrategyEnum strategyEnum) Travel travel = null; switch (strategyEnum.value) case "car": travel = new TravelCarTotal(); break; case "train": travel = new TravelTrainTotal(); break; case "ferryboat": travel = new TravelFerryBoatTotal(); break; case "plane": travel = new TravelPlaneTotal(); break; case "selfDriver": travel = new TravelSelfDriverTotal(); break; default: System.out.println("未找到算法"); return travel; return travel; /** * 反射创建 * * @param strategyClassName * @return */ public static Travel createTravelByNewInstend(StrategyClassEnum strategyClassName) Travel travel = null; try travel = (Travel) Class.forName(strategyClassName.value).newInstance(); catch (Exception e) e.printStackTrace(); return travel; /** * xml创建 * * @return */ public static Travel createTravelByXml() Travel travel = null; String travelName = ""; try XMLConfiguration xmlConfiguration = new XMLConfiguration("travel.xml"); travelName = xmlConfiguration.getString("travel.name"); catch (ConfigurationException e) e.printStackTrace(); System.out.println("xml文件读取错误"); if ("car".equals(travelName)) travel = new TravelCarTotal(); else if ("train".equals(travelName)) travel = new TravelTrainTotal(); else if ("ferryboat".equals(travelName)) travel = new TravelFerryBoatTotal(); else if ("plane".equals(travelName)) travel = new TravelPlaneTotal(); else if ("selfDriver".equals(travelName)) travel = new TravelSelfDriverTotal(); return travel;
附带不同策略使用枚举分配:
package com.zang.schema.strategy; /** * @author Zhang Qiang * @Date 2019/8/30 16:32 */ public enum StrategyEnum /** * */ BY_TRAIN("train"), BY_CAR("car"), BY_FERRYBOAT("ferryboat"), BY_PLANE("plane"), BY_SELFDRIVER("selfDriver"); String value = ""; private StrategyEnum(String value) this.value = value; public String getValue() return value;
反射方法枚举,需要反射类的类路径:
package com.zang.schema.strategy; /** * @author Zhang Qiang * @Date 2019/8/30 17:20 */ public enum StrategyClassEnum /** * */ BY_TRAIN("com.zang.schema.strategy.total.TravelTrainTotal"), BY_CAR("com.zang.schema.strategy.total.TravelCarTotal"), BY_FERRYBOAT("com.zang.schema.strategy.total.TravelFerryBoatTotal"), BY_PLANE("com.zang.schema.strategy.total.TravelPlaneTotal"), BY_SELFDRIVER("com.zang.schema.strategy.total.TravelSelfDriverTotal"); String value = ""; private StrategyClassEnum(String value) this.value = value; public String getValue() return value;
具体使用:
package com.zang.schema.strategy; import com.zang.schema.strategy.total.Travel; /** * 执行 * 策略模式 - 封装策略,通过工厂选择对应策略实现 * * @author Zhang Qiang * @Date 2019/8/30 17:06 */ public class Action public static void main(String[] args) new Action().travel(); public void travel() StrategyEnum strategyEnum = StrategyEnum.BY_PLANE; Travel travel = StrategyFactory.createTravel(strategyEnum); travel.travelWay("吞州"); Travel travelsXml = StrategyFactory.createTravelByXml(); travelsXml.travelWay("亳州"); StrategyClassEnum strategyClassEnum = StrategyClassEnum.BY_SELFDRIVER; Travel travelNewInstend = StrategyFactory.createTravelByNewInstend(strategyClassEnum); travelNewInstend.travelWay("魔都");
print:
TravelPlaneTotal -> 吞州, total: 790 TravelTrainTotal -> 亳州, total: 679 TravelSelfDriverTotal -> 魔都, total: 320 Process finished with exit code 0
以上是关于设计模式-策略模式的主要内容,如果未能解决你的问题,请参考以下文章