adSense 900*70


Visitor 패턴을 활용한 Node Map Converter JAVA - Pattern

Visitor 패턴을 설명해놓은 자료는 많이 있으니 알아서 찾아보시길.
개인적으로 맘에 드는 Visitor 패턴 설명 블로그: http://iilii.egloos.com/4906850


public interface MapVisitor {

public void visit(MapAcceptor mapAcceptor,Map<String,Object> nodeMap);

}


public interface MapAcceptor {

public void accept(MapVisitor mapVisitor,Map<String,Object> nodeMap);

}


public abstract class NodeComponent implements MapAcceptor{

private final String nodeName;

private final String nodeValue;

private List<NodeComponent> children = new ArrayList<NodeComponent>();

private Map<String,Object> nodeMap = new HashMap<String,Object>();


public NodeComponent(String nodeName, String nodeValue){

this.nodeName  = nodeName;

this.nodeValue = nodeValue;

}


public String getNodeName() {

return nodeName;

}


public String getNodeValue() {

return nodeValue;

}


public List<NodeComponent> getChildren(){

return this.children;

}



public Map<String, Object> getNodeMap(){

return this.nodeMap;

}


public abstract void addChildren(NodeComponent childNodeComponent);


@Override

public void accept(MapVisitor visitor, Map<String,Object> nodeMap) {

nodeMap.putAll(this.nodeMap);

visitor.visit(this,this.nodeMap);

}


@Override

public String toString() {

return "NodeComponent [nodeName=" + nodeName + ", nodeValue="

+ nodeValue + "]";

}

}



public class NodeComposite extends NodeComponent {

public NodeComposite(String nodeName, String nodeValue) {

super(nodeName, nodeValue);

super.getNodeMap().put(nodeName, new HashMap<String,Object>());

}


@Override

public void addChildren(NodeComponent childNodeComponent) {

super.getChildren().add(childNodeComponent);

}


}


public class LeafNode extends NodeComponent{

public LeafNode(String nodeName, String nodeValue) {

super(nodeName, nodeValue);

}


@Override

public void addChildren(NodeComponent childNodeComponent) {

throw new UnsupportedOperationException();

}


@Override

public void accept(MapVisitor visitor, Map<String, Object> nodeMap) {

nodeMap.put(getNodeName(), getNodeValue());

}


}


public class NodeMapConverter implements MapVisitor{


@SuppressWarnings("unchecked")

@Override

public void visit(MapAcceptor mapAcceptor, Map<String, Object> nodeMap) {

if(mapAcceptor instanceof NodeComponent){

NodeComponent nodeComponent = (NodeComponent)mapAcceptor;

List<NodeComponent> children = nodeComponent.getChildren();


for(NodeComponent childNode:children){

Map<String,Object> subMap = (Map<String,Object>)nodeMap.get(nodeComponent.getNodeName());

childNode.accept(this, subMap);

}

}

}

}



테스트용  Main class

public class NodeMapConverterMain {


public static void main(String[] args) {

NodeComponent body = new NodeComposite("body", "");

NodeComponent header = new NodeComposite("header", "");

NodeComponent channel = new LeafNode("channel", "1");

NodeComponent type = new LeafNode("type", "1");

header.addChildren(channel);

header.addChildren(type);


NodeComponent payload = new NodeComposite("payload", "");

NodeComponent userId = new LeafNode("userId", "userId");

NodeComponent accessToken = new LeafNode("accessToken", "accessToken");

payload.addChildren(userId);

payload.addChildren(accessToken);


NodeComponent other = new NodeComposite("other", "");

NodeComponent subOther = new LeafNode("subOther", "subOther");

NodeComponent subOther2 = new LeafNode("subOther2", "subOther2");

other.addChildren(subOther);

other.addChildren(subOther2);


body.addChildren(header);

body.addChildren(payload);

body.addChildren(other);


NodeMapConverter nodeMapConverter = new NodeMapConverter();

nodeMapConverter.visit(body, body.getNodeMap());

System.out.println(body.getNodeMap());

}


}


결과:

{body={other={subOther2=subOther2, subOther=subOther}, payload={accessToken=accessToken, userId=userId}, header={type=1, channel=1}}}


xml등의 계층적 정보를 포함하고 있는 파일등을 파싱하여 Component, Composite, Leaf들을 사용하여 Composite패턴을 이용해서

구조화 하고 난후 해당 정보를 NodeMapConverter에 최상위 Component를 던져주면 하위의 구조를 그대로 Map으로 변경해주게 된다.  



덧글

댓글 입력 영역


side adsense

adSense 900*70