项目作者: zakayothuku

项目描述 :
Design Patterns in Object Oriented Programming - Observer Pattern
高级语言: Java
项目地址: git://github.com/zakayothuku/design-patterns---observer-pattern.git
创建时间: 2019-07-08T12:47:48Z
项目社区:https://github.com/zakayothuku/design-patterns---observer-pattern

开源协议:Apache License 2.0

下载


Design Patterns in Object Oriented Programming

Observer Pattern

From Wikipedia:
The observer pattern is a software design pattern in which an object, called the subject, maintains a list of its dependents, called observers, and notifies them automatically of any state changes, usually by calling one of their methods.

It is mainly used to implement distributed event handling systems, in “event driven” software.

What problems can the Observer design pattern solve?

The Observer pattern addresses the following problems:

  • A one-to-many dependency between objects should be defined without making the objects tightly coupled.
  • It should be ensured that when one object changes state an open-ended number of dependent objects are updated automatically.
  • It should be possible that one object can notify an open-ended number of other objects.

Defining a one-to-many dependency between objects by defining one object (subject) that updates the state of dependent objects directly is inflexible because it couples the subject to particular dependent objects. Tightly coupled objects are hard to implement, change, test, and reuse because they refer to and know about (how to update) many different objects with different interfaces.

What solution does the Observer design pattern describe?

  • Define Subject and Observer objects.
  • so that when a subject changes state, all registered observers are notified and updated automatically.

Branch without-observer-pattern

Shows the code without the implementation of the Observer Pattern.

Branch with-observer-pattern

Shows the code with the implementation of the Observer Pattern.