forked from kbuzyk/GoF-patterns-in-JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GoF_patterns_in_JavaScript.js
1489 lines (1437 loc) · 37.3 KB
/
GoF_patterns_in_JavaScript.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
author: Kyrylo Buzykin (kbuzykin@gmail.com)
year: 2013
specially for Script'n'Code by SoftServe Inc.
*/
// inheritance
var __extends = function (Child, Parent) {
for (var key in Parent) if (Parent.hasOwnProperty(key)) Child[key] = Parent[key];
function Surrogate() { this.constructor = Child; };
Surrogate.prototype = Parent.prototype;
Child.prototype = new Surrogate();
Child._super = Parent.prototype;
};
/*
Creational
*/
console.log(">>>>>>> CREATIONAL PATTERNS");
// 1) Singleton
console.log("/* Singleton */");
var SoftServeCompany = (function () {
var self;
function constructor() {
}
function SoftServeCompany () {
if (!self) {
constructor.call(this);
self = this;
}
return self;
};
SoftServeCompany.prototype.createStategicBusinessUnit = function () {};
return SoftServeCompany;
})();
var company1 = new SoftServeCompany();
var company2 = new SoftServeCompany();
console.log(company1);
console.log(company1 === company2);
// 2) Prototype
console.log("\n/* Prototype */");
var JetLab = (function () {
function JetLab () {
this.name = "JetLab";
}
JetLab.prototype.hirePeople = function () {
console.log("People are hired for project needs.");
},
JetLab.prototype.startProject = function () {
console.log("Project has been started. Client is happy.");
},
JetLab.prototype.introduce = function () {
console.log(this.name);
}
return JetLab;
})();
var JaNet = (function (_super) {
__extends(JaNet, _super);
function JaNet () {
this.name = "JaNet";
}
return JaNet;
})(JetLab);
var jN = new JaNet();
console.log(jN);
jN.introduce();
jN.hirePeople();
jN.startProject();
// 2.1) Prototype
console.log("\n/* Prototype */");
var JetLab = {
name: "JetLab",
hirePeople: function () {
console.log("People are hired for project needs.");
},
startProject: function () {
console.log("Project has been started. Client is happy.");
},
introduce: function () {
console.log(this.name);
}
};
var JaNet = Object.create(JetLab, {
name: {
value: "JaNet"
}
});
console.log(JaNet);
JaNet.introduce();
JaNet.hirePeople();
JaNet.startProject();
// 3) Factory Method
console.log("\n/* Factory Method */");
var Student = (function () {
function Student () {
this.name = arguments[0].name;
this.direction = arguments[0].direction;
}
return Student;
})();
var Developer = (function (_super) {
__extends(Developer, _super);
function Developer () {
_super.apply(this, arguments);
this.skills = ["development", "bugfixing"];
}
Developer.prototype.develop = function () {
console.log("Scope has been developed.");
}
Developer.prototype.fixBugs = function () {
console.log("Bug fixing is in progress.");
}
return Developer;
})(Student);
var QC = (function (_super) {
__extends(QC, _super);
function QC () {
_super.apply(this, arguments);
this.skills = ["writing test cases", "testing"];
}
QC.prototype.writeTestCase = function () {
console.log("Test has been written.");
}
QC.prototype.test = function () {
console.log("Testing is in progress.");
}
return QC;
})(Student);
var ITAcademy = (function () {
function ITAcademy (){};
ITAcademy.prototype.graduateStudent = function () {};
return ITAcademy;
})();
var ITAcademyDeveloperGroup = (function (_super) {
__extends(ITAcademyDeveloperGroup, _super);
function ITAcademyDeveloperGroup () {}
ITAcademyDeveloperGroup.prototype.graduateStudent = function () {
return new Developer(arguments[0]);
};
return ITAcademyDeveloperGroup;
})(ITAcademy);
var ITAcademyQCGroup = (function (_super) {
__extends(ITAcademyQCGroup, _super);
function ITAcademyQCGroup () {}
ITAcademyQCGroup.prototype.graduateStudent = function () {
return new QC(arguments[0]);
};
return ITAcademyQCGroup;
})(ITAcademy);
var groupDev = new ITAcademyDeveloperGroup();
var groupQC = new ITAcademyQCGroup();
var dev = groupDev.graduateStudent({
name: "Vasya",
direction: "UI"
});
console.log(dev);
// 4) Abstract Factory
console.log("\n/* Abstract Factory */");
var Student = (function () {
function Student () {
this.name = arguments[0].name;
this.direction = arguments[0].direction;
}
return Student;
})();
var DeveloperAbstract = (function (_super) {
__extends(DeveloperAbstract, _super);
function DeveloperAbstract () {
_super.apply(this, arguments);
this.skills = ["development", "bugfixing"];
}
DeveloperAbstract.prototype.develop = function () {
}
DeveloperAbstract.prototype.fixBugs = function () {
}
return DeveloperAbstract;
})(Student);
var QCAbstract = (function (_super) {
__extends(QCAbstract, _super);
function QCAbstract () {
_super.apply(this, arguments);
this.skills = ["writing test cases", "testing"];
}
QCAbstract.prototype.writeTestCase = function () {
}
QCAbstract.prototype.test = function () {
}
return QCAbstract;
})(Student);
var Developer = (function (_super) {
__extends(Developer, _super);
function Developer () {
_super.apply(this, arguments);
}
Developer.prototype.develop = function () {
console.log("Scope has been developed.");
}
Developer.prototype.fixBugs = function () {
console.log("Bug fixing is in progress.");
}
return Developer;
})(DeveloperAbstract);
var QC = (function (_super) {
__extends(QC, _super);
function QC () {
_super.apply(this, arguments);
}
QC.prototype.writeTestCase = function () {
console.log("Test has been written.");
}
QC.prototype.test = function () {
console.log("Testing is in progress.");
}
return QC;
})(QCAbstract);
var ITAcademyAbstract = (function () {
function ITAcademyAbstract () {};
ITAcademyAbstract.prototype.graduateDeveloper = function () {};
ITAcademyAbstract.prototype.graduateQC = function () {};
return ITAcademyAbstract;
})();
var ITAcademy = (function (_super) {
__extends(ITAcademy, _super);
function ITAcademy () {};
ITAcademy.prototype.graduateDeveloper = function () {
return new Developer(arguments[0]);
};
ITAcademy.prototype.graduateQC = function () {
return new QC();
};
return ITAcademy;
})(ITAcademyAbstract);
var dev = new ITAcademy().graduateDeveloper({
name: "Vasya",
direction: "UI"
});
console.log(dev);
// 4.1) Factory
console.log("\n/* Factory */");
var Student = (function () {
function Student () {
this.name = arguments[0].name;
this.direction = arguments[0].direction;
}
return Student;
})();
var Developer = (function (_super) {
__extends(Developer, _super);
function Developer () {
_super.apply(this, arguments);
this.skills = ["development", "bugfixing"];
}
Developer.prototype.develop = function () {
console.log("Scope has been developed.");
}
Developer.prototype.fixBugs = function () {
console.log("Bug fixing is in progress.");
}
return Developer;
})(Student);
var QC = (function (_super) {
__extends(QC, _super);
function QC () {
_super.apply(this, arguments);
this.skills = ["writing test cases", "testing"];
}
QC.prototype.writeTestCase = function () {
console.log("Test has been written.");
}
QC.prototype.test = function () {
console.log("Testing is in progress.");
}
return QC;
})(Student);
var ITAcademy = (function () {
function ITAcademy () {};
ITAcademy.prototype.graduateDeveloper = function () {
return new Developer(arguments[0]);
};
ITAcademy.prototype.graduateQC = function () {
return new QC(arguments[0]);
};
return ITAcademy;
})();
var dev = new ITAcademy().graduateDeveloper({
name: "Vasya",
direction: "UI"
});
console.log(dev);
// 5) Builder
console.log("\n/* Builder */");
var Student = (function () {
function Student () {
this.skill;
}
Student.prototype.coding = function () {
console.log("Coding on " + this.skill);
}
Student.prototype.setSkill = function (_skill) {
this.skill = _skill;
}
return Student;
})();
var Group = (function () {
function Group () {
this.student;
}
Group.prototype.employStudent = function () {
return this.student;
}
Group.prototype.graduateNewStudent = function () {
this.student = new Student();
}
Group.prototype.giveSkill = function () {}
return Group;
})();
var JavaGroup = (function (_super) {
__extends(JavaGroup, _super);
function JavaGroup () {
_super.apply(this, arguments);
}
JavaGroup.prototype.giveSkill = function () {
this.student.setSkill("Java");
}
return JavaGroup;
})(Group);
var UIGroup = (function (_super) {
__extends(UIGroup, _super);
function UIGroup () {
_super.apply(this, arguments);
}
UIGroup.prototype.giveSkill = function () {
this.student.setSkill("JavaScript");
}
return UIGroup;
})(Group);
var ITAcademy = (function () {
function ITAcademy () {
this.group;
}
ITAcademy.prototype.setGroup = function (_group) {
this.group = _group;
}
ITAcademy.prototype.graduateStudent = function () {
return this.group.employStudent();
}
ITAcademy.prototype.teachStudent = function () {
this.group.graduateNewStudent();
this.group.giveSkill();
}
return ITAcademy;
})();
var SoftServeAcademy = new ITAcademy(),
ui_group = new UIGroup(),
dev;
SoftServeAcademy.setGroup(ui_group);
SoftServeAcademy.teachStudent();
dev = SoftServeAcademy.graduateStudent();
dev.coding();
/*
Structural
*/
console.log("\n\n>>>>>>> STRUCTURAL PATTERNS");
// 6) Decorator
console.log("/* Decorator */");
var QC = (function () {
function QC (name) {
this.name = name;
}
QC.prototype.test = function () {
return 0;
}
return QC;
})();
var ExploratoryTestingDecorator = (function () {
function ExploratoryTestingDecorator (tester) {
this.tester = tester;
}
ExploratoryTestingDecorator.prototype.test = function () {
return this.tester.test() + 30;
}
return ExploratoryTestingDecorator;
})();
var FunctionalTestingDecorator = (function (){
function FunctionalTestingDecorator (tester) {
this.tester = tester;
}
FunctionalTestingDecorator.prototype.test = function () {
return this.tester.test() + 60;
}
return FunctionalTestingDecorator;
})();
var RegressionTestingDecorator = (function (){
function RegressionTestingDecorator (tester) {
this.tester = tester;
}
RegressionTestingDecorator.prototype.test = function () {
return this.tester.test() + 5;
}
return RegressionTestingDecorator;
})();
var AcceptanceTestingDecorator = (function (){
function AcceptanceTestingDecorator (tester) {
this.tester = tester;
}
AcceptanceTestingDecorator.prototype.test = function () {
return this.tester.test() + 5;
}
return AcceptanceTestingDecorator;
})();
var someQC = new QC("Vasya");
console.log(someQC);
var bugScope = 100,
coveredScope = new AcceptanceTestingDecorator (
new RegressionTestingDecorator (
new FunctionalTestingDecorator (
new ExploratoryTestingDecorator(someQC)
)
)
).test(),
missedBugs = bugScope - coveredScope;
console.log(missedBugs);
// 7) Facade
console.log("\n/* Facade */");
var Developer = (function () {
function Developer () {}
Developer.prototype.develop = function (_requirements) {
if (_requirements) {
console.log("Functional, based on iteration scope, is provided.");
return true;
}
}
return Developer;
})();
var QC = (function () {
function QC () {}
QC.prototype.test = function (_functional, _requirements) {
if (_functional === _requirements) {
console.log("Scope is implemented properly.");
return true;
}
}
return QC;
})();
var BA = (function () {
function BA () {};
BA.prototype.getRequirements = function () {
console.log("Requirements for iteration are provided.");
return true;
}
BA.prototype.acceptIteration = function (_product) {
if (_product) {
console.log("Iteration is succeeded.");
} else {
console.log("Iteration has been failed.")
}
}
return BA;
})();
var PM = (function () {
function PM() {
this.developer = new Developer();
this.qc = new QC();
this.ba = new BA();
}
PM.prototype.achieveGoal = function () {
var requirements = this.ba.getRequirements(),
functional = this.developer.develop(requirements),
product = this.qc.test(functional, requirements);
this.ba.acceptIteration(product);
}
return PM;
})();
var scrumTeam = new PM();
scrumTeam.achieveGoal();
// 8) Adapter
console.log("\n/* Adapter */");
var TestAutomation = (function () {
function TestAutomation () {
this.technology = "Java";
}
TestAutomation.prototype.writeTest = function () {
return "Tests on " + this.technology + " are implemented.";
}
return TestAutomation;
})();
var dotNetAdapter = (function () {
function dotNetAdapter (context) {
this.technology = ".Net";
this.context = context;
}
dotNetAdapter.prototype.writeOnDotNet = function () {
console.log(this.context.writeTest.call(this));
}
return dotNetAdapter;
})();
var automator = new TestAutomation();
var dotNetAutomator = new dotNetAdapter(automator);
dotNetAutomator.writeOnDotNet();
// 9) Bridge
console.log("\n/* Bridge */");
var ApplicationTesting = (function () {
function ApplicationTesting () {}
ApplicationTesting.prototype.ExploratoryTesting = function () {
console.log("Exploratory Testing is done.");
}
ApplicationTesting.prototype.FunctionalTesting = function () {
console.log("Functional Testing is done.");
}
ApplicationTesting.prototype.RegressionTesting = function () {
console.log("Regression Testing is done.");
}
ApplicationTesting.prototype.AcceptanceTesting = function () {
console.log("Acceptance Testing is done.");
}
return ApplicationTesting;
})();
var QC = (function () {
function QC (_applicationTesting) {
this.testing = _applicationTesting;
}
QC.prototype.clickRandomlyToEveryWhere = function () {
this.testing.ExploratoryTesting();
}
QC.prototype.goThroughTestCases = function () {
this.testing.FunctionalTesting();
}
QC.prototype.testAllFunctional = function () {
this.testing.RegressionTesting();
}
QC.prototype.talkToBA = function () {
this.testing.AcceptanceTesting();
}
return QC;
})();
var vasyaQC = new QC (new ApplicationTesting ());
vasyaQC.clickRandomlyToEveryWhere();
vasyaQC.goThroughTestCases();
vasyaQC.testAllFunctional();
vasyaQC.talkToBA();
// 10) Proxy
console.log("\n/* Proxy */");
var juniorQC = (function () {
function juniorQC () {}
juniorQC.prototype.writeTestCase = function () {
console.log("Test cases are written.");
}
juniorQC.prototype.test = function () {
console.log("Implementation is tested.");
}
juniorQC.prototype.clarifyRequirements = function () {
console.log("Requirements are clarified.");
}
return juniorQC;
})();
var seniorQC = (function () {
function seniorQC () {
this.pupil = new juniorQC();
}
seniorQC.prototype.writeTestCase = function () {
if (this.pupil) {
this.pupil.writeTestCase();
}
}
seniorQC.prototype.test = function () {
this.pupil.test();
}
seniorQC.prototype.clarifyRequirements = function () {
console.log("Talk to BA.");
this.pupil.clarifyRequirements();
}
return seniorQC;
})();
var mentor = new seniorQC();
mentor.clarifyRequirements();
mentor.writeTestCase();
mentor.test();
// 11) Composite
console.log("\n/* Composite */");
var Fellow = (function () {
function Fellow (_name) {
this.name = _name;
}
Fellow.prototype.getFellowship = function () {}
Fellow.prototype.add = function (child) {
var tmp;
if (tmp = this.getFellowship()) {
tmp.children.push(child);
}
return this;
}
Fellow.prototype.remove = function (child) {
if (tmp = this.getFellowship()) {
for (var i = 0; i < tmp.children.length; i++) {
if (tmp.children[i] === child) {
tmp.children.splice(i, 1);
}
}
}
return this;
}
return Fellow;
})();
var Fellowship = (function (_super) {
__extends(Fellowship, _super);
function Fellowship () {
_super.apply(this, arguments);
this.children = [];
}
Fellowship.prototype.getFellowship = function () {
return this;
}
return Fellowship;
})(Fellow);
var dev = new Fellow ("Developer"),
qc = new Fellow ("QC"),
sbuManager = new Fellow ("SBU Manager"),
vPresident = new Fellow ("Vice-President"),
president = new Fellow ("President"),
team = new Fellowship ("Team"),
sbu = new Fellowship ("SBU"),
solutionGroup = new Fellowship ("Solution group"),
ss = new Fellowship ("SoftServe");
ss.add(president).add(
solutionGroup.add(vPresident).add(
sbu.add(sbuManager).add(
team.add(dev).add(qc)
)
)
);
console.log(ss);
// 12) Flyweight
console.log("\n/* Flyweight */");
// FlyweightFactory
var SDO = (function () {
function SDO () {
this.candidates = {};
}
SDO.prototype.getDeveloper = function (tech) {
if (!(tech in this.candidates)) {
switch (tech) {
case 'Java': this.candidates[tech] = new ConcreteDeveloperJuniorJava1(); break;
case '.Net': this.candidates[tech] = new ConcreteDeveloperSeniorDotNet5(); break;
case 'UI': this.candidates[tech] = new ConcreteDeveloperIntermediateUI2(); break;
}
}
return this.candidates[tech];
}
return SDO;
})();
// Flyweight
var AbstractlDeveloper = (function () {
function AbstractlDeveloper () {
this.technology;
this.position;
this.experience;
}
AbstractlDeveloper.prototype.interview = function () {
console.log("Developing on " + this.technology + " for " + this.experience + " years. Has " + this.position + " position.");
}
return AbstractlDeveloper;
})();
// ConcreteFlyweight
var ConcreteDeveloperJuniorJava1 = (function (_super) {
__extends(ConcreteDeveloperJuniorJava1, _super);
function ConcreteDeveloperJuniorJava1 () {
_super.apply(this, arguments);
this.technology = "Java";
this.position = "Junior";
this.experience = 1;
}
return ConcreteDeveloperJuniorJava1;
})(AbstractlDeveloper);
// ConcreteFlyweight
var ConcreteDeveloperSeniorDotNet5 = (function (_super) {
__extends(ConcreteDeveloperSeniorDotNet5, _super);
function ConcreteDeveloperSeniorDotNet5 () {
_super.apply(this, arguments);
this.technology = ".Net";
this.position = "Senior";
this.experience = 5;
}
return ConcreteDeveloperSeniorDotNet5;
})(AbstractlDeveloper);
// ConcreteFlyweight
var ConcreteDeveloperIntermediateUI2 = (function (_super) {
__extends(ConcreteDeveloperIntermediateUI2, _super);
function ConcreteDeveloperIntermediateUI2 () {
_super.apply(this, arguments);
this.technology = "UI";
this.position = "Intermediate";
this.experience = 2;
}
return ConcreteDeveloperIntermediateUI2;
})(AbstractlDeveloper);
var hr = new SDO(),
interview,
project_needs = ["UI", "Java"];
for (var i = 0; i < project_needs.length; i++) {
interview = hr.getDeveloper(project_needs[i]);
interview.interview();
}
/*
Behavioral
*/
console.log("\n\n>>>>>>> BEHAVIORAL PATTERNS");
// 13) Strategy
console.log("\n/* Strategy */");
// Strategy
var Trainee = (function () {
function Trainee () {}
Trainee.prototype.work = function () {}
return Trainee;
})();
// ConcreteStrategy
var DeveloperTrainee = (function (_super) {
__extends(DeveloperTrainee, _super);
function DeveloperTrainee () {
_super.apply(this, arguments);
}
DeveloperTrainee.prototype.work = function () {
console.log("Starting developing");
}
return DeveloperTrainee;
})(Trainee);
var QCTrainee = (function (_super) {
__extends(QCTrainee, _super);
function QCTrainee () {
_super.apply(this, arguments);
}
QCTrainee.prototype.work = function () {
console.log("Starting testing");
}
return QCTrainee;
})(Trainee);
// Context
var ITAcademy = (function () {
function ITAcademy (_trainee) {
this.strategy = _trainee;
}
ITAcademy.prototype.graduate = function () {
this.strategy.work();
}
return ITAcademy;
})();
var someTrainee = new ITAcademy(new QCTrainee());
someTrainee.graduate();
// 14) Chain of responsibility
console.log("\n/* Chain of responsibility */");
var ResponsiblePerson = (function () {
function ResponsiblePerson (_seniority) {
this.seniority = _seniority;
this.next;
}
ResponsiblePerson.prototype.approve = function (_subject, _priority) {
if (_priority >= this.seniority) {
this.announceResult(_subject);
}
if (typeof this.next !== 'undefined') {
this.next.approve(_subject, _priority);
}
}
ResponsiblePerson.prototype.announceResult = function () {}
ResponsiblePerson.prototype.setNext = function (_responsiblePerson) {
this.next = _responsiblePerson;
return _responsiblePerson;
}
return ResponsiblePerson;
})();
var TeamLead = (function (_super) {
__extends(TeamLead, _super);
function TeamLead () {
_super.apply(this, arguments);
}
TeamLead.prototype.announceResult = function (_subject) {
console.log(_subject + " is approved by Team Lead.");
}
return TeamLead;
})(ResponsiblePerson);
var PM = (function (_super) {
__extends(PM, _super);
function PM () {
_super.apply(this, arguments);
}
PM.prototype.announceResult = function (_subject) {
console.log(_subject + " is approved by Project Manager.");
}
return PM;
})(ResponsiblePerson);
var SBU = (function (_super) {
__extends(SBU, _super);
function SBU () {
_super.apply(this, arguments);
}
SBU.prototype.announceResult = function (_subject) {
console.log(_subject + " is approved by SBU Project Manager.");
}
return SBU;
})(ResponsiblePerson);
var appropriatePerson = new TeamLead(1);
var appropriatePerson1 = appropriatePerson.setNext(new PM(2));
var appropriatePerson2 = appropriatePerson1.setNext(new SBU(3));
appropriatePerson.approve("Task", 1);
appropriatePerson.approve("Day off", 2);
appropriatePerson.approve("Vacation", 3);
// 15) State
console.log("\n/* State */");
var Developer = (function () {
function Developer () {}
Developer.prototype.implementScope = function () {}
Developer.prototype.fixBugs = function () {}
Developer.prototype.reviewCode = function () {}
return Developer;
})();
var JuniorDeveloper = (function (_super) {
__extends(JuniorDeveloper, _super);
function JuniorDeveloper () {}
JuniorDeveloper.prototype.implementScope = function () {
console.log("50% of scope has been implemented properly.");
}
JuniorDeveloper.prototype.fixBugs = function () {
console.log("5 bugs have been resolved.");
}
JuniorDeveloper.prototype.reviewCode = function () {
console.log ("5 bugs have been missed.");
}
return JuniorDeveloper;
})(Developer);
var IntermediateDeveloper = (function (_super) {
__extends(IntermediateDeveloper, _super);
function IntermediateDeveloper () {}
IntermediateDeveloper.prototype.implementScope = function () {
console.log("75% of scope has been implemented properly.");
}
IntermediateDeveloper.prototype.fixBugs = function () {
console.log("7 bugs have been resolved");
}
IntermediateDeveloper.prototype.reviewCode = function () {
console.log ("3 bugs have been missed.");
}
return IntermediateDeveloper;
})(Developer);
var SeniorDeveloper = (function (_super) {
__extends(SeniorDeveloper, _super);
function SeniorDeveloper () {}
SeniorDeveloper.prototype.implementScope = function () {
console.log("100% of scope has been implemented properly.");
}
SeniorDeveloper.prototype.fixBugs = function () {
console.log("10 bugs have been resolved");
}
SeniorDeveloper.prototype.reviewCode = function () {
console.log ("1 bugs have been missed.");
}
return SeniorDeveloper;
})(Developer);
var Developing = (function () {
function Developing () {
this.developer = new JuniorDeveloper();
}
Developing.prototype.implementScope = function () {
this.developer.implementScope();
}
Developing.prototype.fixBugs = function () {
this.developer.fixBugs();
}
Developing.prototype.reviewCode = function () {
this.developer.reviewCode();
}
Developing.prototype.promote = function () {
if (this.developer instanceof JuniorDeveloper) {
this.developer = new IntermediateDeveloper();
} else if (this.developer instanceof IntermediateDeveloper) {
this.developer = new SeniorDeveloper();
}
}
return Developing;
})();
var work = new Developing();
work.implementScope();
work.fixBugs();
work.reviewCode();
work.promote();
work.implementScope();
work.fixBugs();
work.reviewCode();
work.promote();
work.implementScope();
work.fixBugs();
work.reviewCode();
// 16) Iterator
console.log("\n/* Iterator */");
// Agregator
var PM = (function () {
function PM () {}
PM.prototype.createTeam = function () {}
return PM;
})();
// Iterator
var TeamIterator = (function () {
function TeamIterator () {}
TeamIterator.prototype.hasNext = function () {}
TeamIterator.prototype.next = function () {}
TeamIterator.prototype.remove = function () {}
return TeamIterator;
})();
// ConcreteAggregate
var TeamPM = (function (_super) {
__extends(TeamPM, _super);
function TeamPM () {
this.teammates = [];
}
TeamPM.prototype.setTeamMates = function (_teammates) {
this.teammates = _teammates;
}
TeamPM.prototype.createTeam = function () {
return new ConcreteTeam(this.teammates);
}
return TeamPM;
})(PM);
// ConcreteIterator
var ConcreteTeam = (function (_super) {
__extends(ConcreteTeam, _super);
function ConcreteTeam (_teammates) {
this.teammates = _teammates;
this.position = 0;
}
TeamIterator.prototype.hasNext = function () {
return !(this.position >= this.teammates.length || this.teammates.length === 0);
}
TeamIterator.prototype.next = function () {
return this.teammates[this.position++];
}
TeamIterator.prototype.remove = function () {}
return ConcreteTeam;
})(TeamIterator);
var Project = (function () {
function Project (_teamPM1, _teamPM2) {
this.teamPM1 = _teamPM1;
this.teamPM2 = _teamPM2;
}
Project.prototype.reviewTeams = function () {
this.team1 = this.teamPM1.createTeam();
this.team2 = this.teamPM2.createTeam();
console.log(">>First team:");
this.reviewTeamMates(this.team1);
console.log(">>Second team:");
this.reviewTeamMates(this.team2);
}
Project.prototype.reviewTeamMates = function (team) {
while (team.hasNext()) {
console.log(team.next());
}
}
return Project;
})();
var people1 = ["Valera", "Anton"],
people2 = ["Vasya", "Kolya", "Artem"],
team1 = new TeamPM(),
team2 = new TeamPM();
team1.setTeamMates(people1);
team2.setTeamMates(people2);
project = new Project(