Wed. Oct 16th, 2019

Samir Daoudi's Technical Blog

SharePoint, Office 365, C#, .Net, SQL Server News, Tips, Ebooks, Events, Webinars ….etc

Observer Design Pattern

Author : DAOUDI Samir | Context : MSc Software Engineering – Internet Programming

The web development as a trend has seen major improvements since the 90s, new technologies and tools are continually being designed and developed to provide enhanced solutions with a great user interactivity and the powerful processes. Web site has changed from the static ones that were mainly used to display static information about products and companies and which were very hardly maintainable and upgradable. Web site administrators had a great workload when dealing with web site update. An important enhancement in the web development field is the appearance of approaches and methods for development, known more as design patterns.
What are the Design Patterns?
A pattern is the description of a recurrent problem in our environment, and proposes the core the solution to that problem (Gamma et.al, 1994). Christopher Alexander provided this definition for buildings and towns, but the same definition is true for object-oriented programming.
Elements of Design Pattern
A pattern has four main elements
– The pattern name: it describes a design problem, its solutions and consequences.
– The problem: Describes when we should apply the pattern, it summarizes the problem and its context.
– The solution: Describes the different elements of the solution, how they are related, how they collaborate and the responsibilities of each element.
– The consequences: The results of the applying the pattern (Gamma et.al, 1994).
Example of a design pattern
Different design patterns have been developed for divers needs and applying for specific contexts. Depending of the problem’s nature and the environment of the solution, we might choose to apply a specific design pattern for each solution. One of the most interesting design patterns is by far the MVC (Model-View-Controller) design pattern, in which the solution is divided into three sub problems, each one concerned with a specific part of the solution (Business Logic, Solution representation, User interactions …etc.). Another important Design Pattern which might be applicable in a different scenarios if the Observer Pattern.
What is the Observer pattern? 
An object oriented software design patter, in which objects have a certain relationship. It keeps the objects informed if something they care about happens (Freeman el.al,2004).
In the Observer pattern, objects have specific roles. Observers are objects that depend on another known as Subject. This last (Subject) notifies the entire dependant Observers of any changes. In general the Subject calls a predefined method in the Observers that handles the notification and reacts appropriately

in_observer_design_pattern_clip_image002
Figure 1: General overview of Observer Design Pattern.

Example of system implemented with Observer Pattern
Here is a good example of the use of Observer Pattern for a real solution. Let’s consider the weather monitoring application. Three layers last mainly compose this:
– The weather station: which the physical device that gets the weather information.
– The Weather data Object: which gets the information from the weather station, makes the different treatments and casts and pasts it to the display object.
– The display object: The interface that displays the weather information in more convivial way.
in_online_datawearhouse_clip_image004
Figure 2: Weather station representation
This is a concrete system that might be designed using the Observer pattern. If we analyse the three elements composing this last we might propose the following implementation.
The weather Data Object is the Observer for the Subject Weather station. Each change in the weather station fires anUpdate method in the Weather Data Object that processes the information and updates the Display device.
The design patterns propose a specific way to address problems and provide suitable solutions. Different design patterns have been developed and we should consider carefully the problem to choose the best and appropriate design patter for our solution.

References:

Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides (1994). Design Pattern, elements of reusable object-oriented software. ISBN: 0-201-63361-2.
Eric Freeman, Elisabeth Robson, Bert Bates, Kathy Sierra (2004). Head First Design Patterns. ISBN: 978-0-596-00712-6

Leave a comment

Show Buttons
Hide Buttons
%d bloggers like this: