1. Introduction
Over a period of time, software engineers were facing a common problem during the development of various software programs. There were no standards to instruct them how to design and proceed. The issue became significant when a new member (experienced or unexperienced; it does not matter) joined the team and was assigned to do something from scratch or to modify something in the existing product. As already mentioned, since there were no standards, it took a lot of effort to become familiar with the existing product. Design Patterns simply addresses this issue and makes a common platform for all developers. We shall remember that these patterns were intended to be applied in object-oriented designs with the intention of reuse.
In 199495, four IsErich Gamma, Richard Helm, Ralph Johnson and John Vlissidespublished Design PatternsElements of Reusable Object-Oriented Software (Addison-Wesley, 1995) in which they initiated the concept of design patterns for software development. These Is became known as the Gang of Four (GoF). They introduced 23 patterns which were developed by experienced software engineers over a very long period of time. As a result, now if any new member joins a development team and he knows that the new system is following some specific design patterns, he can actively participate in the development process with the other members of the team within a very short period of time.
The first concept of real-life design pattern came from the building architect Christopher Alexander. In his experience he came to understand some common problems. Then he tried to address those issues with related solutions (for building design) in a uniform manner. People believe that the software industry grasped those concepts because software engineers can also relate their product applications with these building applications.
Each pattern describes a problem which occurs over and over again in our environment, and then describes the core of the solution to that problem, in such a way that you can use this solution a million times over, without ever doing it the same way twice.
Christopher Alexander
GoF assures us that though the patterns were described for buildings and towns, the same concepts can be applied for the patterns in object-oriented design. We can substitute the original concepts of walls and doors with objects and interfaces. The common thing in both is that at the core, both types of patterns are solution to problems in some context.
In 1995 the original concepts were discussed with C++. Sun Microsystems released its first public implementationJava 1.0in 1995 and then it went through various changes. So, the key point is: Java was relatively new at that time. In this book, well try to examine these core concepts with Java. The book is written in Java, but still, if you are familiar with any other popular programming languages (C#, C++ etc.), you can also grasp the concept very easily because I has made as his main focus the design patterns and how we can implement the concept with the basic Java language construct. Again: he has chosen simple, easy-to-remember examples to help you to develop these concepts easily.
2. Observer Patterns
GoF Definition: Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically.
Concept
In this pattern, there are many observers (objects) which are observing a particular subject (object). Observers are basically interested and want to be notified when there is a change made inside that subject. So, they register themselves to that subject. When they lose interest in the subject they simply unregister from the subject. Sometimes this model is also referred to as the Publisher-Subscriber model.
Real-Life Example
We can think about a celebrity who has many fans. Each of these fans wants to get all the latest updates of his/her favorite celebrity. So, he/she can follow the celebrity as long as his/her interest persists. When he loses interest, he simply stops following that celebrity. Here we can think of the fan as an observer and the celebrity as a subject.
Computer World Example
In the world of computer science, consider a simple UI-based example, where this UI is connected with some database (or business logic). A user can execute some query through that UI and after searching the database, the result is reflected back in the UI. In most of the cases we segregate the UI with the database. If a change occurs in the database, the UI should be notified so that it can update its display according to the change.
Illustration
Now let us directly enter into our simple example. Here I have created one observer (though you can create more) and one subject. The subject maintains a list for all of its observers (though here we have only one for simplicity). Our observer here wants to be notified when the flag value changes in the subject. With the output, you will discover that the observer is getting the notifications when the flag value changed to 5 or 25. But there is no notification when the flag value changed to 50 because by this time the observer has unregistered himself from the subject.
Package Explorer view
High-level structure of the parts of the program is as follows:
Implementation
package observer.pattern.demo;
import java.util.*;
class Observer
{
public void update()
{
System. out .println("flag value changed in Subject");
}
}
interface ISubject
{
void register(Observer o);
void unregister( Observer o);
void notifyObservers();
}
class Subject implements ISubject
{
List observerList = new ArrayList();
private int _flag;
public int getFlag()
{
return _flag;
}
public void setFlag( int _flag)
{
this ._flag=_flag;
//flag value changed .So notify observer(s)
notifyObservers();
}
@Override
public void register(Observer o)
{
observerList.add(o);
}
@Override
public void unregister(Observer o)
{
observerList.remove(o);
}
@Override
public void notifyObservers()
{
for ( int i=0;i
{
observerList.get(i).update();
}
}
}
class ObserverPatternEx
{
public static void main(String[] args)
{
System. out .println("***Observer Pattern Demo***\n");
Observer o1 = new Observer();
Subject sub1 = new Subject();
sub1.register(o1);