๐๐ปโโ๏ธContents
1. Compositeย
2. Decoratorย
3. Observe
4. Facade
5. Mediator
๊ฐ๋ณ ๊ฐ์ฒด์ ๋ณตํฉ ๊ฐ์ฒด๋ค์ ๋์ผํ ๋ฐฉ๋ฒ์ผ๋ก ๋ค๋ฃฐ ์ ์๋ ํจํด
function EquipmentComposition(name) {
this.equipments = [];
this.name = name;
}
EquipmentComposition.prototype.add = function(equipment) {
this.equipments.push(equipment);
};
EquipmentComposition.prototype.getPrice = function() {
return this.equipments.map(function(equipment) {
return equipment.getPrice();
}).reduce(function(a, b) {
return a + b;
});
};
function Equipment() {}
Equipment.prototype.getPrice = function() {
return this.price;
};
composit
function Equipment() {}
Equipment.prototype.getPrice = function() {
return this.price;
};
function FloppyDisk() {
this.name = "Floppy Disk";
this.price = 70;
}
FloppyDisk.prototype = Object.create(Equipment.prototype);
function HardDrive() {
this.name = "Hard Drive";
this.price = 250;
}
HardDrive.prototype = Object.create(Equipment.prototype);
function Memory() {
this.name = "8gb memomry";
this.price = 280;
}
Memory.prototype = Object.create(Equipment.prototype);
leaf
โย Client์์ leaf์ Composite๋ฅผ ๊ตฌ๋ถํ ํ์๊ฐ ์๊ธฐ ๋๋ฌธ์ ๊ด๋ฆฌํ๊ธฐ๊ฐ ์ฝ๋ค
ย
โย left/composite๋ก ๊ตฌ์ฑํ์ฌ ์ผ๊ด๋ ๊ณ์ธต์ ์ ์งํ ์ ์๋ค.
ย
โย composite์ leaf๋ ๋ ๋ฆฝ์ ์ผ๋ก ๋์ํ๋ฏ๋ก ์๋ก์ด ์์๊ฐ ์ถ๊ฐ๋๋๋ผ๋ย
ย ย ๋ถ์ํจ๊ณผ๊ฐ ์๋ค.
ย
โย jQuery๋ฅผ ๋ํ์ ์ธ ์๋ก ๋ค ์ ์๋ค..
์ํฉ์ ๋ฐ๋ผ ์ด๋ค ๊ฐ์ฒด์ ์ฑ ์์ ๋ง๋ถ์ด๋ ํจํด
ํด๋์ค ์์
// ์ปคํผ๋ ์์คํ๋ ์๋ถํ ์์... ์ํผํด๋์ค๋ก ์ ์ธ
function Espresso() {
this.cost = 2500;
}
// ์๋ฉ๋ฆฌ์นด๋
ธ, ์นดํ๋ผ๋ผ.. ์๋ธํด๋์ค๋ก ์ ์ธ
function Americano() {
Espresso.call(this);
this.cost = (new Espresso()).cost + 500;
this.water = 250;
}
function CafeLatte() {
Americano.call(this);
this.cost = (new Americano()).cost + 500;
this.milk = 100;
}
console.log(new Espresso());
// { cost: 2500 }
console.log(new Americano());
// { cost: 3000, water: 250 }
console.log(new CafeLatte());
//{ cost: 3500, water: 250, milk: 100 }
๋ฐ์ฝ๋ ์ดํฐ ์ ์ฉ
// ์์คํ๋ ์์ ๋ฌผ์ ๋ฃ์...
function Water (espresso) {
espresso.cost = espresso.cost + 500;
espresso.water = 250;
return espresso; //ํจ์ ์ฒด์ด๋
}
// ์์คํ๋ ์์ ์ฐ๋ฅ๋ฅผ ๋ฃ์..
function Milk (espresso) {
espresso.cost = espresso.cost + 500;
espresso.milk = 100;
return espresso;
}
var espresso = new Espresso();
// { cost: 2500 }
var americano = Water(new Espresso());
// { cost: 3000, water: 250 }
var cafeLatte = Milk(Water(new Espresso()));
// { cost: 3500, water: 250, milk: 100 }
Ghost ์ฝ๋์์ ๋ฐ์ฝ๋ ์ดํฐ
// ํ๋ผ๋งคํฐ๋ก ๋ฐ์ apiMethod ํจ์๋ฅผ ๋ฐ๋๋ค.
http = function http(apiMethod) {
return function apiHandler(req, res, next) {
// request ๊ฐ์ฒด๋ฅผ ํตํด ๋์ด์จ ์์ฒญ๋ฐ์ดํฐ๋ฅผ ์ ๊ทํ ํ๋ค.
// body, params, query, files ๋ฅผ object์ options ๊ฐ์ฒด์ ๋ชจ์ ๋ด๋๋ค.
var object = req.body,
options = _.extend({}, req.files, req.query, req.params, {
context: {
user: (req.user && req.user.id) ? req.user.id : null
}
});
if (_.isEmpty(object)) {
object = options;
options = {};
}
// ์ ๊ทํ๋ ์์ฒญ๋ฐ์ดํฐ๋ฅผ ํ๋ผ๋ฉํฐ๋ก ๋์ด์จ apiMethod ํจ์์ ๋๊ฒจ์ค๋ค.
return apiMethod(object, options).tap(function onSuccess(response) {
// ์๋ต ํค๋๋ฅผ ์ค์ ํ๋ค.
return addHeaders(apiMethod, req, res, (response || {}));
}).then(function then(response) {
// ์๋ต ๋ฐ๋๋ฅผ ์ค์ ํ๋ค.
res.json(response || {});
}).catch(function onAPIError(error) {
// ์๋ฌ ์ฒ๋ฆฌ
next(error);
});
};
};
โย ์ปคํผ ์์ : ์ํฉ์ ๋ฐ๋ผ coffe ๊ฐ์ฒด์ ์ฑ ์์ ๋ง๋ถ์
ย
โย ๊ณ ์คํธ ์์ : ์ํฉ์ ๋ฐ๋ผ apiMethod ํจ์์ ์ฑ ์์ ๋ง๋ถ์
ย
=> ์ํฉ์ ๋ฐ๋ผ ์ด๋ค ๊ฐ์ฒด์ ์ฑ ์์ ๋ง๋ถ์ด๋ ํจํด
๋ค์ ํ๋ฒ ์ ๋ฆฌํด๋ณด๋ฉด...
function Product() {
this.price = 0;
this.actions = [];
}
Product.prototype.setBasePrice = function(val) {
this.price = val;
this.notifyAll();
};
Product.prototype.register = function(observer) {
this.actions.push(observer);
};
Product.prototype.unregister = function(observer) {
this.actions = this.actions.filter(function(el) {
return el != observer;
});
};
Product.prototype.notifyAll = function() {
return this.actions.forEach(function(el) {
el.update(this);
}.bind(this));
};
var fees = {
update: function(product) {
product.price = product.price * 1.2;
}
};
var proft = {
update: function(product) {
product.price = product.price * 2;
}
};
โย ๊ด์ฐฐ์ ์๊ฐ ๋ง์์ง๋ฉด ์ฑ๋ฅ์ด ์ ํ๋ ์ ์๋ค.
์๋ธ์์คํ ์ ๋จ์ํ๋ ์ธํฐํ์ด์ค๋ฅผ ์ ๊ณตํ๋ ํจํด
์๋ธ์์คํ ์ ๋จ์ํ๋ ์ธํฐํ์ด์ค๋ฅผ ์ ๊ณตํ๋ ํจํด
// discount, shipping, fees ์คํ
var shopFacade = {
calc: function(price) {
price = discount(price);
price = fees(price);
price += shipping();
return price;
}
};
// ํ ์ธ
function discount(value) {
return value * 0.9;
}
// ๋ฐฐ์ก๋ฃ
function shipping() {
return 5;
}
// ์์๋ฃ
function fees(value) {
return value * 1.05;
}
โย ์๋ก์ด ํจํด์ด ์๋๋ค. ํ์์ ์ฌ์ฉํ๊ณ ์๋ ๋ถ๋ถ์ด ์๋ค.
ย
โ ์์์ฑ์ ๋ณด์ฅํด์ผ ๋๋ค๋ฉด ํผ์ฌ๋ ํจํด์ ์ฌ์ฉ
ย
โ DB ํธ๋์ญ์ ์ฒ๋ฆฌ๋ ํผ์ฌ๋ ํจํด์ ์ฌ์ฉํ ์ ์์ ๊ฒ ๊ฐ๋ค.
๊ฐ์ฒด๊ฐ์ M:N๊ด๊ณ์์ M:1๋ก ๊ด๊ณ๋ฅผ ๋ฎ์ถ๋ ํจํด
function TrafficTower() {
this.airplanes = [];
}
TrafficTower.prototype.requestPositions = function() {
return this.airplanes.map(function(airplane) {
return airplane.position;
});
};
function Airplane(position, trafficTower) {
this.position = position;
this.trafficTower = trafficTower;
this.trafficTower.airplanes.push(this);
}
Airplane.prototype.requestPositions = function() {
return this.trafficTower.requestPositions();
};
โย ๋ณต์กํ ๊ด๊ณ๋ฅผ ์ธํฐํ์ด์คํ ํ๋ ์ ์์ Facade์ ์ ์ฌํ ์ ์ด ์๋ค.
ย
โ ์ฐ๊ด๋๋ ๊ฐ์ฒด๊ฐ ๋ง์์ง์๋ก ์ค๊ณ์ ๋ฐ๋ฅธ ๋น์ฉ์ด ์ฆ๊ฐํ๋ฏ๋ก ์ํฉ์ ํ์คํ
ย ย ํ์
ํด์ผ๋ ๊ฒ ๊ฐ๋ค.
ย