I had an idea and it goes like this:

  1. Parse a file on service side.
  2. Create a list of actions based on the file's contents.
  3. Pass the list of actions to the client side.
  4. Have the client define and perform actions based on the items on the list.

As in the visitor pattern, we'd have a class for the actions and all of them inherit the Action interface. The clients would then implement the visitors. In Java it'd be something like this:

public interface Action {
    void act(Visitor visitor);
}

public class PerfectAction implements Action {
    void act(Visitor visitor) {
         visitor.bePerfect();
    }
}

public class VisibleAction implements Action {
    void act(Visitor visitor) {
         visitor.beVisible();
    }
}

public interface Visitor {
    void bePerfect();
    void beVisible();
}

The Problem
I can't create Proxy classes for the Action and Visitor interfaces. They do not contain setters and/or getters. Plus they do not contain any data. Is it possible to pass this knowledge of which method should be called on the Visitor object from service to client side?

有帮助吗?

解决方案

Request Factory can only move data around (EntityProxy and/or ValueProxy), and ask the server to do things on behalf of the client (RequestContext).

To transfer actions, the client and server first need to share the knowledge of those actions that can be performed.

You then have two solutions:

  • move to GWT-RPC
  • because the client has to know every possible action upfront anyway, create an enum or whatever to identify each action, and transfer those identifiers to the client, which will map them back to concrete actions to perform.

其他提示

I don't think this is how you'd implement the visitor pattern. I'd do something like this

public interface ActionVisitor {
  void visit(VisibleAction va);
  void visit(PerfrectAction pa);
}

public class PerfectAction implements Action {
    void act(Visitor visitor) {
       visitor.visit(this);
    }
}

public class VisibleAction implements Action {
    void act(Visitor visitor) {
        visitor.visit(this);
    }
}

Then I'd define an implementation of the visitor that performed the appropriate actions.

It's important to define it in this way so that the logic of what the visitor does is external to the class. Prior to this, each implementation had a different implementation of the visitor, so it'd be harder to change behaviour.

I think that this will solve your problem, because now the logic of what to do is externalized to the visitor.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top