Design Patterns Series 5 - Observer Pattern
We talked about Factory pattern in previous post ,in this post we will discover one of design patterns that keep your object know when something has happened and passing a notification to an observer. Actually there is two design pattern do this job, Observer Pattern and Chain of Responsibilities pattern.
Observer Pattern :
It lets several observer objects be notified when a subject object is changed in some way, each observer registers with the subject and when a change occurs the subject notifies them all in parallel (yes at the same time).
The observer design pattern should "define a one-to-many dependency between objects so that when one object changes state, all its dependants are notified and updated automatically ".
You should consider the observer design pattern when you have an object that can cause events to occur - events that you want other objects to know about. Instead of writing everything in one monolithic class, consider breaking control out into a set of objects that will be notified when an event occur.
Consider you(observer object) as an administrator want to be notified when some records inside a database(subject object) have been changed. Also the client(another observer object) who make the changes want to be notified to be sure his changes occurred correctly. So here we have two observer(you & client) objects observe one subject object(database).
When you are putting the observer design pattern into code, set up an interface or abstract class for the observers and you have to keep the methods they implement consistent. We need to set up another interface for the subject which lists the methods subjects must implement. We put a RegisterObserver() method so that the subject can keep track of observers that want to be registered. You should have some way to get rid of them so we add a RemoveObserver() method. And there's a NotifyObservers() method that will notify the observers of some changes.
In Database class we will add another method named EditRecord() that will be called when the user actually does something with the database as deleting, updating or inserting some records. This method will also notify all observers.
Create Our Observers
you can download the full source code here
Observer Pattern :
It lets several observer objects be notified when a subject object is changed in some way, each observer registers with the subject and when a change occurs the subject notifies them all in parallel (yes at the same time).
The observer design pattern should "define a one-to-many dependency between objects so that when one object changes state, all its dependants are notified and updated automatically ".
You should consider the observer design pattern when you have an object that can cause events to occur - events that you want other objects to know about. Instead of writing everything in one monolithic class, consider breaking control out into a set of objects that will be notified when an event occur.
Consider you(observer object) as an administrator want to be notified when some records inside a database(subject object) have been changed. Also the client(another observer object) who make the changes want to be notified to be sure his changes occurred correctly. So here we have two observer(you & client) objects observe one subject object(database).
When you are putting the observer design pattern into code, set up an interface or abstract class for the observers and you have to keep the methods they implement consistent. We need to set up another interface for the subject which lists the methods subjects must implement. We put a RegisterObserver() method so that the subject can keep track of observers that want to be registered. You should have some way to get rid of them so we add a RemoveObserver() method. And there's a NotifyObservers() method that will notify the observers of some changes.
public interface ISubject
{
void RegisterObserver(IObserver O);
void RemoveObserver(IObserver O);
void NotifyObserver();
}
Build the Observer interface, implemented by observers to enable them to get notifications. All you need is a method that will be called when a new notification is ready.
public interface IObserver
{
void Update(String operation , String record);
}
Now we will create the subject to let observer register and has to notify them when an event occurs. To keep track of all observers I will use List object named observers, created in the Database constructor(our subject object).Database class inherits Subject interface so we will implement its methods, RegisterObserver(), RemoveObserver(), and NotifyObserver().In Database class we will add another method named EditRecord() that will be called when the user actually does something with the database as deleting, updating or inserting some records. This method will also notify all observers.
public class Database : ISubject
{
private List<IObserver> observers;
private String operation;
private String record;
public Database()
{
observers = new List<IObserver>();
}
public void RegisterObserver(IObserver O)
{
observers.Add(O);
}
public void RemoveObserver(IObserver O)
{
observers.Remove(O);
}
public void NotifyObserver()
{
for(Int32 i = 0 ; i < observers.Count ; i++)
{
Observer observer = observers[i];
observer.Update(operation , record);
}
}
public void EditRecord(String operation , String record)
{
this.operation = operation;
this.record = record;
NotifyObserver();
}
}
To create an observer, you just have to implement the Observer interface you have created, which has only one method, Update().
public class Administrator: IObserver
{
public Administrator()
{
}
public void Update(String operation , String record)
{
Console.WriteLine("The Administrator says a {0} operation was performed on {1}",
operation ,
record);
}
}
Remember we have another observer want to be notified, yes the client so we will create another observer class for him.public class Client: IObserver
{
public Client()
{
}
public void Update(String operation , String record)
{
Console.WriteLine("The Client says a {0} operation was performed on {1}",
operation ,
record);
}
}
As you see you can implement many observers objects. Now we need to check our observer pattern how it works. First we will create our observer objects then register them to our subject object, after that we will make a change in our database and see what will happen.Create Our Observers
Administrator admin = new Administrator();
Client client = new Client();
Register observers objects to subject object and update databaseDatabase database = new Database(); //Subject Object
database.RegisterObserver(admin);
database.RegisterObserver(client);
database.EditRecord("Delete" , "Record 1");
this code fragment will print out the following resultThe client says a Delete operation was performed on record 1
The Administrator says a Delete operation was performed on record 1
As you see all observers were notified and that what we need. You can note our implementation is more flexible that hard coding everything into one unbreakable block and allow you to add or remove observers at runtime. It couples your objects loosely and builds composite objects using "has-a" relationships.you can download the full source code here
Comments
Post a Comment