GWT serialization and decorator pattern

I use a decorator template to describe actions, and I would like to use these actions in RPC calls

public abstract class Action implement Serializable
{
  boolean       isDecorated = false;
  public Action() {} // default constructor for Serialization
}

public abstract class ActionDecorator extends Action
{
  private   Action  _decoratedAction;

  public ActionDecorator()  // default constructor for Serialization
  {}

  public ActionDecorator(Action action)
  {
    _decoratedAction = action;
    _decoratedAction.isDecorated = true;
  }
}

After the transaction, I get a DecoratorAction that contains the Action, but the isDecorated member _decoratedAction is false.

Since the default constructor (zero-arg) is called to reconstruct my object, both my decorator and my framed actions get the default value isDecorated (false).

I can not copy "_decoratedAction.isDecorated = true;" in the zero-arg constructor of ActionDecorator, because _decoratedAction is not initialized (null) at this time.

, , ( ) , Action...

+3
4

, , .

Action ActionDecorator , public_decoratedAction , .

:

class MyAction extends Action {
    public MyAction() {}
}

class MyActionDecorator extends ActionDecorator {
    public MyActionDecorator() {}
    public MyActionDecorator(Action a) {
        super(a);
    }
}

, . :

public Action getAction() {
    return new MyActionDecorator(new MyAction());
}

:

System.out.println(action.isDecorated); // false
System.out.println(((ActionDecorator) action)._decoratedAction.isDecorated); // true

, : isDecorated of ActionDecorator false isDecorated Action, , . , . , .

+1

, isDecorated false Action ActionDecorator, true. isDecorated? Action , Action ?

0

Action RPC? - ? , .

0

. . (getter/setter) isDecorated Action, ActionDecorator. ActionDecorator _decoratedAction.

0

Source: https://habr.com/ru/post/1755482/


All Articles