Skip to content

Commit 7105b22

Browse files
committed
update: completing the design patterns
1 parent b7bc0ef commit 7105b22

6 files changed

+219
-7
lines changed

snippets/js/patterns/design-patterns/README.md

Lines changed: 108 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,25 +106,92 @@ var ${1:MyModule} = ( function( window, undefined ) {
106106
### [jdp.factory] Factory
107107

108108
```javascript
109+
function ${1:Animal}(${2:name}) {
110+
${9}
111+
}
112+
113+
function ${3:Rabbit}(${2:name}) {
114+
var ${4:rabbit} = ${1:Animal}(${2:name})
115+
116+
var ${5:parentRun} = ${4:rabbit}.${6:run}
117+
118+
${4:rabbit}.${7:jump} = function() {
119+
${10:alert(${2:name} + " jumped!");}
120+
}
121+
122+
${4:rabbit}.${6:run} = function() {
123+
${5:parentRun}.call(this)
124+
${11:alert("fast");}
125+
}
126+
127+
return ${4:rabbit}
128+
}
109129

130+
${4:rabbit} = ${3:Rabbit}("${8:rab}");
110131
```
111132

112133
### [jdp.flyweight] Flyweight
113134

114135
```javascript
136+
${0:'use strict';}
137+
138+
var ${1:Flyweight} = function (${2:intrinisicState}) {
139+
this.${2:intrinisicState} = ${2:intrinisicState};
140+
};
115141

142+
${1:Flyweight}.prototype.operation = function (${4:extrinsicState}) {
143+
${3:// Perform some action using intrinsic and extrinsic state}
144+
return this.${2:intrinisicState} * ${4:extrinsicState};
145+
};
146+
147+
module.exports = ${1:Flyweight};
116148
```
117149
118150
### [jdp.mediator] Mediator
119151
120152
```javascript
121-
153+
var mediator = (function(){
154+
var subscribe = function(channel, fn){
155+
if(!mediator.channels[channel]) mediator.channels[channel] = [];
156+
mediator.channels[channel].push({ context : this, callback : fn });
157+
return this;
158+
};
159+
var publish = function(channel){
160+
if(!mediator.channels[channel]) return false;
161+
var args = Array.prototype.slice.call(arguments, 1);
162+
for(var i = 0, l = mediator.channels[channel].length; i < l; i++){
163+
var subscription = mediator.channels[channel][i];
164+
subscription.callback.apply(subscription.context.args);
165+
};
166+
return this;
167+
};
168+
return {
169+
channels : {},
170+
publish : publish,
171+
subscribe : subscribe,
172+
installTo : function(obj){
173+
obj.subscribe = subscribe;
174+
obj.publish = publish;
175+
}
176+
};
177+
}());${1}
122178
```
123179
124180
### [jdp.mixin] Mixin
125181
126182
```javascript
127-
183+
var ${1:Circle} = function() {};
184+
${1:Circle}.prototype = {
185+
${2:area}: function() {
186+
${3:return Math.PI * this.radius * this.radius};
187+
},
188+
${4:grow}: function() {
189+
${5:this.radius++;}
190+
},
191+
${6:shrink}: function() {
192+
${7:this.radius--;}
193+
}
194+
};${8}
128195
```
129196
130197
### [jdp.module] Module
@@ -144,7 +211,46 @@ var ${1:moduleName} = (function() {
144211
### [jdp.observer] Observer
145212
146213
```javascript
214+
var ${1:Publisher} = {
215+
${2:subscribers}: {
216+
${6:any}: []
217+
},
218+
219+
${3:subscribe}: function(fn, type) {
220+
221+
type = type || "${6:any}";
222+
if (typeof this.${2:subscribers}[type] === "undefined") {
223+
this.${2:subscribers}[type] = [];
224+
}
225+
226+
this.${2:subscribers}[type].push(fn);
227+
},
228+
229+
${4:unsubscribe}: function(fn, type) {
230+
if (typeof this.${2:subscribers}[type] !== undefined) {
231+
this.${2:subscribers}[type].pull(fn);
232+
}
233+
},
234+
235+
${5:publish}: function(type, obj) {
236+
var ${2:subscribers} = this.${2:subscribers}[type];
237+
238+
for (var i = 0, max = ${2:subscribers}.length; i < max; i++) {
239+
${2:subscribers}[i](obj);
240+
}
241+
}
242+
};
243+
244+
245+
${7://var paper = Object.create(${1:Publisher});
246+
//var joe = {
247+
// shout : function(obj){
248+
// console.log("oh i got issue" + obj);
249+
// }
250+
//};
147251

252+
//paper.${3:subscribe}(joe.shout, "new issue");
253+
//paper.${5:publish}("new issue", 27);}
148254
```
149255
150256
### [jdp.prototype] Prototype

snippets/js/patterns/design-patterns/js-design-patterns-factory.sublime-snippet

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,27 @@
11
<snippet>
22
<content><![CDATA[
3-
${1}
3+
function ${1:Animal}(${2:name}) {
4+
${9}
5+
}
6+
7+
function ${3:Rabbit}(${2:name}) {
8+
var ${4:rabbit} = ${1:Animal}(${2:name})
9+
10+
var ${5:parentRun} = ${4:rabbit}.${6:run}
11+
12+
${4:rabbit}.${7:jump} = function() {
13+
${10:alert(${2:name} + " jumped!");}
14+
}
15+
16+
${4:rabbit}.${6:run} = function() {
17+
${5:parentRun}.call(this)
18+
${11:alert("fast");}
19+
}
20+
21+
return ${4:rabbit}
22+
}
23+
24+
${4:rabbit} = ${3:Rabbit}("${8:rab}");
425
]]></content>
526
<tabTrigger>jdp.factory</tabTrigger>
627
<description>jdp - Factory</description>

snippets/js/patterns/design-patterns/js-design-patterns-flyweight.sublime-snippet

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,17 @@
11
<snippet>
22
<content><![CDATA[
3-
${1}
3+
${0:'use strict';}
4+
5+
var ${1:Flyweight} = function (${2:intrinisicState}) {
6+
this.${2:intrinisicState} = ${2:intrinisicState};
7+
};
8+
9+
${1:Flyweight}.prototype.${5:operation} = function (${4:extrinsicState}) {
10+
${3:// Perform some action using intrinsic and extrinsic state}
11+
return this.${2:intrinisicState} * ${4:extrinsicState};
12+
};
13+
14+
module.exports = ${1:Flyweight};
415
]]></content>
516
<tabTrigger>jdp.flyweight</tabTrigger>
617
<description>jdp - Flyweight</description>

snippets/js/patterns/design-patterns/js-design-patterns-mediator.sublime-snippet

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,30 @@
11
<snippet>
22
<content><![CDATA[
3-
${1}
3+
var mediator = (function(){
4+
var subscribe = function(channel, fn){
5+
if(!mediator.channels[channel]) mediator.channels[channel] = [];
6+
mediator.channels[channel].push({ context : this, callback : fn });
7+
return this;
8+
};
9+
var publish = function(channel){
10+
if(!mediator.channels[channel]) return false;
11+
var args = Array.prototype.slice.call(arguments, 1);
12+
for(var i = 0, l = mediator.channels[channel].length; i < l; i++){
13+
var subscription = mediator.channels[channel][i];
14+
subscription.callback.apply(subscription.context.args);
15+
};
16+
return this;
17+
};
18+
return {
19+
channels : {},
20+
publish : publish,
21+
subscribe : subscribe,
22+
installTo : function(obj){
23+
obj.subscribe = subscribe;
24+
obj.publish = publish;
25+
}
26+
};
27+
}());${1}
428
]]></content>
529
<tabTrigger>jdp.mediator</tabTrigger>
630
<description>jdp - Mediator</description>

snippets/js/patterns/design-patterns/js-design-patterns-mixin.sublime-snippet

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,17 @@
11
<snippet>
22
<content><![CDATA[
3-
${1}
3+
var ${1:Circle} = function() {};
4+
${1:Circle}.prototype = {
5+
${2:area}: function() {
6+
${3:return Math.PI * this.radius * this.radius};
7+
},
8+
${4:grow}: function() {
9+
${5:this.radius++;}
10+
},
11+
${6:shrink}: function() {
12+
${7:this.radius--;}
13+
}
14+
};${8}
415
]]></content>
516
<tabTrigger>jdp.mixin</tabTrigger>
617
<description>jdp - Mixin</description>

snippets/js/patterns/design-patterns/js-design-patterns-observer.sublime-snippet

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,45 @@
11
<snippet>
22
<content><![CDATA[
3-
${1}
3+
var ${1:Publisher} = {
4+
${2:subscribers}: {
5+
${6:any}: []
6+
},
7+
8+
${3:subscribe}: function(fn, type) {
9+
10+
type = type || "${6:any}";
11+
if (typeof this.${2:subscribers}[type] === "undefined") {
12+
this.${2:subscribers}[type] = [];
13+
}
14+
15+
this.${2:subscribers}[type].push(fn);
16+
},
17+
18+
${4:unsubscribe}: function(fn, type) {
19+
if (typeof this.${2:subscribers}[type] !== undefined) {
20+
this.${2:subscribers}[type].pull(fn);
21+
}
22+
},
23+
24+
${5:publish}: function(type, obj) {
25+
var ${2:subscribers} = this.${2:subscribers}[type];
26+
27+
for (var i = 0, max = ${2:subscribers}.length; i < max; i++) {
28+
${2:subscribers}[i](obj);
29+
}
30+
}
31+
};
32+
33+
34+
${7://var paper = Object.create(${1:Publisher});
35+
//var joe = {
36+
// shout : function(obj){
37+
// console.log("oh i got issue" + obj);
38+
// }
39+
//};
40+
41+
//paper.${3:subscribe}(joe.shout, "new issue");
42+
//paper.${5:publish}("new issue", 27);}
443
]]></content>
544
<tabTrigger>jdp.observer</tabTrigger>
645
<description>jdp - Observer</description>

0 commit comments

Comments
 (0)