一.XML建模
1.概念:将XML配置文件中的元素、属性、文本信息转化成对象的一个过程。
2.关系:从里到外
3.思路:
3.1 分析需要被建模的文件中有哪几个对象
3.2 每个对象拥有的行为以及属性
3.3 根据XML中元素节点 情况定义 ConfigModel , ActionModel , ForwardModel 对象模型。 (定义对象从里到外)
config节点下有多个子action节点,无节点属性
action节点下有多个子forward节点,有节点属性
forward下无子节点,有节点属性
3.4 通过32种的设计模式中的工厂模式,解析XML生产出指定对象(好处:提高代码复用性)
4.步骤:
4.1 以面向对象的编程思想,描述xml资源文件
4.2 将xml文件中内容封装进model实体对象
例:( action标签里的path和forward标签里的name相当于ID)
ForwardModel类
:
package com.lj.model;
import java.io.Serializable;
public class ForwardModel implements Serializable{
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;
}
@Override
public String toString() {
return "ForwardModel [name=" + name + ", path=" + path + ", redirect=" + redirect + "]";
}
public ForwardModel() {
super();
// TODO Auto-generated constructor stub
}
public ForwardModel(String name, String path, boolean redirect) {
super();
this.name = name;
this.path = path;
this.redirect = redirect;
}
}
ActionModel类
:
package com.lj.model;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
public class ActionModel implements Serializable{
private String type;
private String path;
private Map<String, ForwardModel> forwards=new HashMap<String, ForwardModel>();
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;