Observer Design Pattern
Since the 1990s, web development has seen significant advancements; new technologies and tools are constantly being designed and developed to provide enhanced solutions with high user interactivity and powerful processes. Websites have evolved from static ones that were primarily used to display static information about products and companies and were difficult to maintain and upgrade. When it came to website updates, web site administrators had a lot on their plates. The appearance of approaches and methods for development, more commonly known as design patterns, is a significant advancement in the field of web development.
What are the Design Patterns?
A pattern describes a recurring problem in our environment and proposes a core solution to that problem (Gamma et.al, 1994). This definition was provided by Christopher Alexander for buildings and towns, but it also applies to 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 to meet various needs and to be applied in specific contexts. Depending on the nature of the problem and the environment in which the solution will be implemented, we may choose to use a different design pattern for each solution. The MVC (Model-View-Controller) design pattern, in which the solution is divided into three sub-problems, each concerned with a specific part of the solution (Business Logic, Solution representation, User interactions…etc.), is by far one of the most interesting design patterns. The Observer Pattern is another important Design Pattern that may be applicable in various scenarios.
What is the Observer pattern?
An object-oriented software design pattern in which objects have a specific relationship with one another. It notifies the objects when something important occurs (Freeman el.al,2004).
Objects in the Observer pattern play specific roles. Observers are objects that are dependent on another object known as the Subject. This final (Subject) notifies all dependent Observers of any changes. In general, the Subject invokes a predefined method in the Observers, which handles the notification and appropriately reacts.
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.
This is a concrete system that could be built with the Observer pattern. If we examine the three components of this final, we might propose the following implementation.
The Subject Weather station’s Observer is the weather Data Object. Each change in the weather station triggers the Weather Data Object’s Update method, which processes the data and updates the Display device.
The design patterns propose a specific approach to addressing problems and providing appropriate solutions. Different design patterns have been developed, and we must carefully consider the problem in order to select the best and most appropriate design pattern for our solution.
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