Search code examples
javagwtvisitor-patternproxy-pattern

Visitor design pattern with GWT


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?


Solution

  • 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.