实战重构工厂模式
程序员文章站
2022-05-27 22:37:02
工厂模式 实战 待优化业务逻辑代码 /** * @author sunyang * @date 2019/1/30 10:32 */ public class BizUser { public String getShowMoreNum(Long showNumRedis) { String res ......
工厂模式
实战
待优化业务逻辑代码
/** * @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); } }
题外话
实战中的代码是简单可公开的逻辑代码
举一反三,去改造看不下去的代码