MVP中的观察者模式 [英] Observer Pattern in MVP

查看:59
本文介绍了MVP中的观察者模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个系统(游戏),我尝试使用体系结构Model-View-Presenter来实现.我现在所做的是在演示者中的一个while循环,该循环不断调用用于显示的view方法.我这样做的方式是使用生产者/消费者模式,其中View寄存器和触摸事件的事件处理程序(Android)并产生相应的触摸实例,供演示者在while循环中使用.

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 view methods for displaying. The way I do this is using a Producer/Consumer pattern, where the View register and event handler for touch events(Android) and produce the corresponding touch imstances, that the presenter consumes in the while loop.

现在,我想在The Model和Presenter之间使用模式Observer/Suscriber.演示者将使用此呈现器使观察者订阅对模型状态的更改.问题是,演示者将根据视图中发生的事件执行模型中的更新.演示者每次在模型中执行一种方法时,它都会.可以更改其状态并通知演示者.然后,我将在另一个线程中为每次更新分离模型,但是如果它在while循环中的另一个线程中运行,我如何通知演示者?如果我调用该方法通知观察者,演示者何时会调用相应的方法?

Now I would like to use the pattern Observer/Suscriber between The Model and the Presenter. Using this the Presenter will be the Observer subscribe to changes on the state of the Model. The thing is, the presenter will execute the updates in the model, accordingly to the events occurred in the view. Every time that the presenter executes one method in.the model, It will be. possible to change its state and notify the presenter. I will separate then in another thread the model for every update, but how can I notify the presenter if this is running in a different thread inside a while loop? If I call the method notify observers, when the presenter will call the corresponding method?

这让我发疯了! :P我需要你的帮助队长!

Its driving me crazy! :P I need your help captains!

推荐答案

没关系.您有一个对象Presenter,该对象正在线程内的无限循环内使用.这并不意味着您也不能在线程调用它的同时调用它的方法.您唯一需要注意的是,如果线程读取/使用的数据与观察者通知所更改的内容相同,则应同步访问.

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.

因此,总而言之,当Presenter在无限循环内运行时,对其方法的任何调用都将立即得到答复(除非它们具有同步访问权限,在这种情况下,它将阻塞直到获得锁所有权为止)

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:

class Presenter implements IObserver
{
    public void aMethod() { }

    public void notifyObserver() { }
}

class Model
{
    private List<IObserver> observers = new ArrayList<>();

    public void addObserver(IObserver obs) { observers.add(obs); }

    public void notifyObservers()
    {
        for(IObserver o : observers) { o.notifyObserver(); }
    }
}


final Presenter myPresenter = new Presenter();
Model myModel = new Model();
myModel.add(myPresenter);

new Thread(new Runnable() 
{
    @Override
    public void run()
    {
        while(true)
        {
            myPresenter.aMethod();
        }           
    }
).start();

这篇关于MVP中的观察者模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆