CURRENT PROJECTS
loading
CATEGORIES AND POSTS
loading
overset
DEVELOPMENT LOG FOR JIM PALMER
Posted 11/30/2006 in flash


This is a simple Observer Pattern for Flash AS2.0. The idea is to instantiate this as a singleton within your project. I add this class as a plan MovieClip library asset linked to the AS2.0 class.

This code "subscribes" other instantiated classes manually. This means that every other class defined in the project that I want to be subscribed to this observer, I must manually subscribe for every single instance. The subscribed instance are available through this observer via reference (i.e. no Delegate needed here) and attempt to call _on or _off functions that might reside in the subscribed class.

Use-case:
If you have an interface that has multiple forms of input, i.e. several buttons that each perform a specific action, and you want each action to happen one at a time. To dig a little deeper on a specific example: if one button performs a specific flash remoting function that you must wait for a response back before continuing any form of user input. In comes the observer pattern. If this one button that performs an action and you want to disable all other buttons, assuming all button instances have been "subscribed", it takes a single call to the iToggle(false) to make an immediate call to all subscribed instances "_off" method. Once the action is complete, make a single call to the iToggle(true) to make an immediate call to all subscribed instances "_on" methods.

This methodology is very straight forward and is essential in RIA development. As mentioned above, the observer should be instantiated once, globally, as what some consider a singleton pattern. Each object you wish to "subscribe" to the observer will require an _on and _off function defined to perform any action.

The observer pattern code is as follows:
class com.overset.Observer {

	private var subscribers:Array;
	
	function Observer () {
		this.subscribers = new Array();
	}
	
	public function addSubscriber (classInstance):Boolean {
		for (var i=0; i < this.subscribers.length; i++) {
			if (this.subscribers[i] === classInstance) {
				return false;
			}
		}
		this.subscribers.push(classInstance);
		return true;
	}
	
	public function removeSubscriber (classInstance):Boolean {
		for (var i in subscribers) {
			if (this.subscribers[i] === classInstance) {
				this.subscribers.splice(i, 1);
				return true;
			}
		}
		return false;
	}

	public function iToggle (toggle:Boolean):Void {
		// reverse the subscribers in a new temporary array
		var tmpSubscribers:Array = this.subscribers;
		tmpSubscribers.reverse();
		for (var i in tmpSubscribers) {
			if (toggle == true && typeof tmpSubscribers[i]._on == "function") tmpSubscribers[i]._on();
			if (toggle == false && typeof tmpSubscribers[i]._off == "function") tmpSubscribers[i]._off();
		}
	}

	public function update ():Void {
		// reverse the subscribers in a new temporary array
		var tmpSubscribers:Array = this.subscribers;
		tmpSubscribers.reverse();
		for (var i in tmpSubscribers) {
			if (typeof tmpSubscribers[i]._update == "function") {
				tmpSubscribers[i]._update();
			}
		}
	}

}

Please note - this idea is well written/defined in many Flash books and is always the predecessor to introduction the more complex MVC (Model View Controller) pattern which is something worth investigating.
comments
loading
new comment
NAME
EMAIL ME ON UPDATES
EMAIL (hidden)
URL
MESSAGE TAGS ALLOWED: <code> <a> <pre class="code [tab4|tabX|inline|bash]"> <br>
PREVIEW COMMENT
TURING TEST
gravatar