Skip to content Skip to sidebar Skip to footer

Observer Pattern In Mvp

I have a System (game) which I try to implement using the architecture Model-View-Presenter. What I have done right now is a while loop in the presenter that calls continuously the

Solution 1:

It Doesn't matter. You have an object, Presenter, which is being used inside an infinite loop inside a thread. That does not mean you cannot call it's methods while its being called by the thread as well. The only consideration you have to take care of is that, if the data being read/used by the thread is the same altered by the observer notification, you shoud synchronize it access.

So, to summarize, while the Presenter is running inside the infinite loop, any call to it's method will be answered instantly (unless they have synchronized access, in which case it will block until it gets the lock ownership)

The following is completly fine:

classPresenterimplements IObserver
{
    publicvoidaMethod() { }

    publicvoidnotifyObserver() { }
}

classModel
{
    privateList<IObserver> observers = newArrayList<>();

    publicvoidaddObserver(IObserver obs) { observers.add(obs); }

    publicvoidnotifyObservers()
    {
        for(IObserver o : observers) { o.notifyObserver(); }
    }
}


final Presenter myPresenter = newPresenter();
Model myModel = newModel();
myModel.add(myPresenter);

newThread(newRunnable() 
{
    @Overridepublicvoidrun()
    {
        while(true)
        {
            myPresenter.aMethod();
        }           
    }
).start();

Solution 2:

It's possible you are over engineering this in terms of architecture. MVP and Producer/Consumer and Observable all in the same post. Alarm bells are ringing. I suspect that you don't need the producer/consumer or the observer pattern, MVP is probably totally adequate.

Try this.

Create 3 files:

  • GameFragment.java
  • GameView.java
  • GamePresenter.java

GamePresenter:

publicclassGamePresenter {
    private final GameView view;

    publicGamePresenter(GameView view){
        this.view = view;
        NetworkController.addObserver(this);//listen for events coming from the other player for example. 
    }

    publicvoidonSwipeRight(){
        // blah blah do some logic etc etc
        view.moveRight(100);
        NetworkController.userMovedRight();
    }

    publicvoidonNetworkEvent(UserLeftGameEvent event){
        // blah blah do some logic etc etc
        view.stopGame()
    }
}

GameView

is an interface

publicinterfaceGameView {
    voidstopGame();
    voidmoveRight(int pixels);
}

GameFragment is a class that extends Fragment and implements GameView AND has a GamePresenter as a member.

publicclassGameFragmentextendsFragmentimplementsGameView {
    private GamePresenter presenter;

    @OverridepublicvoidonCreate(Bundle savedInstanceState){
        presenter = newGamePresenter(this);
    }
}

The key to this approach is to clearly understand the role of each file.

The Fragment is in control of anything view related (Buttons, TextView etc). It informs the presenter of user interactions.

The Presenter is the engine, it takes the information from the View (in this case it is the Fragment, but notice that this pattern lends itself well to Dependency injection? That's no coincidence. The Presenter doesn't know that the View is a Fragment - it doesn't care) and combines it with the information it is receiving from 'below' (comms, database etc) and then commands the View accordingly.

The View is simply an interface through which the Presenter communicates with the View. Notice that the methods read as commands, not as questions (eg getViewState()) and not to inform (eg onPlayerPositionUpdated()) - commands (eg movePlayerHere(int position)).

Post a Comment for "Observer Pattern In Mvp"