Design patterns (Observer, Singleton, Factory Method and Adapter) implemented in Dojo Toolkit JS

This is a list of design patterns, implemented in Dojo JS

I'v found difficult to work with some restrictions as the use of static variables, in this kind of implementation

Observer


//Observer pattern

dojo.declare("com.dev.dojo.Observer",[],
{
state: "observer",
obs: null,
constructor:function(obl){
this.obs = obl;
},
update: function(sta){
this.state = sta;
status = this.obs.updateObserver(this);

console.log("udate observer result for "+ this + ": " + status);
}


});

dojo.declare("com.dev.dojo.Observable",[],
{
state: "observable",
observers: [],

Observervable:function(obss){
this.observers = obss;
},
addObserver: function(o){
idx = dojo.indexOf(this.observers,o);
if(idx!=null && idx>=0)
this.observers[idx] = o;
else
this.observers.push(o);
},
getObservers: function(){
return this.observers;
},
notifyAll: function(stat){
this.state = stat;
for(i=0;i< this.observers.length;i++)
this.observers[i].update(this.state);
},
updateObserver:function(observer){
idx = dojo.indexOf(this.observers,observer);
if(idx!=null && idx>=0){
this.observers[idx] = observer;
return 1;
}
return -1;
}
,
displayObservers: function(s) {
for(i=0;i s+= " Observer " + i + " state: " + this.observers[i].state +"\n";
}
return s;
}

});

var osvr = null;
var osvrs = [];
var obble = new com.dev.dojo.Observable();
for(i=0;i<5;i++){
osvr = new com.dev.dojo.Observer(obble);
osvrs.push(osvr);
obble.addObserver(osvr);
}

obble.notifyAll("NEWMessage");

s = " Observable State :" + obble.state +"\n ";

s = obble.displayObservers(s);

alert(s);
//End Observer pattern

Singleton


//Singleton pattern
var created = false;
dojo.declare("com.dev.dojo.Singleton",[],{

singleton:null,
__isCreated__: created,

getInstance: function(){
if(!this.__isCreated__){
created = true;
this.singleton = new com.dev.dojo.Singleton();
}
return this.singleton;
},

doTask: function(seconds){
for(i=0;i console.log("Spooling " + i);
}


});


singleton1 = new com.dev.dojo.Singleton().getInstance();

singleton2 = new com.dev.dojo.Singleton().getInstance();

if(singleton1==singleton2)
alert("Singletons Equals!!");

singleton1.doTask(5);
singleton2.doTask(10);
//End of singleton pattern

Factory Method


//Factory method pattern

dojo.declare("com.dev.dojo.Animal",[],{
type:"Animal",
saySomething: function(){
return "I'm too generic to say something";
}
});
dojo.declare("com.dev.dojo.Dog",[com.dev.dojo.Animal],{
type:"Dog",
saySomething: function(){
return "Guau,guau,guau";
}
});

dojo.declare("com.dev.dojo.Tiger",[com.dev.dojo.Animal],{
type:"Tiger",
saySomething: function(){
return "Roarrrr,roarrrrrr";
}
});

dojo.declare("com.dev.dojo.Factory",[],{

getInstance:function(type){
var instance = null;
if(type==new com.dev.dojo.Animal().type)
instance = new com.dev.dojo.Animal();
else if(type==new com.dev.dojo.Tiger().type)
instance = new com.dev.dojo.Tiger();
else if(type==new com.dev.dojo.Dog().type)
instance = new com.dev.dojo.Dog();

return instance;
}

});
var t = "Dog";
var o = com.dev.dojo.Factory().getInstance(t);

alert(o.saySomething());

t="Tiger";

o = com.dev.dojo.Factory().getInstance(t);

alert(o.saySomething());

//End of factory method

Adapter


//Adapter pattern
/* Example of adapter pattern*/

function FlyingEngine() {
var operation = "Principal ";
var state = -1;
this.turnOnEngine=function(){
this.operation += "Engine is turning ON\n";
this.state = 0;
}
this.takingOver=function(){
if(this.state<1)
this.operation += "Can't take over until engine is turn on";
this.operation += "Taking over\n";
this.state=2;
}

this.flying=function(){
if(state<2)
operation+="Not posible to fly until taking over";
this.operation += "FLYING\n";
this.state=3;
}


}

function Helicopter(){
var operation = "Principal ";
var flyingEngine = new FlyingEngine();
this.rotorsWarm=function(){
if(this.state==0){
/*adapter->*/ flyingEngine.operation = "Rotors ";
flyingEngine.turnOnEngine();
this.operation += flyingEngine.operation;
this.operation += "warming rotors:"
for(i=0;i<5;i++)
this.operation +=i
}
this.operation += "\n";

this.state+=1;
}


}


dojo.extend(Helicopter,new FlyingEngine());

var h1 = new Helicopter();
var s = "";
h1.turnOnEngine();
h1.rotorsWarm();
h1.takingOver();
h1.flying();

s = h1.operation;

alert(s);


Comments

Popular posts from this blog

3D with POV-Ray, isosurfaces example

Algorithms: Backpack problem, Greedy , Dynamic Programming techniques (C++)

Classic Email, Web mail application refactory for visual effects (software engineering-efficiency-UX)