`
dr2tr
  • 浏览: 142237 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

Design Patterns -- Obeserver

阅读更多

1. The intent of the Observer pattern is to define a one-to-many dependency such that when one object changes state, all its dependents are notified and updated automatically. see pic:

The Subject here is also called Observable. in SmallTalk, we also call Observer and Subject as View and Model.

The progress of Observer is like below:

" the observer registers with the subject, expressing its interest in observing. When a state change occurs, the subject notifies the observer of the change. When the observer no longer wishes to observe the subject, the observer unregisters from the subject. These steps are known as observer registration, notification, and unregistration, respectively." (from Exploring the Observer Design Pattern  Doug Purdy, Microsoft Corporation
Jeffrey Richter, Wintellect)

Register, "The observer invokes the Register method on the subject, passing itself as an argument. Once the subject receives this reference, it must store it in order to notify the observer when a state change occurs sometime in the future. Rather than storing the observer reference in an instance variable directly, most observer implementations delegate this responsibility to a separate object, typically a container. " see pic:

Notification: "When a state change occurs (AskPriceChanged), the subject retrieves all the observers within the container by invoking the GetObservers method. The subject then enumerates through the retrieved observers, calling the Notify method, which notifies the observer of the state change." see pic:

Unregistration, "This sequence is performed when the observer no longer needs to observe the subject. The observer calls the UnRegister method, passing itself as an argument. The subject then invokes the Remove method on the container, ending the period of observation", see pic:

"The use of a container provides the flexibility to support multiple observers per subject. This permits a subject to notify a potentially infinite number of observers of state changes, rather than just one."

For easy understanding, we can simplely combine the diagram above like below, (not whole)

 

2. Observer pattern supports MVC. MVC supports n-tier computing, which brings many practical advatages to software development and deployment. When the pattern uses MVC, 2 requirements must be met  for design:

2.1 Implementations of Observer(who observes...) must register their interest and must update themselves appropriately, often including repainting themselves(in GUI).

2.2 Subclasses of Observable(who is observed) must remember to notify observers when their value changed.

3. When applying MVC, the flow of events may seem indirect. Component(View, Observable) changes caused an update to the bean(Model, Observer). In turn, a change in the bean notifies the Conmponet(View) and they repaint. So changes from GUI layer to the business layer and back to the GUI layer.

4. One pattern of Observer is make the class we want to observe a subclass of Observable. But sometimes the class is already a subclass of another one. In this case, we can provide our class with an Observable object and have our class forward key method calls to it. The Component class in java.awt uses this approach, although it uses a PropertyChangeSupport object instead of an Observable object. In short, this approach implments Observer through delegation instead of subclassing.

Later today I will write an Observer example here to test the pattern.

分享到:
评论

相关推荐

    java-design-patterns-master_JAVAdesignpattens_java_design_

    这个压缩包“java-design-patterns-master”显然是一个专注于Java设计模式的学习资源,旨在帮助开发者深入理解和应用这些模式。下面我们将详细探讨Java设计模式及其在实际开发中的应用。 1. **单例模式(Singleton...

    Head-First-Design-Patterns-master.zip

    《Head First设计模式》是一本深受开发者喜爱的设计模式入门书籍,其官方源码库"Head-First-Design-Patterns-master.zip"包含了书中所讲解的各种设计模式的实际代码示例,旨在帮助读者更深入地理解并应用这些模式。...

    Pro-Objective-C-Design-Patterns-for-iOS

    《Pro-Objective-C-Design-Patterns-for-iOS》是一本专注于在iOS平台上利用Objective-C语言实现设计模式的专业书籍。书中旨在帮助已经有一定Cocoa开发基础的开发者,通过掌握设计模式的实践应用,提升软件开发的生产...

    GOF-设计模式-Design Patterns-英文原版-高清-有目录-有页码

    **设计模式**(Design Patterns)是软件工程中的一个重要概念,它是一套被反复使用、多数人知晓的、经过分类编目的、代码设计经验的总结。本章节将从多个角度对设计模式进行深入探讨。 #### 二、设计模式的基本定义 ...

    Node.js Design Patterns Second Edition[July 2016]

    Chapter 2, Node.js Essential Patterns, introduces the first steps towards asynchronous coding and design patterns with Node.js discussing and comparing callbacks and the event emitter (observer ...

    design-patterns-c-sharp

    "design-patterns-c-sharp"这个主题涵盖了C#编程环境中如何应用这些模式的知识。 首先,我们需要理解设计模式的基本概念。设计模式并不具体到任何特定的代码或库,而是一种描述在特定情况下如何设计软件组件的通用...

    java-Design-Patterns-chinese.rar_java design patterns

    这个“java-Design-Patterns-chinese.rar”压缩包文件包含了对Java设计模式的详细中文解释,非常适合Java开发者学习和参考。 在Java设计模式中,主要分为三大类:创建型、结构型和行为型模式。 1. **创建型模式**...

    Design-Patterns-In-Kotlin,在kotlin中实现的设计模式.zip

    《Kotlin设计模式实战解析》 在编程领域,设计模式是一种通用、可重用的...开源项目"Design-Patterns-In-Kotlin"则是一个很好的学习资源,它提供了各种设计模式在Kotlin中的具体实现,有助于开发者深入学习和实践。

    DesignPatterns-master.zip

    - 观察者模式(Observer):定义对象间的一种一对多的依赖关系,当一个对象的状态发生改变时,所有依赖于它的对象都会得到通知并被自动更新。 - 中介者模式(Mediator):用一个中介对象来封装一系列的对象交互,...

    Java-EE-8-Design-Patterns-and-Best-Practices-源码.rar

    本篇文章将围绕"Java-EE-8-Design-Patterns-and-Best-Practices-源码.rar"中的内容,深入探讨Java EE 8中的一些关键设计模式和最佳实践。 1. **设计模式**: - **工厂模式**:用于创建对象的类,提供了一种封装...

    design-patterns-cpp-master.zip

    在"design-patterns-cpp-master.zip"这个压缩包中,我们可以期待找到C++实现的各种设计模式的代码示例。设计模式通常分为三大类:创建型模式、结构型模式和行为型模式。 1. 创建型模式: - 单例模式(Singleton)...

    design patterns - elements of reusable

    行为型模式涉及对象之间的交互和责任分配,如观察者模式(Observer),它定义了一种一对多的依赖关系,当一个对象的状态改变时,所有依赖于它的对象都会得到通知并自动更新;还有策略模式(Strategy),它定义了一...

    yifanzheng-java-design-patterns-master_java_

    本资源"yifanzheng-java-design-patterns-master"是一个专注于Java设计模式的入门学习项目,作者承诺将持续更新,旨在帮助初学者和开发者更好地理解和应用这些模式。 设计模式通常分为三大类:创建型模式、结构型...

    java-design-patterns-PPT.zip_观察者模式ppt

    在这一模式中,一个主题(Subject)维护着一个观察者(Observer)列表,并通知他们有关状态变化的消息。这种模式允许对象之间的松耦合,使得当一个对象的状态发生改变时,所有依赖于它的对象都会得到通知并自动更新...

    Head-First-Design-Patterns-master设计模式官方源码

    14. 观察者模式(Observer Pattern):观察者模式定义了对象之间的一对多依赖关系,当一个对象的状态发生改变时,所有依赖于它的对象都会得到通知并被自动更新。 15. 状态模式(State Pattern):状态模式允许对象...

    Design-patterns-_The-class-diagram.rar_The Class_ea

    在Java中,可以利用`java.util.Observable`和`java.util.Observer`接口实现这一模式。例如,当我们需要实时显示天气数据时,天气服务作为被观察者,而显示界面作为观察者,当天气变化时,服务会通知界面更新显示。 ...

    design-patterns-spring-boot:Spring引导中的设计模式

    git clone https://github.com/indrekru/design-patterns-spring-boot.git 您需要在您的环境中安装Maven: Mac(自制): brew install maven Ubuntu: sudo apt-get install maven 正在安装 在环境中安装了Maven...

    DesignPatterns.pdf_objects_designpatterns_

    标题中的"DesignPatterns.pdf_objects_designpatterns_"暗示了这可能是一个关于面向对象设计模式的PDF文档,而描述中的"Design patterns elements of reusable objects"进一步确认了这一点。设计模式的核心理念在于...

Global site tag (gtag.js) - Google Analytics