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:
The Observer pattern provides you with an object design where the Subject
and Observer
are loosely coupled. The following points will explain this better:
Subject
knows about an Observer
is that it implements a certain interface. It need not know the ConcreteObserver
class.Observer
can be added at any point in time (as we saw in the sample example earlier in this chapter).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
.Observers
are not tied up and can be used independently of each other. So the Observer
can be reused anywhere else, if needed.Subject
or Observer
will not affect each other. As both are independent or loosely coupled, they are free to make their own changes.18.223.33.157