工厂模式
实战
待优化业务逻辑代码
/** * @author sunyang * @date 2019/1/30 10:32 */public class BizUser { public String getShowMoreNum(Long showNumRedis) { String result= String.valueOf(showNumRedis); if (showNumRedis >= 10 && showNumRedis <= 99){ String code = String.valueOf(showNumRedis); String first = code.substring(0, 1); result = first + "0+"; }else if (showNumRedis >= 100 && showNumRedis <=999){ String code = String.valueOf(showNumRedis); String first = code.substring(0, 1); result = first + "00+"; }else if (showNumRedis >= 1000 && showNumRedis <=9999){ String code = String.valueOf(showNumRedis); String first = code.substring(0, 1); result = first + "000+"; }else if (showNumRedis > 9999){ result = "9999+"; }else{ result= String.valueOf(showNumRedis); } return result; }}
分析
从代码看的出来,不同条件下,执行的逻辑是不同的,可以把这种执行逻辑抽象出来,用多态的概念来定义不同的执行方式。
/** * 工厂模式 * 从代码看的出来,不同的条件下,执行的逻辑是不同的,那么可以把这种执行逻辑抽象出来 * 用多态的概念来定义不同的执行方式 * * @author sunyang * @date 2019/1/30 10:51 */public abstract class AbstractBizUser2 { /** * 处理展示 * @param showNumRedis 展示数量 * @return */ public abstract String process(Long showNumRedis);}
/** * @author sunyang * @date 2019/1/30 10:57 */public class ShowOneExecutor extends AbstractBizUser2{ @Override public String process(Long showNumRedis) { String result; String code = String.valueOf(showNumRedis); String first = code.substring(0, 1); result = first + "0+"; return result; }}
/** * @author sunyang * @date 2019/1/30 11:07 */public class ShowTwoExecutor extends AbstractBizUser2 { @Override public String process(Long showNumRedis) { String result; String code = String.valueOf(showNumRedis); String first = code.substring(0, 1); result = first + "00+"; return result; }}
/** * @author sunyang * @date 2019/1/30 11:09 */public class ShowThreeExecutor extends AbstractBizUser2 { @Override public String process(Long showNumRedis) { String result; String code = String.valueOf(showNumRedis); String first = code.substring(0, 1); result = first + "000+"; return result; }}
/** * @author sunyang * @date 2019/1/30 11:09 */public class ShowFourExecutor extends AbstractBizUser2 { @Override public String process(Long showNumRedis) { String result; result = "9999+"; return result; }}
/** * @author sunyang * @date 2019/1/30 11:10 */public class ShowOtherExecutor extends AbstractBizUser2 { @Override public String process(Long showNumRedis) { String result; result= String.valueOf(showNumRedis); return result; }}
/** * 定义一个工厂来把需要处理逻辑给包起来 * * @author sunyang * @date 2019/1/30 11:21 */public class ExecutorFactory { public AbstractBizUser2 getExecutor(Long showNumRedis){ if (showNumRedis >= 10 && showNumRedis <= 99){ return new ShowOneExecutor(); }else if (showNumRedis >= 100 && showNumRedis <=999){ return new ShowTwoExecutor(); }else if (showNumRedis >= 1000 && showNumRedis <=9999){ return new ShowThreeExecutor(); }else if (showNumRedis > 9999){ return new ShowFourExecutor(); }else{ return new ShowOtherExecutor(); } }}
结论
/** * @author sunyang * @date 2019/1/30 10:32 */public class BizUser { public String getShowMoreNum(Long showNumRedis) { /*String result= String.valueOf(showNumRedis); if (showNumRedis >= 10 && showNumRedis <= 99){ String code = String.valueOf(showNumRedis); String first = code.substring(0, 1); result = first + "0+"; }else if (showNumRedis >= 100 && showNumRedis <=999){ String code = String.valueOf(showNumRedis); String first = code.substring(0, 1); result = first + "00+"; }else if (showNumRedis >= 1000 && showNumRedis <=9999){ String code = String.valueOf(showNumRedis); String first = code.substring(0, 1); result = first + "000+"; }else if (showNumRedis > 9999){ result = "9999+"; }else{ result= String.valueOf(showNumRedis); } return result;*/ ExecutorFactory executorFactory = new ExecutorFactory(); AbstractBizUser2 bizUser2 = executorFactory.getExecutor(showNumRedis); return bizUser2.process(showNumRedis); }}
/** * @author sunyang * @date 2019/1/30 12:32 */public class BizUserDemo { public static void main(String[] args) { long showNum = 1111; BizUser bizUser = new BizUser(); String result = bizUser.getShowMoreNum(showNum); System.out.println("工厂模式实战后的结果 result:" + result); }}
题外话
实战中的代码是简单可公开的逻辑代码
举一反三,去改造看不下去的代码