建模的由来
就是将指定的xml字符串当作对象来操作
如果说当对一个指定的xml格式字符串完成了建模操作
只需要调用指定的方法就可以完成预定的字符串获取
建模的思路
1、分析需要被建模的文件中有那几个对象
2、每个对象拥有的行为以及属性
3、定义对象从小到大(从里到外)
4、通过23种的设计模式中的工厂模式,解析xml生产出指定对象
建模案例
就拿这一段代码来说,里面有三个对象
config,action,forward
在action中增加forward对象就是行为
action中的path,type就是属性
<?xml version="1.0" encoding="UTF-8"?>
<config>
<action path="/regAction" type="test.RegAction">
<forward name="failed" path="/reg.jsp" redirect="false" />
<forward name="success" path="/login.jsp" redirect="true" />
</action>
<action path="/loginAction" type="test.LoginAction">
<forward name="failed" path="/login.jsp" redirect="false" />
<forward name="success" path="/main.jsp" redirect="true" />
</action>
</config>
定义对象(从小到大)
forward
package com.zhangsiwen.model;
public class ForwardModel {
// <forward name="failed" path="/reg.jsp" redirect="false" />
private String name;
private String path;
private boolean redirect;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}
public boolean isRedirect() {
return redirect;
}
public void setRedirect(boolean redirect) {
this.redirect = redirect;
}
}
action
package com.zhangsiwen.model;
import java.util.HashMap;
import java.util.Map;
public class ActionModel {
// <action path="/regAction" type="test.RegAction">
private String path;
private String type;
private Map<String ,ForwardModel> foMap=new HashMap<String, ForwardModel>();
public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
//两个行为
/**
* 将指定的forwardModel压入当前actionModel对象中
* <forward />放入<action></action>
* @param forwardModel
*/
public void push(ForwardModel forwardModel) {
foMap.put(forwardModel.getName(), forwardModel);
}
public ForwardModel pop(String name) {
return foMap.get(name);
}
}
config
package com.zhangsiwen.model;
import java.util.HashMap;
import java.util.Map;
public class ConfigModel {
private Map<String, ActionModel> acMap=new HashMap<String, ActionModel>();
/**
* 将指定的actionModel压入当前ConfigModel对象中
* @param actionModel
*/
public void push(ActionModel actionModel) {
acMap.put(actionModel.getPath(), actionModel);
}
public ActionModel pop(String path) {
return acMap.get(path);
}
}
将xml文件的内容封装到model中
工厂模式
工厂模式解决的问题,将代码封装,提高代码的复用性
类比汉堡的获取方式(肯德基直接购买,原材料自己制作)
一般工厂类一定会有一个方法,就是生产指定模型对象的方法
拥抱变化
- 注意:
- 在工厂类中会有两个以上的构建方法,
- 一个是默认框架路径的模型对象构建方法
- 还有一个是动态读取任意位置下的构建配置文件
package com.zhangsiwen.model;
import java.io.InputStream;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
/**
* @author zhangsw
*/
public class ConfigModelFactory {
/**
* 通过资源文件构建对应的模型对象
* @param path 具体的资源文件路径
* @return
* @throws Exception
*/
public static ConfigModel build(String path) throws Exception {
// path=/config.xml
InputStream in = ConfigModelFactory.class.getResourceAsStream(path);
SAXReader reader=new SAXReader();
// /config.xml里的内容s
Document doc=reader.read(in);
// 接下来就做一件事情--》吧内容填充到configModel对象中(dco.asXML-->configModel)
ConfigModel configModel=new ConfigModel();
ActionModel actionModel=null;
ForwardModel forwardModel=null;
List<Element> actionEles = doc.selectNodes("/config/action");
for (Element actionEle : actionEles) {
actionModel =new ActionModel();
actionModel.setPath(actionEle.attributeValue("path"));
actionModel.setType(actionEle.attributeValue("type"));
// 拿到forward标签内容
List<Element> forwardEles = actionEle.selectNodes("forward");
for (Element forwardEle : forwardEles) {
forwardModel =new ForwardModel();
forwardModel.setName(forwardEle.attributeValue("name"));
forwardModel.setPath(forwardEle.attributeValue("path"));
// redirect属性是boolean类型的
// 需求,只有config.xml中redirect属性值填写了false,他才代表转发
// forwardEle.attributeValue("redirect")获取到config中的redirect属性值,那个字符串
forwardModel.setRedirect(!"false".equals(forwardEle.attributeValue("redirect")));
actionModel.push(forwardModel);
}
configModel.push(actionModel);
}
return configModel;
}
/**
* 默认框架路径的模型对象构建方法
* @return
* @throws Exception
*/
public static ConfigModel build() throws Exception {
return build("/config.xml");
}
public static void main(String[] args) throws Exception {
ConfigModel model = ConfigModelFactory.build();
ActionModel actionModel = model.pop("/loginAction");
System.out.println(actionModel.getType());
}
}
运行结果:
总结
通过xml建模提高代码的复用性,可以节省很多的代码。