Best OO Method for Handling the Cancel Button

I always wondered what the best way to handle the cancel button is in a more OO way. In a hurry, I always render an ugly check on the properties of the Boolean form if the button has been canceled.

The fact is, this makes the code dirty, having a lot of “canceled checks” between the logic.

I always get something like this:

void doLogic()
{
     checkIfIsCancelled();
     callOtherFunction();

     checkIfIsCancelled();
     callAnotherFunction();

     checkIfIsCancelled();
     callAnotherFunction();

     checkIfIsCancelled();
     callAnotherFunction();
}

I hope I was clear enough. I just want a tidier way to do this :)

+3
source share
4 answers

The right way to handle this is with a strategy template where you have a default strategy, where you perform normal processing, and you have a Canceled strategy.

Strategy, , . .

, , , .

( ) , , .

+1

, GUI-, . , , Linux Mac. , , .

. (, , ..) , " ", , , .

java, :

Button b = JButton("Push") ;
listener = new ActionListener()_ {
    public void actionPerformed(ActionEvent e) {
        System.out.println("I was pushed!") ;
    }
} ;
b.addActionListener(listener)

" !" . , , , , ..

0

.

0

All Articles