I decided to write this week’s blog on Observer Design Patterns because I am going to be writing a tutorial on this in class next week. I chose a tutorial posted by author Santosh Kumar on the blog “WalkingTechie”. I chose this blog about the Observer Design Pattern because Kumar gives an in-depth explanation of what an Observer Design Pattern is, how it’s implemented and gives UML and code examples of it which I found to be extremely helpful.
Kumar starts by giving a definition for the Observer Pattern, which he states “defines a one-to-many dependency between objects so that when one object changes state, all of its dependents are notified and updated automatically.” He also states that this is a part of the Behavioral Design Pattern.
Kumar furthers explains the Observer Design Pattern by giving a real-world example with a newspaper subscription. There are two variables – Subject and Observer. In this example, the newspaper publisher is the subject and goes into business by publishing newspapers. The observer is the subscriber who subscribes to a particular publisher, and everytime a new edition comes out it gets delivered from them. A user can unsubscribe which will stop papers from coming.
Kumar then implements the Observer Design Pattern. He defines a Subject interface that has a method to register, remove/unregister user and notify users when the object changes state. There is also a DisplayElement interface that has a display method, and an Observer interface and all concrete classes – BinaryObserver, OctalObserver, and HexObserver.
After constructing a UML diagram, Kumar goes through the code implementation step by step.
In the first step, the Subject interface is created. This contains the registerObserver and removeObserver and notifyObserver methods.
In the second step, the Observer interface is created. This includes the update method,
In the third step, the DisplayElement interface is created, which has the display method.
In the fourth step, a concrete class DecimalData is created and implements the Subject interface. Then a concrete class is created that implements the Observer and DisplayElement interface. And finally, the ObservePatternDemo class is created to use DecimalData and concrete objects to test observer design patterns.
I enjoyed Kumar’s blog explaining the Observer Design Pattern because he went in depth by defining the Observer Design Pattern, giving a real-world example, and implemented it by creating a java project. This really helped me understand the Observer Design Pattern more and it will come in handy when I do my tutorial on it.