Skip to content

Commit 0629d18

Browse files
committed
Adding code examples for chapter 3
1 parent 828470f commit 0629d18

12 files changed

+543
-1
lines changed

Chapter 3/AbstractFactory.js

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
var Westros;
2+
(function (Westros) {
3+
(function (Ruling) {
4+
(function (Lannister) {
5+
var KingJoffery = (function () {
6+
function KingJoffery() {
7+
}
8+
KingJoffery.prototype.makeDecision = function () {
9+
};
10+
KingJoffery.prototype.marry = function () {
11+
};
12+
return KingJoffery;
13+
})();
14+
Lannister.KingJoffery = KingJoffery;
15+
16+
var LordTywin = (function () {
17+
function LordTywin() {
18+
}
19+
LordTywin.prototype.makeDecision = function () {
20+
};
21+
return LordTywin;
22+
})();
23+
Lannister.LordTywin = LordTywin;
24+
25+
var LannisterFactory = (function () {
26+
function LannisterFactory() {
27+
}
28+
LannisterFactory.prototype.getKing = function () {
29+
return new KingJoffery();
30+
};
31+
LannisterFactory.prototype.getHandOfTheKing = function () {
32+
return new LordTywin();
33+
};
34+
return LannisterFactory;
35+
})();
36+
Lannister.LannisterFactory = LannisterFactory;
37+
})(Ruling.Lannister || (Ruling.Lannister = {}));
38+
var Lannister = Ruling.Lannister;
39+
})(Westros.Ruling || (Westros.Ruling = {}));
40+
var Ruling = Westros.Ruling;
41+
})(Westros || (Westros = {}));
42+
43+
var Westros;
44+
(function (Westros) {
45+
(function (Ruling) {
46+
(function (Targaryen) {
47+
var KingAerys = (function () {
48+
function KingAerys() {
49+
}
50+
KingAerys.prototype.makeDecision = function () {
51+
};
52+
KingAerys.prototype.marry = function () {
53+
};
54+
return KingAerys;
55+
})();
56+
Targaryen.KingAerys = KingAerys;
57+
58+
var LordConnington = (function () {
59+
function LordConnington() {
60+
}
61+
LordConnington.prototype.makeDecision = function () {
62+
};
63+
return LordConnington;
64+
})();
65+
Targaryen.LordConnington = LordConnington;
66+
67+
var TargaryenFactory = (function () {
68+
function TargaryenFactory() {
69+
}
70+
TargaryenFactory.prototype.getKing = function () {
71+
return new KingAerys();
72+
};
73+
TargaryenFactory.prototype.getHandOfTheKing = function () {
74+
return new LordConnington();
75+
};
76+
return TargaryenFactory;
77+
})();
78+
Targaryen.TargaryenFactory = TargaryenFactory;
79+
})(Ruling.Targaryen || (Ruling.Targaryen = {}));
80+
var Targaryen = Ruling.Targaryen;
81+
})(Westros.Ruling || (Westros.Ruling = {}));
82+
var Ruling = Westros.Ruling;
83+
})(Westros || (Westros = {}));

Chapter 3/AbstractFactory.ts

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
module Westros.Ruling
2+
{
3+
export interface IKing
4+
{
5+
makeDecision();
6+
marry();
7+
}
8+
9+
export interface IHandOfTheKing
10+
{
11+
makeDecision();
12+
}
13+
14+
export interface IRulingFamilyAbstractFactory{
15+
getKing():IKing;
16+
getHandOfTheKing():IHandOfTheKing;
17+
}
18+
}
19+
20+
module Westros.Ruling.Lannister
21+
{
22+
export class KingJoffery implements Westros.Ruling.IKing{
23+
makeDecision(){}
24+
marry(){}
25+
}
26+
27+
export class LordTywin implements Westros.Ruling.IHandOfTheKing{
28+
makeDecision(){}
29+
}
30+
31+
export class LannisterFactory implements Westros.Ruling.IRulingFamilyAbstractFactory{
32+
getKing():IKing{
33+
return new KingJoffery();
34+
}
35+
getHandOfTheKing():IHandOfTheKing{
36+
return new LordTywin();
37+
}
38+
}
39+
}
40+
41+
module Westros.Ruling.Targaryen
42+
{
43+
export class KingAerys implements Westros.Ruling.IKing{
44+
makeDecision(){}
45+
marry(){}
46+
}
47+
48+
export class LordConnington implements Westros.Ruling.IHandOfTheKing{
49+
makeDecision(){}
50+
}
51+
52+
export class TargaryenFactory implements Westros.Ruling.IRulingFamilyAbstractFactory{
53+
getKing():IKing{
54+
return new KingAerys();
55+
}
56+
getHandOfTheKing():IHandOfTheKing{
57+
return new LordConnington();
58+
}
59+
}
60+
61+
}

Chapter 3/Builder.js

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
var Westros;
2+
(function (Westros) {
3+
var Tournament = (function () {
4+
function Tournament() {
5+
}
6+
return Tournament;
7+
})();
8+
Westros.Tournament = Tournament;
9+
10+
var TournamentBuilder = (function () {
11+
function TournamentBuilder() {
12+
}
13+
TournamentBuilder.prototype.build = function (builder) {
14+
return builder.build();
15+
};
16+
return TournamentBuilder;
17+
})();
18+
Westros.TournamentBuilder = TournamentBuilder;
19+
20+
var LanisterTournamentBuilder = (function () {
21+
function LanisterTournamentBuilder() {
22+
}
23+
LanisterTournamentBuilder.prototype.build = function () {
24+
var tournament = new Tournament();
25+
tournament.events.push(new Event("Joust"));
26+
tournament.events.push(new Event("Melee"));
27+
28+
tournament.attendees.push(new Attendee("Jamie"));
29+
30+
tournament.prizes.push(new Prize("Gold"));
31+
tournament.prizes.push(new Prize("More Gold"));
32+
33+
return tournament;
34+
};
35+
return LanisterTournamentBuilder;
36+
})();
37+
Westros.LanisterTournamentBuilder = LanisterTournamentBuilder;
38+
39+
var BaratheonTournamentBuilder = (function () {
40+
function BaratheonTournamentBuilder() {
41+
}
42+
BaratheonTournamentBuilder.prototype.build = function () {
43+
var tournament = new Tournament();
44+
tournament.events.push(new Event("Joust"));
45+
tournament.events.push(new Event("Melee"));
46+
47+
tournament.attendees.push(new Attendee("Stannis"));
48+
tournament.attendees.push(new Attendee("Robert"));
49+
50+
return tournament;
51+
};
52+
return BaratheonTournamentBuilder;
53+
})();
54+
Westros.BaratheonTournamentBuilder = BaratheonTournamentBuilder;
55+
56+
var Event = (function () {
57+
function Event(name) {
58+
this.name = name;
59+
}
60+
return Event;
61+
})();
62+
Westros.Event = Event;
63+
64+
var Prize = (function () {
65+
function Prize(name) {
66+
this.name = name;
67+
}
68+
return Prize;
69+
})();
70+
Westros.Prize = Prize;
71+
72+
var Attendee = (function () {
73+
function Attendee(name) {
74+
this.name = name;
75+
}
76+
return Attendee;
77+
})();
78+
Westros.Attendee = Attendee;
79+
})(Westros || (Westros = {}));

Chapter 3/Builder.ts

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
module Westros
2+
{
3+
export class Tournament{
4+
public events:Event[];
5+
public prizes:Prize[];
6+
public attendees: Attendee[]
7+
constructor(){
8+
9+
}
10+
}
11+
12+
export class TournamentBuilder
13+
{
14+
build(builder)
15+
{
16+
return builder.build();
17+
}
18+
}
19+
20+
export class LanisterTournamentBuilder{
21+
public build(){
22+
23+
var tournament = new Tournament();
24+
tournament.events.push(new Event("Joust"));
25+
tournament.events.push(new Event("Melee"));
26+
27+
tournament.attendees.push(new Attendee("Jamie"));
28+
29+
tournament.prizes.push(new Prize("Gold"));
30+
tournament.prizes.push(new Prize("More Gold"));
31+
32+
return tournament;
33+
}
34+
}
35+
36+
export class BaratheonTournamentBuilder{
37+
public build(){
38+
var tournament = new Tournament();
39+
tournament.events.push(new Event("Joust"));
40+
tournament.events.push(new Event("Melee"));
41+
42+
tournament.attendees.push(new Attendee("Stannis"));
43+
tournament.attendees.push(new Attendee("Robert"));
44+
45+
return tournament;
46+
}
47+
}
48+
49+
export class Event{
50+
constructor(public name: string){}
51+
}
52+
53+
export class Prize{
54+
constructor(public name: string){}
55+
}
56+
57+
export class Attendee{
58+
constructor(public name: string){}
59+
}
60+
}

Chapter 3/FactoryMethod.js

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
var Westros;
2+
(function (Westros) {
3+
(function (Religion) {
4+
var Prayer = (function () {
5+
function Prayer() {
6+
}
7+
Prayer.prototype.pray = function (godName) {
8+
GodFactory.Build(godName).prayTo();
9+
};
10+
return Prayer;
11+
})();
12+
Religion.Prayer = Prayer;
13+
14+
var GodDeterminant = (function () {
15+
function GodDeterminant(religionName, prayerPurpose) {
16+
this.religionName = religionName;
17+
this.prayerPurpose = prayerPurpose;
18+
}
19+
return GodDeterminant;
20+
})();
21+
22+
var GodFactory = (function () {
23+
function GodFactory() {
24+
}
25+
GodFactory.Build = function (godName) {
26+
if (godName === "watery")
27+
return new WateryGod();
28+
if (godName === "ancient")
29+
return new AncientGods();
30+
return new DefaultGod();
31+
};
32+
return GodFactory;
33+
})();
34+
35+
var WateryGod = (function () {
36+
function WateryGod() {
37+
}
38+
WateryGod.prototype.prayTo = function () {
39+
};
40+
return WateryGod;
41+
})();
42+
Religion.WateryGod = WateryGod;
43+
44+
var AncientGods = (function () {
45+
function AncientGods() {
46+
}
47+
AncientGods.prototype.prayTo = function () {
48+
};
49+
return AncientGods;
50+
})();
51+
Religion.AncientGods = AncientGods;
52+
53+
var DefaultGod = (function () {
54+
function DefaultGod() {
55+
}
56+
DefaultGod.prototype.prayTo = function () {
57+
};
58+
return DefaultGod;
59+
})();
60+
Religion.DefaultGod = DefaultGod;
61+
})(Westros.Religion || (Westros.Religion = {}));
62+
var Religion = Westros.Religion;
63+
})(Westros || (Westros = {}));

Chapter 3/FactoryMethod.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
module Westros.Religion
2+
{
3+
export class Prayer{
4+
pray(godName:string){
5+
GodFactory.Build(godName).prayTo();
6+
}
7+
}
8+
9+
class GodDeterminant{
10+
constructor(public religionName: string, public prayerPurpose: string){}
11+
}
12+
13+
class GodFactory{
14+
static Build(godName: string):God{
15+
if(godName === "watery")
16+
return new WateryGod();
17+
if(godName === "ancient")
18+
return new AncientGods();
19+
return new DefaultGod();
20+
}
21+
}
22+
23+
export interface God
24+
{
25+
prayTo():void;
26+
}
27+
28+
export class WateryGod implements God{
29+
public prayTo(){
30+
31+
}
32+
}
33+
34+
export class AncientGods implements God{
35+
public prayTo(){
36+
37+
}
38+
}
39+
40+
export class DefaultGod implements God{
41+
public prayTo(){
42+
43+
}
44+
}
45+
}

0 commit comments

Comments
 (0)