Loose coupling and the Observer pattern

Loose coupling is an important design principle that should be used in software applications. The main purpose of loose coupling is to strive for loosely-coupled designs between objects that interact with each other. Coupling refers to the degree of knowledge that one object has about the other object that it interacts with.

Loosely-coupled designs allow us to build flexible object-oriented systems that can handle changes because they reduce the dependency between multiple objects.

The loose coupling architecture ensures following features:

  • It reduces the risk that a change made within one element might create an unanticipated impact on the other elements
  • It simplifies testing, maintenance, and troubleshooting problems
  • The system can be easily broken down into definable elements

The Observer pattern provides you with an object design where the Subject and Observer are loosely coupled. The following points will explain this better:

  • The only thing that the Subject knows about an Observer is that it implements a certain interface. It need not know the ConcreteObserver class.
  • Any new Observer can be added at any point in time (as we saw in the sample example earlier in this chapter).
  • The Subject need not be modified at all to add any new Observer. In the example, we saw that AnyOtherObserver can be added/removed without any changes in the Subject.
  • Subjects or Observers are not tied up and can be used independently of each other. So the Observer can be reused anywhere else, if needed.
  • Changes in the Subject or Observer will not affect each other. As both are independent or loosely coupled, they are free to make their own changes.
..................Content has been hidden....................

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