jeudi 20 septembre 2018

MVC - Correct way to implement views reacting to something

Suppose I have a view which displays some kind of a puzzle.
I want to display some kind of animation whenever the puzzle is solved.

What is the correct way to receive handle the notification from the model?

I have thought of two ways:
1. Inside the controller react to the notification from the model by calling the animation in the view

inController_onSolvedNotificationFromModel(){
    view.fireworks();
}


2. Have an event handler for solved puzzle in the view: In controllers constructor:

view.setSolvedEventHandler(()-> {
    view.fireworks();
});

and then whenever the model notifies the controller the controller in turn will call a method in the view which in turn raises a solved event and calls the event handler:

view.setSolved(); // calls the event handler set by setSolvedEventHandler


3. Create an abstract method called onSolved inside the view so another developer can just inherit the view and change the behavior.

class View {
    public abstract void onSolved();
}

class FireworksView extends View {
    public void onSolved(){
        this.fireworks();
    }
}

Aucun commentaire:

Enregistrer un commentaire