Search content within the blog

Saturday, May 7, 2011

Design Patterns - Observer pattern

In this post we are going to take a look at the Observer pattern.
What Is The Observer Pattern?

The Observer pattern is a way for an object to notify all of it’s dependants when something changes based on a one to many relationship.
The Situation

Continuing to use our city dog registration software senario, one of the project’s requirements is that the dog’s owner’s be notified whenever a dog is processed for an infraction (i.e. picked up by the dog catcher). The Observer pattern is perfectly suited to solve this problem.
Setting Up Our Observer

First before we create our Observer we need to define some classes and interfaces that we will need to use with the Observer.
public interface IInfraction {
string Type { get; set; }
int Id { get; set; }
DateTime Date { get; set; }
string Reason { get; set; }
double Fee { get; set; }
}

public class Infraction : IInfraction {
public string Type { get; set; }

int Id { get; set; }

DateTime Date { get; set; }
string Reason { get; set; }
double Fee { get; set; }
}

public interface IOwner {
int Id { get; set; }

string FirstName { get; set; }

string LastName { get; set; }

string EmailAddress { get; set; }
}

public class Owner : IOwner {
public Owner() { }
public int Id { get; set; }

public string FirstName { get; set; }

public string LastName { get; set; }

public string EmailAddress { get; set; }

}

The above code defines an infraction and an owner for our Observer to use. Now we can setup our Observer and the notifications when a dog gets an infraction.
public interface IDog {
int Id { get; set; }
string Name { get; set; }
void AddInfraction(IInfraction infraction);
void AddOwner(IOwner owner);
}

public class Dog : IDog {
private IList _owners;
private IList _infractions;

public Dog() {
_owners = new List();
_infractions = new List();
}

public void AddOwner(IOwner owner) {
_owners.Add(owner);
}

public void AddInfraction(IInfraction infraction) {
_infractions.Add(infraction);

//notify owners of infraction : the Observer
foreach(IOwner owner in _owners) {
string message = “Dear ” + owner.FirstName + “,\n\n” +
“We are writing you to inform you that there has been an incident with your dog, “ +
this.Name + “, and that the city had to get involved.\n\n” +
“Your dog is being held at the city dog pound for the following reason: “ + infraction.Reason + “.” +
“You can pick them up anytime. There will be a fee of $” + infraction.Fee + ” applied.”;
MessageService.SendEmail(owner.EmailAddress, “cityhall@fictionalcity.com”, message);
}
}

public int Id { get; set; }
public string Name { get; set; }
}

You can see the this pattern at work in the AddInfraction method. Everytime this method is called an infraction is added to the dog’s record and all the owners are sent an email detailing the infraction and where to pick up their dog. It’s nice when you can structure your code to handle situations automatically, for you and your clients.

No comments:

Post a Comment