Java设计模式 Design Pattern:观察者模式 Observer Pattern

本文介绍了观察者模式的核心概念,即定义对象间的一对多依赖关系,确保当一个对象状态变化时,所有依赖对象都能自动更新。文章详细阐述了观察者模式的结构、交互方式及应用场景,并强调了该模式在降低对象间耦合度和支持广播通信方面的优势。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

意图 

  • Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically.
  • 定义对象间的一种一对多的关系,从而使得当对象发生变化时,所有依赖他的对象都得到通知并且自动更新

结构

 

  • Subject : knows its observers. Any number of Observer objects may observe a subject. provides an interface for attaching and detaching Observer objects.
  • Subject : 知道他的观察者.可能会有任意多个Observer对象来观察一个subject.他提供了一个接口来添加和去除观察者对象.
  • Observer : defines an updating interface for objects that should be notified of changes in a subject.
  • Observer : 为当subject发生变化需要得到通知的对象定义了一个更新的接口
  • ConcreteSubject : stores state of interest to ConcreteObserver objects.
  • ConcreteSubject : 存储了感兴趣的ConcreteObserver对象的状态.
  • ConcreteObserver : maintains a reference to a ConcreteSubject object. stores state that should stay consistent with the subject's. implements the Observer updating interface to keep its state consistent with the subject's.
  • ConcreteObserver :持有一个ConcreteSubject对象的引用.存储了subject需要持续保存的状态.实现了Observer定义的更新接口来保持它的状态和subject一致.

交互

 

1.      ConcreteSubject notifies its observers whenever a change occurs that could make its observers' state inconsistent with its own.

2.      After being informed of a change in the concrete subject, a ConcreteObserver object may query the subject for information.

3.      ConcreteObserver uses this information to reconcile its state with that of the subject.

  • ConcreteSubject发生变化时,通知他所有的观察者,者可能使得他的观察者的状态于其不一致.
  • 在通告了具体订阅者中的变更后, ConcreteObserver对象有可能会查询订阅者的信息.
  • ConcreteObserver使用这个信息来调整它的状态使其和订阅者一致.

应用场景

  • When an abstraction has two aspects, one dependent on the other. Encapsulating these aspects in separate objects lets you vary and reuse them independently.
  • 当一个抽象有两个方面,一个依赖于另一个.将这些方面封装在不同的对象中,使得你可以独立的改变和重复使用他们.
  • When a change to one object requires changing others, and you don't know how many objects need to be changed.
  • 当一个对象修改时需要修改其他的对象,并且你不知道有多少对象需要被改变.
  • When an object should be able to notify other objects without making assumptions about who these objects are. In other words, you don't want these objects tightly coupled.
  • 当一个对象应当在不假定有哪些对象时,需要具备通知其它对象的能力时.换句话说,你不想要这些对象是紧耦合的.

影响效果

  • Abstract coupling between Subject and Observer. All a subject knows is that it has a list of observers, each conforming to the simple interface of the abstract Observer class. The subject doesn't know the concrete class of any observer. Thus the coupling between subjects and observers is abstract and minimal. Because Subject and Observer aren't tightly coupled, they can belong to different layers of abstraction in a system. A lower-level subject can communicate and inform a higher-level observer, thereby keeping the system's layering intact. If Subject and Observer are lumped together, then the resulting object must either span two layers (and violate the layering), or it must be forced to live in one layer or the other (which might compromise the layering abstraction).
  • 抽象发布者和观察者之间的耦合.所有的发布者知道的是有一个观察者列表,每一个都符合抽象的Observer类提供的简单接口.这样,发布者和观察者之间的耦合是抽象的而且是最小的.因为发布者和观察者之间不是紧耦合的,他们在系统中可以属于不同的抽象层.一个底层的发布者可以传达并通知一个上层的观察者,从而保持系统层次的完整性.如果发布者和订阅者混杂在一起,则结果对象必须跨越两个层次(并且干扰分层),或者必须强制存活在同一个层或者其它(可能干扰层之间的抽象)
  • Support for broadcast communication. Unlike an ordinary request, the notification that a subject sends needn't specify its receiver. The notification is broadcast automatically to all interested objects that subscribed to it. The subject doesn't care how many interested objects exist; its only responsibility is to notify its observers. This gives you the freedom to add and remove observers at any time. It's up to the observer to handle or ignore a notification.
  • 支持广播通信.与传统的请求不同,发布者发出的消息并不需要指定它的接收者.同时会自动的广播给所有对他感兴趣的订阅他的对象.发布者不关心有多少感兴趣的对象存在;他唯一的职责就是通知他的观察者.这给了你在任意时刻添加或删除观察者的自由.是否处理或忽略消息由观察者决定.

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值