我们什么时候应该使用 Observer 和 Observable? [英] When should we use Observer and Observable?

查看:26
本文介绍了我们什么时候应该使用 Observer 和 Observable?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

面试官问我:

什么是ObserverObservable,我们应该在什么时候使用它们?

我不知道这些术语,所以当我回到家开始谷歌搜索 ObserverObservable 时,我从不同的资源中发现了一些要点:<块引用>

1) Observable 是一个类和 Observer 是一个接口.

2) Observable 类维护了一个 Observer 的列表.

3) 当一个 Observable 对象被更新时,它会调用它的每个 Observerupdate() 方法来通知,改了.

我找到了这个例子:

import java.util.Observable;导入 java.util.Observer;类 MessageBoard 扩展了 Observable{public void changeMes​​sage(字符串消息){设置更改();通知观察者(消息);}}学生类实现观察者{@覆盖公共无效更新(Observable o,对象arg){System.out.println("留言板更改:" + arg);}}公共类 MessageBoardTest{public static void main(String[] args){MessageBoard board = new MessageBoard();学生鲍勃 = 新学生();学生乔 = 新学生();board.addObserver(bob);board.addObserver(乔);board.changeMes​​sage("更多作业!");}}

但我不明白为什么我们需要 ObserverObservable?setChanged()notifyObservers(message) 方法有什么用?

解决方案

您有一个 Student 和 MessageBoard 的具体示例.学生通过将自己添加到希望在新消息发布到 MessageBoard 时收到通知的观察者列表进行注册.当一个消息被添加到消息板时,它会遍历它的观察者列表并通知他们事件发生了.

想想推特.当您说要关注某人时,Twitter 会将您添加到他们的关注者列表中.当他们发送一条新推文时,您会在输入中看到它.在这种情况下,您的 Twitter 帐户是观察者,您关注的人是可观察者.

这个类比可能并不完美,因为 Twitter 更有可能是一个中介.但它说明了这一点.

An interviewer asked me:

What is Observer and Observable and when should we use them?

I wasn't aware of these terms, so when I got back home and started Googling about Observer and Observable, I found some points from different resources:

1) Observable is a class and Observer is an interface.

2) The Observable class maintains a list of Observers.

3) When an Observable object is updated, it invokes the update() method of each of its Observers to notify that, it is changed.

I found this example:

import java.util.Observable;
import java.util.Observer;

class MessageBoard extends Observable
{
    public void changeMessage(String message) 
    {
        setChanged();
        notifyObservers(message);
    }
}

class Student implements Observer 
{
    @Override
    public void update(Observable o, Object arg) 
    {
        System.out.println("Message board changed: " + arg);
    }
}

public class MessageBoardTest 
{
    public static void main(String[] args) 
    {
        MessageBoard board = new MessageBoard();
        Student bob = new Student();
        Student joe = new Student();
        board.addObserver(bob);
        board.addObserver(joe);
        board.changeMessage("More Homework!");
    }
}

But I don't understand why we need Observer and Observable? What are the setChanged() and notifyObservers(message) methods for?

解决方案

You have a concrete example of a Student and a MessageBoard. The Student registers by adding itself to the list of Observers that want to be notified when a new Message is posted to the MessageBoard. When a Message is added to the MessageBoard, it iterates over its list of Observers and notifies them that the event occurred.

Think Twitter. When you say you want to follow someone, Twitter adds you to their follower list. When they sent a new tweet in, you see it in your input. In that case, your Twitter account is the Observer and the person you're following is the Observable.

The analogy might not be perfect, because Twitter is more likely to be a Mediator. But it illustrates the point.

这篇关于我们什么时候应该使用 Observer 和 Observable?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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