首页 技术 正文
技术 2022年11月15日
0 收藏 937 点赞 2,520 浏览 3204 个字

平时在开发中避免不了使用大量的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可以看到运行结果。

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,484
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,899
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,732
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,485
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,125
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,286