av一区二区在线观看_亚洲男人的天堂网站_日韩亚洲视频_在线成人免费_欧美日韩精品免费观看视频_久草视

您的位置:首頁技術文章
文章詳情頁

Java如何利用策略模式替代if/else語句

瀏覽:13日期:2022-08-23 16:23:01

平時在開發中避免不了使用大量的if else語句,但過多層的if else對于性能有很大的開銷,類似如下代碼

public class MainStart { public static void main(String[] args) { String msgid = 'MS066'; if(message.equals('MS066')){ System.out.println('MS066'); }else if (message.equals('MS034')){ System.out.println('MS034'); }else if (message.equals('MS064')){ System.out.println('MS064'); }else{ System.out.println('no msgid!'); } }}

上邊代碼只是示例,實際情況可能不止4層

策略模式是一種解耦的方法,它對算法進行封裝,使得算法的調用和算法本身分離。使用策略模式客戶端代碼不需要調整,算法之間可以互相替換,因為不同的算法實現的是同一個接口。將上面的代碼優化后變為:

public class MainStart { public static void main(String[] args) { OrderDictController controller=new OrderDictController(); String msgid = 'MS066'; MsgInterface msgInterface=MsgContext.getInstance(msgId); msgInterface.manage(msg,controller); }}

實現策略模式需要以下幾個步驟:

1.定義接口

import java.sql.SQLException;import org.dom4j.DocumentException;import com.huc.controller.OrderDictController;public interface MsgInterface { public void manage(String msg, OrderDictController controller) throws DocumentException, SQLException;}

2.實現接口,重寫處理邏輯

package com.huc.msg.imp;import java.sql.SQLException;import org.dom4j.DocumentException;import com.huc.controller.OrderDictController;import com.huc.msg.MsgInterface;public class MS003 implements MsgInterface{ @Override public void manage(String msg, OrderDictController controller) throws DocumentException, SQLException { controller.manageMs003(msg); }}

package com.huc.msg.imp;import java.sql.SQLException;import org.dom4j.DocumentException;import com.huc.controller.OrderDictController;import com.huc.msg.MsgInterface;public class MS028 implements MsgInterface{ @Override public void manage(String msg, OrderDictController controller) throws DocumentException, SQLException { controller.manageMs028(msg); }}

寫兩個作為例子,可根據情況自行擴展實現類

3.定義策略上下文,根據msgid獲取對象實例

package com.huc.msg;import java.util.Map;public class MsgContext { public static MsgInterface getInstance(String msgId){ MsgInterface inter=null; Map<String, String> allClazz = MsgEnum.getAllClazz(); String clazz = allClazz.get(msgId); if (msgId!=null&&msgId.trim().length()>0) { try {try { inter = (MsgInterface) Class.forName(clazz).newInstance();//調用無參構造器創建實例} catch (InstantiationException e) { e.printStackTrace();} catch (IllegalAccessException e) { e.printStackTrace();} } catch (ClassNotFoundException e) {e.printStackTrace(); } } return inter; }}

在這一步驟中,我們需要一種方式可以根據msgid來反射獲取對象的實例,這里使用枚舉來維護二者的對應關系。

package com.huc.msg;import java.util.HashMap;import java.util.Map;public enum MsgEnum { MS066('MS066', 'com.huc.msg.imp.MS066'), MS034('MS034', 'com.huc.msg.imp.MS034'), MS064('MS064', 'com.huc.msg.imp.MS064'), MS028('MS028', 'com.huc.msg.imp.MS028'), MS003('MS003', 'com.huc.msg.imp.MS003'), MS062('MS062', 'com.huc.msg.imp.MS062'), MS154('MS154', 'com.huc.msg.imp.MS154'), MS153('MS153', 'com.huc.msg.imp.MS153'), MS033('MS033', 'com.huc.msg.imp.MS033'); private String msgid; private String clazz; public static Map<String, String> getAllClazz() { Map<String, String> map = new HashMap<String, String>(); for (MsgEnum msgEnum : MsgEnum.values()) { map.put(msgEnum.getMsgid(), msgEnum.getClazz()); } return map; } MsgEnum(String msgid, String clazz) { this.msgid = msgid; this.clazz = clazz; } public String getMsgid() { return msgid; } public void setMsgid(String msgid) { this.msgid = msgid; } public String getClazz() { return clazz; } public void setClazz(String clazz) { this.clazz = clazz; }}

在上面的代碼中,getAllClazz()方法用于獲取所有message和對應處理類的映射關系。至此策略模式優化就已經完成了,運行MainStart可以看到運行結果。

以上就是Java如何利用策略模式替代if/else語句的詳細內容,更多關于Java 策略模式的資料請關注好吧啦網其它相關文章!

標簽: Java
相關文章:
主站蜘蛛池模板: 美女视频h| 成人特级毛片 | 国产精品久久影院 | 国产美女精品视频免费观看 | 九色在线观看 | 国产精品资源在线 | 久久99精品久久久久久 | 鸳鸯谱在线观看高清 | 成人永久免费视频 | 四虎影院美女 | 亚洲欧美日韩中文字幕一区二区三区 | 综合久久av | 亚洲成人免费视频在线 | 成人一区av | 99精品在线观看 | 在线看av的网址 | 欧美精品一 | 日韩国产免费观看 | 色婷婷亚洲国产女人的天堂 | 国产不卡在线观看 | 伦理午夜电影免费观看 | 久久人人爽人人爽 | 国产婷婷 | 午夜久久久 | 国产午夜精品一区二区三区 | 免费黄色片在线观看 | 国产精品一区二区三区在线 | 免费天天干| 国产乱码精品1区2区3区 | 久久久.com | 先锋影音资源网站 | 美女一级a毛片免费观看97 | 日韩精品免费播放 | 国产精久久久 | 中文日本在线 | 久久国产欧美一区二区三区精品 | 欧美福利一区 | 久久久亚洲一区 | 美女黄网站 | 日产久久| 精品国产乱码久久久久久久久 |