Skip to content

Commit b4dd0d0

Browse files
committed
Fixing some typos and adding a new instance of approach to visitor
1 parent 0273df6 commit b4dd0d0

File tree

4 files changed

+67
-8
lines changed

4 files changed

+67
-8
lines changed

Chapter 5/State.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ var Westeros;
2626

2727
var BankAccountManager = (function () {
2828
function BankAccountManager() {
29+
this.currentState = new GoodStandingState(this);
2930
}
3031
BankAccountManager.prototype.Deposit = function (amount) {
3132
this.currentState.Deposit(amount);
@@ -34,8 +35,8 @@ var Westeros;
3435
BankAccountManager.prototype.Withdraw = function (amount) {
3536
this.currentState.Withdraw(amount);
3637
};
37-
BankAccountManager.prototype.addToBalance = function (newBalance) {
38-
this.balance = newBalance;
38+
BankAccountManager.prototype.addToBalance = function (amount) {
39+
this.balance += amount;
3940
};
4041
BankAccountManager.prototype.getBalance = function () {
4142
return this.balance;

Chapter 5/State.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ module Westeros.Banking{
3232
export class BankAccountManager{
3333
private balance: number;
3434
private currentState: IState;
35+
constructor() {
36+
this.currentState = new GoodStandingState(this);
37+
}
38+
3539
public Deposit(amount: number)
3640
{
3741
this.currentState.Deposit(amount);
@@ -41,9 +45,9 @@ module Westeros.Banking{
4145
{
4246
this.currentState.Withdraw(amount);
4347
}
44-
public addToBalance(newBalance: number)
48+
public addToBalance(amount: number)
4549
{
46-
this.balance = newBalance;
50+
this.balance += amount;
4751
}
4852
public getBalance():number{
4953
return this.balance;

Chapter 5/Visitor.js

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,27 @@ var Westeros;
5454
})();
5555
Army.Lord = Lord;
5656

57+
var InstanceOfExample = (function () {
58+
function InstanceOfExample() {
59+
}
60+
InstanceOfExample.prototype.Execute = function () {
61+
var collection = [];
62+
collection.push(Object.create(Knight));
63+
collection.push(Object.create(FootSoldier));
64+
collection.push(new Lord());
65+
collection.push(new Archer());
66+
67+
for (var i = 0; i < collection.length; i++) {
68+
if (collection[i] instanceof Westeros.Army.Knight)
69+
collection[i].printName();
70+
else
71+
console.log("No match");
72+
}
73+
};
74+
return InstanceOfExample;
75+
})();
76+
Army.InstanceOfExample = InstanceOfExample;
77+
5778
var IfExample = (function () {
5879
function IfExample() {
5980
}
@@ -97,7 +118,7 @@ var Westeros;
97118
function SelectiveNamePrinterVisitor() {
98119
}
99120
SelectiveNamePrinterVisitor.prototype.visit = function (memberOfArmy) {
100-
if (memberOfArmy._type == "Westeros.Army.Knight") {
121+
if (memberOfArmy instanceof Westeros.Army.Knight) {
101122
this.VisitKnight(memberOfArmy);
102123
} else {
103124
console.log("Not a knight");
@@ -113,5 +134,12 @@ var Westeros;
113134
var Army = Westeros.Army;
114135
})(Westeros || (Westeros = {}));
115136

116-
var b = new Westeros.Army.VisitorExample();
137+
console.log("Instance of");
138+
var a = new Westeros.Army.InstanceOfExample();
139+
a.Execute();
140+
console.log("Type of");
141+
var b = new Westeros.Army.IfExample();
117142
b.Execute();
143+
console.log("Internal type");
144+
var c = new Westeros.Army.VisitorExample();
145+
c.Execute();

Chapter 5/Visitor.ts

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,25 @@ module Westeros.Army{
5151
}
5252
}
5353

54+
export class InstanceOfExample{
55+
Execute()
56+
{
57+
var collection = [];
58+
collection.push(new Knight());
59+
collection.push(new FootSoldier());
60+
collection.push(new Lord());
61+
collection.push(new Archer());
62+
63+
for(var i = 0; i< collection.length; i++)
64+
{
65+
if(collection[i] instanceof Westeros.Army.Knight)
66+
collection[i].printName();
67+
else
68+
console.log("No match");
69+
}
70+
}
71+
}
72+
5473
export class IfExample{
5574
Execute()
5675
{
@@ -92,7 +111,7 @@ module Westeros.Army{
92111
class SelectiveNamePrinterVisitor implements IVisitor{
93112
public visit(memberOfArmy: IMemberOfArmy)
94113
{
95-
if(memberOfArmy._type == "Westeros.Army.Knight")
114+
if(memberOfArmy instanceof Westeros.Army.Knight)
96115
{
97116
this.VisitKnight(memberOfArmy);
98117
}
@@ -108,5 +127,12 @@ module Westeros.Army{
108127
}
109128
}
110129

111-
var b = new Westeros.Army.VisitorExample();
130+
console.log("Instance of");
131+
var a = new Westeros.Army.InstanceOfExample();
132+
a.Execute();
133+
console.log("Type of");
134+
var b = new Westeros.Army.IfExample();
112135
b.Execute();
136+
console.log("Vistor example");
137+
var c = new Westeros.Army.VisitorExample();
138+
c.Execute();

0 commit comments

Comments
 (0)