Sunday, February 15, 2009

State Pattern with code



Definition: Allow an object to alter its behavior when its internal state changes. The object will appear to change its class.



var Machin = function(){
this.gearState = new Gear_0(0, this);

//Method
this.setGearState = function(state){
this.gearState = state;
};
this.getGearState = function(){
return this.gearState.getGearState();
};

this.setSpeed = function(speed){
this.gearState.setSpeed(speed);
};

this.getSpeed = function(){
return this.gearState.getSpeed();
};

}

/**
* Since javascript features it OOP with its duck typing
* thus we assume that all 'gears' class follow an interface 'gears'
*/

var Gear_0 = function(speed, machin){
this.speed = speed;
this.machin = machin;
this.setSpeed = function(speed){
this.speed = speed;
if(speed > 1) {
this.machin.setGearState(new Gear_1(this));
}
};
this.getSpeed = function(){
return this.speed;
};
this.getMachin = function(){
return this.machin;
};
this.getGearState = function(){
return 'Gear 0';
};
}

var Gear_1 = function(state){
this.speed = state.getSpeed();
this.machin = state.getMachin();
this.setSpeed = function(speed){
this.speed = speed;
if(this.speed > 30) this.machin.setGearState(new Gear_2(this));
else if(this.speed <> 0) this.machin.setGearState(new Gear_0(this.getSpeed()), this.getMachin());
};
this.getSpeed = function(){
return this.speed;
};
this.getMachin = function(){
return this.machin;
};
this.getGearState = function(){
return 'Gear 1';
};
};

var Gear_2 = function(state){
this.speed = state.getSpeed();
this.machin = state.getMachin();
this.setSpeed = function(speed){
this.speed = speed;
if(this.speed > 60) this.machin.setGearState(new Gear_3(this));
else if(this.speed <> 30) this.machin.setGearState(new Gear_2(this));
};
this.getSpeed = function(){
return this.speed;
};
this.getMachin = function(){
return this.machin;
};
this.getGearState = function(){
return 'Gear 2';
};
};

var Gear_3 = function(state){
this.speed = state.getSpeed();
this.machin = state.getMachin();
this.setSpeed = function(speed){
this.speed = speed;
if(this.speed > 80 ) this.machin.setGearState(new Gear_4(this));
else if(this.speed > 60 && this.speed < getspeed =" function(){" getmachin =" function(){" getgearstate =" function(){" gear_4 =" function(state){" speed =" state.getSpeed();" machin =" state.getMachin();" setspeed =" function(speed){" speed =" speed;"> 60 && this.speed <>
};
this.getSpeed = function(){
return this.speed;
};
this.getMachin = function(){
return this.machin;
};
this.getGearState = function(){
return 'Gear 4';
};
}

//1. speed at 0
var machin = new Machin();
machin.setSpeed(3);

//2. speed up
console.log(machin.getGearState());
machin.setSpeed(32);
console.log(machin.getGearState());
machin.setSpeed(62);
console.log(machin.getGearState());
machin.setSpeed(82);
console.log(machin.getGearState());

//3. slow down
machin.setSpeed(62);
console.log(machin.getGearState());
machin.setSpeed(32);
console.log(machin.getGearState());

No comments: