Monday, October 08, 2007

Beginner OOP: Observer and Event Delegation Patterns (Part 1)

I'm going to try to post more frequently. Apologies, my posting has been scarce by any means.

This post is for beginner programmers who want to get into object-oriented programming. Let's talk about design patterns. As you may know object oriented programming is all about objects, creating definitions of them and planning interactions between them.

Now design patterns are about common situations encountered in programming. Implementing them and using them is a common practice for programmers and is the day to day challenge of the object-oriented programmer. Of course, there's always millions of ways to hack around problems and get around things, but hacking code, producing spaghetti code, always creates problems when further changes are later required. Structuring good code while it can take more time initially to plan and carefully understand always shortens the amount of time required to later make changes. Good implementation of design patterns always directly solve problems.

It is possible to use design patterns wrong and to over-program. Remember while programming you should account for changes you will later make, but you don't have to program rules for every situation, only the ones you will encounter. It would be useless to write algorithms for situations that will never occur, all situations should obviously be understood and predetermined as use cases in advance.

The Observer Pattern
So let's talk about a common design pattern that people use frequently, the observer pattern.
Like I said earlier the observer pattern is all about interaction between your code. In the observer design pattern you have several objects which require action when a particular piece of data changes value.

That is you have an object that is observable and an object that is your observer. The object that is the center of attention is the observable, this object has data which you are interested in. When this data changes it sends an update call to all of its observers, who then execute the required actions based on this value change in the interested data.

Code for a simple Observable:
class Observable {
private var _list :Array;
private var _data :String;

public function Observable() {
_list=[];
}

public function register($o:Observer):Void {
_list.push($o);
}

public function set data($s:String):Void {
_data = $s;
_update();
}

public _update():Void {
var n:int=_list.length;
for(var i:int=0; i
}
}


Code for a simple Observer:
public class Observer {
public function Observer() { }

public function update($data:Object):Void {
/* do something exciting! */
}
}


Now to set these classes up and use them:
// let's instantiate them
var parent:Observable = new Observable();
var child1:Observer = new Observable();
var child2:Observer = new Observable();

// let's set it up
parent.register(child1);
parent.register(child2);

// now let's get the parent to
parent.data = "new data woohoo!";


Once the parent changes its data all of its children should then change their data.

This is very useful if you want to create a class to manage your in a class dedicated to being your data model, such as when you use the Model-View-Controller design pattern.

Next time let's talk about the event delegation design pattern.

1 comment:

Anonymous said...

Hello,
I just registered and wanted to pop in and introduce myself to the people. Let me just say that it is a great site that you have here.

Here are some other web sites and blogs that might be of interest to you.

[url=http://skintoday.info]skintoday.info[/url] - skintoday.info
[url=http://skintoday.info]skincareblog.info[/url] - skincareblog.info
[url=http://skintoday.info]skinsmart.info[/url] - skinsmart.info
[url=http://skintoday.info]skincareshop.info[/url] - skincareshop.info

skin care products and tips and tricks