- 构建树型结构的实体
树型结构的实体主要是用组合模式构建的,且是透明模式的组合模式;关于组合模式,此处不再赘述。
代码部分:
> package com.dp.composite.example4;
import com.google.gson.annotations.Expose;
import java.io.Serializable;
import java.util.ArrayList;
/**
* @desc 抽象的缺省类
* @creator caozhiqing
* @data 2015/8/12
*/
public abstract class Component implements Serializable{
private transient Component parent;
public Component getParent() {
return parent;
}
public void setParent(Component parent) {
this.parent = parent;
}
public void addChild(Component component){
throw new UnsupportedOperationException("不支持此方法");
}
public void getChild(int index) {
throw new UnsupportedOperationException("不支持此方法");
}
public ArrayList<Component> getChild() {
throw new UnsupportedOperationException("不支持此方法");
}
public void removeChile(Component component) {
throw new UnsupportedOperationException("不支持此方法");
}
public abstract void operation(String pre);
}
package com.dp.composite.example4;
import java.util.ArrayList;
import java.util.Iterator;
/**
* @desc 具体树枝类,即容器类
* @creator caozhiqing
* @data 2015/8/12
*/
public class Composite extends Component {
ArrayList<Component> children ;
private String name;
public Composite(String name){
this.name= name;
}
@Override
public void addChild(Component component) {
if(component == null){
return;
}
if(children == null){
children = new ArrayList<Component>();
}
children.add(component);
component.setParent(this);
}
@Override
public void removeChile(Component component) {
if(component == null || children == null){
return;
}
Iterator<Component> iterator = children.iterator();
while (iterator.hasNext()){
Component component1 = iterator.next();
if(component1 != null && component1 == component){
iterator.remove();
if(component instanceof Composite && component1.getChild() != null){
for(Component component2:component.getChild()){
component2.setParent(this);
addChild(component2);
}
}
break;
}
}
}
@Override
public ArrayList<Component> getChild() {
return children;
}
@Override
public void operation(String pre) {
System.out.println(pre+"#"+this.name);
pre +=" ";
if(children != null && children.size()>0){
for(Component component:children){
component.operation(pre);
}
}
}
}
package com.dp.composite.example4;
/**
* @desc 叶子类,即没有子节点
* @creator caozhiqing
* @data 2015/8/12
*/
public class Leaf extends Component {
private String name;
public Leaf(String name){
this.name = name;
}
@Override
public void operation(String pre) {
System.out.println(pre+"-"+this.name);
}
}
package com.dp.composite.example4;
import com.google.gson.Gson;
/**
* @desc 客户端
* @creator caozhiqing
* @data 2015/8/12
*/
public class Client {
public static void main(String[] arg){
Component root = new Composite(“服装”);
Component man = new Composite(“男装”);
Component wom = new Composite(“女装”);
root.addChild(man);
root.addChild(wom);
Component leaf1 = new Leaf("夹克");
Component leaf2 = new Leaf("寸衫");
man.addChild(leaf1);
man.addChild(leaf2);
Component leaf3 = new Leaf("短裙");
Component leaf4 = new Leaf("Browse");
wom.addChild(leaf3);
wom.addChild(leaf4);
root.operation("");
System.out.println("----------->");
// root.removeChile(man);
// root.operation(“”);
Gson gson = new Gson();
System.out.println(gson.toJson(root));
}
}
“`
解析
此处最主要的是忽略每个子节点的父节点。
即在Component 中把此属性设置为transient 即不被序列化,且不能使用Gson提供的@Expose(serialize = false) 或@Expose(与new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create() )配合使用。JSON 结果。
{“children”:[{“children”:[{“name”:”夹克”},{“name”:”寸衫”}],”name”:”男装”},{“children”:[{“name”:”短裙”},{“name”:”Browse”}],”name”:”女装”}],”name”:”服装”}
- 总结:
如果古加入transient关键字,会报栈溢出的bug
这回crm系统封装解析json和以树型为结构的json解析很有帮助。