Design patterns in Javascript : Observer

August 2, 2011 by nuno costa no comments

This pattern also referred as publisher – subscriber pattern describes a method to allow one object to be aware of another object changes.

It allows you to ask to be notified when the state of the observed object changes, so that you can react on that information.

As you can imagine, this pattern is widely used to implement event systems.

Have you ever write Ajax code or used a call back function?

Then you have used this pattern.

In the typical implementation the object that changes keeps a list of registered observers, when the change happens it notifies the registered objects usually by calling a function of the observer object, that function usually is called cal back function At its simplest form this pattern can be implemented this way

var publisherClass = function(){
	var subscribers=[];
	return{
		subscribe : function(callback){
			subscribers.push(callback);
		},
 
		publish : function(){
			for (var n = 0; n < subscribers.length; n++){
				subscribers[n].call(this, n);
			}
		}
	};
};
 
 
var subscriber = function(parameterFromPublisher){
	console.log("publisher published the following value " + parameterFromPublisher);
};
 
var publisher = new publisherClass();
 
publisher.subscribe(subscriber);
publisher.subscribe(subscriber);
 
publisher.publish();
 
//output:
// publisher published the following value 0
// publisher published the following value 1

This code works by storing an internal references to all the callbacks, when the publish() method is triggered we iterate through all of those references and execute them

All good linux hosts will support sites built with this code, whether in their basic shared hosting plans or in their more advanced business hosting plans

Design patterns in Javascript : Factory

August 1, 2011 by nuno costa 3 comments

As the name implies this pattern is used to create objects,

You can use it when you need to create an object without knowing the exact class, for example you want to create a Milk object but you don’t know if you need to create a ChocolateMilk or LowFatMilk classes.

You can also use it if you need to create pre configured classes where you need the same object several times only with some small configuration changes Now a few examples of the factory pattern implementation in JavaScript, the first example is the classic example of “pre configured” classes

var fooFactoryClass = function(){
	return{
		create : function(width, height){
			var obj = new fooClass();
			obj.setWidth(width);
			obj.setHeight(height);
			return obj;
		}
	};
};
 
var fooFactory = new fooFactoryClass();
var myFooClass = fooFactory.create(100,50);

This is a simple implementation where we constructed a factory class to return a fooClass object but with different configuration options.

The next example a different class is returned according to the options passed to the factory create method.

var chocolateMilk = function(milk){
	return {
		name: "Chocolate Milk"
	};
};
 
var lowFatMilk = function(milk){
	return {
		name: "Low Fat Milk"
	};
};
 
 
var milkFactoryClass = function(){
	return{
		create: function(unknownMilkObject){
			switch(unknownMilkObject.color){
				case "brown":
					return new chocolateMilk();
					break;
				default:
					return new lowFatMilk();
			}
		}
	};
};
 
 
var milkFactory = new milkFactoryClass();
var milkType = {
		color: &#39;brown&#39;
};
var milkObject = milkFactory.create(milkType);
 
console.log(milkObject.name);//outputs Chocolate Milk

The first two object create are the chocolate and low fat milk classes.

The factory method will inspect the color property and return a chocolateMilk or lowFatMilk class depending on the property value

Design patterns in Javascript : Singleton

March 6, 2011 by nuno costa no comments

 

The singleton patterns was documented as a solution to a simple problem, “Only one object of class X can be instantiated at any time in the application”

Typically a singleton is implemented by restricting access to the class constructor, effectively making impossible to instantiate the class

It is common to use singletons for application wide configuration or database access classes.

continue reading »

Your code is CRAP

November 8, 2010 by nuno costa no comments

That is most probably true if you are not unit testing it or collecting metrics.

One of the most important metrics is the Change Risk Analysis and Predictions or CRAP index. continue reading »

Extending a PHP class with plugins

October 31, 2010 by nuno costa 3 comments

It’s has been a while since I post anything useful or technical. My last posts all have ben sponsored by some one!

When I was writing the new version of phMagick I had a few requirements in mind

It should be:

  • Compatible with previous versions
  • Easy to incorporate patches and new features the community sends back
  • Small footprint (let the user choose what features he needs)

Having one big class would defeat the last 2 requirements, it would be too costly to manage contributions, and obviously there is no way to include just the features needed!

I tried some workarounds to php lack of multi inheritance support, but the code was just becoming to messy!

I come up with a solutions to “extend” a class by using a plugin system.

continue reading »

Older Posts »

More from francodacosta.com

© francodacosta.com - All rights reserved