Iterator design pattern in the Spring Framework

The Spring Framework also extends the Iterator pattern through the CompositeIterator class. Mainly this pattern used in the Collection Framework of Java for iterating the elements in sequence.

Now that we've seen Iterator design pattern, let's turn to a different variant of it--Observer design pattern.

Observer Design Pattern

Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically
-GOF Design Pattern

Observer pattern is one of very common design pattern, This pattern is a part of the Behavioral design pattern family of GOF pattern that addresses responsibilities of objects in an application and how they communicate between them at runtime. According to this pattern, sometimes objects make a one-to-many relationship between the objects in your application, such that if one object is modified, it's notified to other dependent objects automatically.

For example, Facebook post comments are one of the examples of the observer design pattern. If you comment on a post of your friend then you are always notified by this post whenever anyone else comments on the same post again.

The Observer pattern provides communication between decoupled objects. It makes a relationship between objects mostly a one-to-many relationship. In this pattern, there is an object which is known as the subject. Whenever there is any change in the state of this subject, it will be notified to its list of dependents accordingly. This list of dependents is known as observers. The following figure illustrates the Observer pattern:

Use case of the Observer design pattern

There are following lists of the benefits of using the Observer pattern:

  • This pattern provides decoupled relationship between the subject and observer
  • It provides support for broadcasting

Let's see the following UML diagram is showing all components of Observer design pattern:

UML Diagram for Observer Design Pattern
  • Subject: It is an interface. It has information about its observers.
  • ConcreteSubject: It is a concrete implementation of Subject, it has information about all its observers to be notified when its state changes.
  • Observer: It is an interface to be notified of changes in a subject.
  • ConcreteObserver: It is a concrete implementation of Observer, it keeps its state consistent with the subject's state.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset