Skip to content

Instantly share code, notes, and snippets.

View rochnyak-d-i's full-sized avatar

rdi rochnyak-d-i

View GitHub Profile
function aop(method, params, moduleName, functionName) {
var that = this;
// Такое формирование ключа кеширования приведено для простоты примера
var key = moduleName + '_' + functionName + '_' + params[0];
cache.get(key, function(error, cachedResult) {
// Получаем ссылку на callback-функцию (всегда передаётся последним параметром)
var callback = params[params.length - 1];
if (error || !cachedResult) {
// Результата в кеше не нашли, передаём управление в метод, подменяя callback-функцию
params[params.length - 1] = function(error, result) {
@rochnyak-d-i
rochnyak-d-i / chain_of_responsibility.js
Created October 28, 2014 05:06
JS шаблон цепочка обязанностей
var NO_TOPIC = -1;
var Topic;
function Handler(s, t) {
this.successor = s || null;
this.topic = t || 0;
}
Handler.prototype = {
handle: function () {
@rochnyak-d-i
rochnyak-d-i / command.js
Created October 28, 2014 05:05
JS шаблон команда
var CarManager = {
/* request information */
requestInfo:function (model, id) {
return 'The purchase info for ' + model + ' with ID ' + id + ' is being processed...';
},
/* purchase the car */
buyVehicle:function (model, id) {
return 'You have successfully purchased Item ' + id + ', a ' + model + '.';
@rochnyak-d-i
rochnyak-d-i / decorator.js
Created October 28, 2014 05:04
JS шаблон декоратор
//первый способ
function StripedBall( ball ) {
this._ball = ball
}
StripedBall.prototype = {
constructor: StripedBall,
draw: function() {
this._ball.draw();
//и еще какие то действия
@rochnyak-d-i
rochnyak-d-i / facade.js
Created October 28, 2014 05:03
JS шаблон фасад
var module = (function() {
var _private = {
i: 5,
get: function() {
console.log('Текущее значение:' + this.i);
},
set: function(val) {
this.i = val;
},
run: function() {
@rochnyak-d-i
rochnyak-d-i / factory.js
Created October 28, 2014 05:03
JS шаблон стратегия
//способ первый
function ShapeFactory(size, color)
{
this.size = size;
this.color = color;
}
ShapeFactory.prototype =
{
constructor: ShapeFactory,
@rochnyak-d-i
rochnyak-d-i / iterator.js
Created October 28, 2014 05:02
JS шаблон итератор
var agg = (function () {
var index = 0,
data = [1, 2, 3, 4, 5],
length = data.length;
return {
next:function () {
var element;
@rochnyak-d-i
rochnyak-d-i / mediator.js
Created October 28, 2014 05:01
JS шаблон медиатор
var mediator = (function() {
var subscribe = function(channel, fn) {
if (!mediator.channels[channel]) mediator.channels[channel] = [];
mediator.channels[channel].push({ context: this, callback: fn });
return this;
},
publish = function(channel) {
if (!mediator.channels[channel]) return false;
var args = Array.prototype.slice.call(arguments, 1);
@rochnyak-d-i
rochnyak-d-i / memoization.js
Created October 28, 2014 05:00
JS шаблон мемоизации
function calculation(x, y)
{
var key = x.toString() + "|" + y.toString();
var result = 0;
if (!calculation.memento[key])
{
for (var i = 0; i < y; ++i) result += x;
calculation.memento[key] = result;
}
@rochnyak-d-i
rochnyak-d-i / observer.js
Created October 28, 2014 04:58
JS шаблон слушатель
Event = function() {
this._observers = [];
}
Event.prototype = {
raise: function (data) {
for (var i in this._observers) {
var item = this._observers[i];
item.observer.call(item.context, data);
}
},