策略模式可以使算法的变化独立于使用它们的客户端(这里的客户端代指使用算法的代码)。我们知道,工厂模式是解耦对象的创建和使用,观察者模式是解耦观察者和被观察者。策略模式跟两者类似,也能起到解耦的作用,不过,它解耦的是策略的定义、创建、使用这三部分。
在实际的项目开发中,这个模式也比较常用。最常见的应用场景是,利用它来避免冗长的 if-else 或 switch 分支判断。不过,它的作用还不止如此。它也可以像模板模式那样,提供框架的扩展点等等。本篇我们讲解策略模式的原理和实现,以及如何用它来避免分支判断逻辑。
一、问题说明
平时在开发中避免不了使用大量的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 {
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;
MsgEnum(String msgid, String clazz) {
this.msgid = msgid;
this.clazz = 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;
}
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可以看到运行结果。
评论区