Java如何利用策略模式替代if/else語(yǔ)句
平時(shí)在開(kāi)發(fā)中避免不了使用大量的if else語(yǔ)句,但過(guò)多層的if else對(duì)于性能有很大的開(kāi)銷,類似如下代碼
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!");
}
}
}
上邊代碼只是示例,實(shí)際情況可能不止4層
策略模式是一種解耦的方法,它對(duì)算法進(jìn)行封裝,使得算法的調(diào)用和算法本身分離。使用策略模式客戶端代碼不需要調(diào)整,算法之間可以互相替換,因?yàn)椴煌乃惴▽?shí)現(xiàn)的是同一個(gè)接口。將上面的代碼優(yōu)化后變?yōu)椋?/p>
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);
}
}
實(shí)現(xiàn)策略模式需要以下幾個(gè)步驟:
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.實(shí)現(xiàn)接口,重寫處理邏輯
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);
}
}
寫兩個(gè)作為例子,可根據(jù)情況自行擴(kuò)展實(shí)現(xiàn)類
3.定義策略上下文,根據(jù)msgid獲取對(duì)象實(shí)例
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();//調(diào)用無(wú)參構(gòu)造器創(chuàng)建實(shí)例
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
return inter;
}
}
在這一步驟中,我們需要一種方式可以根據(jù)msgid來(lái)反射獲取對(duì)象的實(shí)例,這里使用枚舉來(lái)維護(hù)二者的對(duì)應(yīng)關(guān)系。
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和對(duì)應(yīng)處理類的映射關(guān)系。至此策略模式優(yōu)化就已經(jīng)完成了,運(yùn)行MainStart可以看到運(yùn)行結(jié)果。
以上就是Java如何利用策略模式替代if/else語(yǔ)句的詳細(xì)內(nèi)容,更多關(guān)于Java 策略模式的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
MyBatis-Plus如何關(guān)閉SQL日志打印詳解
在使用mybatisplus進(jìn)行開(kāi)發(fā)時(shí),日志是一個(gè)非常有用的工具,它可以幫助我們更好地了解和調(diào)試我們的代碼,這篇文章主要給大家介紹了關(guān)于MyBatis-Plus如何關(guān)閉SQL日志打印的相關(guān)資料,需要的朋友可以參考下2024-03-03
Mybatis調(diào)用MySQL存儲(chǔ)過(guò)程的簡(jiǎn)單實(shí)現(xiàn)
本篇文章主要介紹了Mybatis調(diào)用MySQL存儲(chǔ)過(guò)程的簡(jiǎn)單實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-04-04
基于java socket實(shí)現(xiàn) 聊天小程序
這篇文章主要介紹了基于java socket實(shí)現(xiàn) 聊天小程序,代碼分為服務(wù)器和客戶端,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-12-12
Java開(kāi)發(fā)者必備10大數(shù)據(jù)工具和框架
這篇文章主要為大家詳細(xì)介紹了Java開(kāi)發(fā)者必備10大數(shù)據(jù)工具和框架,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06
IDEA中添加xml配置文件時(shí),顯示file問(wèn)題
這篇文章主要介紹了IDEA中添加xml配置文件時(shí),顯示file問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-12-12
關(guān)于HttpServletRequest獲取POST請(qǐng)求Body參數(shù)的3種方式
這篇文章主要介紹了關(guān)于HttpServletRequest獲取POST請(qǐng)求Body參數(shù)的3種方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-11-11
springboot驗(yàn)證碼的生成與驗(yàn)證的兩種方法
本文主要介紹了springboot驗(yàn)證碼的生成與驗(yàn)證的兩種方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-06-06

