如何使用Java Observer的update(Observable,Object)函数? [英] How to Use Java Observer's update(Observable, Object) Function?

查看:313
本文介绍了如何使用Java Observer的update(Observable,Object)函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用Java创建的基本MVC模式,该模式使用Observable/Observer类/接口.

I have a basic MVC pattern created in Java that uses the Observable/Observer class/interface.

Observable    Observer      Observable/Observer
Model         Controller    View

View triggers an event to the Controller, when the user interacts with the GUI.
- E.g presses a button, fills in a field, etc.

Model triggers an event to the View when it updates its state.
- E.g when the a button was pressed and the Controller requests new results.

我的问题是关于观察者功能

My question is about the Observer function

update(Observable obs, Object arg);

这是一个功能,但是例如,我要在View中进行多种更新.如何优雅地区分搜索结果的更新或其他信息的显示?这是两个完全不同的更新,它们使用来自模型的不同对象.

This is one function, but I have many different kinds of updating to do in my View for example. How do I elegantly distinguish between an update to, say, my search results or the displaying of additional information? These are two completely different updates that use different objects from the Model.

我的第一个想法是使用Object传递一个字符串,该字符串将描述需要进行哪些更新.

My first idea was to use the Object to pass a string which would describe what update is required.

"UpdateResults" "DisplayAdditionalInformation" "AddQuestions"

但是这似乎容易出错并且很丑陋.我的第二个本能是创建一个将作为对象传递的EventObject,但随后我必须不断询问我正在使用哪种EventObject:

but that seems error-prone and ugly. My second instinct was to create an EventObject that would be passed as an Object, but then I have to keep asking what kind of EventObject I'm using:

if (arg instanceof ResultEventObject) 
    // Get results from model
else if (arg instanceof InformationEventObject)
    // Get information from model
else if (arg instanceof QuestionsEventObject)
    // get questions from model

我的第三个想法是简单地更新所有内容,但这似乎毫无意义.

My third idea is to simply update everything, but that seems pointlessly inefficient.

我可能无法正确理解Observable/Observer接口,或者我没有按照其作者的意图使用update().因此,我的问题是,当我要处理许多不同类型的更新或事件时,如何正确使用update函数?

I am probably not understanding the Observable/Observer interface correctly or I'm not using update() as it was intended by it's authors. Therefore my question, how do I use the updatefunction properly when I have many different types of updates or events to process?

推荐答案

您可以根据要收听的视图/模型创建自己的Listener接口.这样一来,您的视图/模型就可以将所需的信息准确传递给控制器​​,并使对控制器进行单元测试变得更加容易.

You can create your own Listener interfaces depending on what view/model you are listening to. This allows your view/model to pass exactly the information required to your controller and will make it easier to unit test the controller.

对于侦听模型,更新所有内容是最简单的解决方案,除非性能证明是个问题,否则您可以这样做.

For listening to the model, updating everything is the simplest solution and you can do that unless performance proves to be an issue.

这篇关于如何使用Java Observer的update(Observable,Object)函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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