The VisitorStrategy object is a simplification of a strategy, which allows manipulation of the serialization process. Typically implementing a Strategy is impractical as it requires the implementation to determine the type a node represents. Instead it is often easier to visit each node that is being serialized or deserialized and manipulate it so that the resulting XML can be customized.

To perform customization in this way a Visitor can be implemented. This can be passed to this strategy which will ensure the visitor is given each XML element as it is either being serialized or deserialized. Such an inversion of control allows the nodes to be manipulated with little effort. By default this used TreeStrategy object as a default strategy to delegate to. However, any strategy can be used.

상속: Strategy
예제 #1
0
 public void TestStrategy() {
    Visitor visitor = new ClassToNamespaceVisitor();
    Strategy strategy = new VisitorStrategy(visitor);
    Persister persister = new Persister(strategy);
    VisitorExample item = new VisitorExample();
    StringWriter writer = new StringWriter();
    item.Put("1", "ONE");
    item.Put("2", "TWO");
    item.Add("A");
    item.Add("B");
    persister.write(item, writer);
    String text = writer.toString();
    System.out.println(text);
    VisitorExample recover = persister.read(VisitorExample.class, text);
예제 #2
0
        public void TestStrategy()
        {
            Visitor        visitor   = new ClassToNamespaceVisitor();
            Strategy       strategy  = new VisitorStrategy(visitor);
            Persister      persister = new Persister(strategy);
            VisitorExample item      = new VisitorExample();
            StringWriter   writer    = new StringWriter();

            item.Put("1", "ONE");
            item.Put("2", "TWO");
            item.Add("A");
            item.Add("B");
            persister.write(item, writer);
            String text = writer.toString();

            System.out.println(text);
            VisitorExample recover = persister.read(VisitorExample.class, text);