Abstract method versus listener

I am making a button class that handles input and drawing on its own; the only thing that needs to be determined is the location and what happens when the button is pressed.

In this situation, would it be better to have an interface ButtonPressListenerand have it as a parameter in the constructor Button, or if to Buttonbe abstract with an abstract method pressed()?

The resulting initiation code for Buttonwill look like this:

new Button(x,y,new ButtonPressListener(){
    @Override
    protected void pressed(){
        // code
    }
});

or

new Button(x,y){
    @Override
    protected void pressed(){
        // code
    }
};

Also, in other similar situations, what should be considered when choosing between two approaches?

Thanks.

+4
source share
2 answers

I prefer the listener.

Resons:

  • A listener will give you more flexibility when using java8 lambdas.
  • ,
  • , -

: , . - . , , , java.

+3

, , . Wenn , .

Button.pressed(), , . , , - .

, , API.

+1

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


All Articles