JavaScript Desing Pattern 2-Adapter, Decorator, and Factory

Ocak 2013 | Ahmet Faik, Yazılım Geliştirme Uzmanı
Adapter

var Faik = {
    GetName: function () {
        alert('ahmet');
    }
};
 
var FaikAdapter = {
    Info: function () {
        Faik.GetName();
    }
};
 
//Kullanım
FaikAdapter.Info();

Decorator


var Dog = function(){};
Dog.prototype = {
    Woof: function () {
        alert('woof woof (zaten bir başlarsa susmaz :D )');
    }
};
 
var DogDecorator = function (dog) {
    this.dog = dog;
};
 
DogDecorator.prototype = {
    Woof:function(){
        this.dog.woof();
    }
};

Biraz daha genişletirsek

var PowerDogDecorator = function(dog){
    DogDecorator.call(this,dog);
}
 
PowerDogDecorator.prototype = new DogDecorator();
PowerDogDecorator.prototype.Woof = function () {
    //ya bunu
    this.dog.drive();
    // yada parent içerisindekini cagırabilirsin
    DogDecorator.prototype.Woof.call(this);
};

Factory


var DogFactory() = { makeWoof:function(features){ var car = new Car(); if (features && features.length) { var i = 0, l = features.length; for (; i < l; i++) { var feature = features[i]; switch(feature) { case 'PowerDog': dog= new PowerDogDecorator(dog); break; } } } return dog; } } // Kullanım Var Kopuk = DogFactory.makeWoof(['PowerDog']);