Protected field versus protected method and implementation interface for action handlers

I have a class that represents a base page with the back, go forward, and canel buttons in Wicket. But not all pages have all buttons, for example. d. The first page does not have a "return", obviously.

My idea is to define a common ActionHandler

public interface ActionHandler {
  void perform();
}

and subclasses return the actions they support:

public Derived extends BasicPage {
  protected ActionHandler getForwardHandler() {
    return new ActionHandler() {
      void perform() {
        doIt();
      }
    };
  }
}

The question arises: why not use protected fields?

public Derived extends BasicPage {
  protected forwardHandler = new ActionHandler() {
      void perform() {
        doIt();
      }
    };
}

Another option is to not use inheritance (which makes no sense here) and set the ActionHandlers from the outside:

Toolbar toolbar = new Toolbar();
toolbar.setForwardHandler(new ActionHandler() {
  void perform() {
    doIt();
  }
});

, , ForwardHandler, CancelHandler, , , ActionHandlers . , ? .

, , , , ...

+3
1

? ; ActionHandler.

, . , . null, , , , , undefined.

, , . , .

, , , . FooPage Foo, Foo , FooPage .

, , , - , , .

+2

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


All Articles