Skip to content

Commit 94e9cf6

Browse files
committed
Adding patterns from chapter 4
1 parent 0629d18 commit 94e9cf6

File tree

16 files changed

+563
-3
lines changed

16 files changed

+563
-3
lines changed

Chapter 2/code.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
module Westeros.Structures {
2+
export class BaseStructure{
3+
4+
}
5+
export class Castle extends BaseStructure{
6+
name;
7+
constructor(name){
8+
this.name = name;
9+
super();
10+
}
11+
public Build(){
12+
console.log("Castle built: " + this.name);
13+
}
14+
}
15+
}
16+
17+
var cc = new Westeros.Structures.Castle("Winterfell");
18+
cc.Build();
19+
cc.name = "ss";

Chapter 3/FactoryMethod.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ module Westros.Religion
1212

1313
class GodFactory{
1414
static Build(godName: string):God{
15-
if(godName === "watery")
16-
return new WateryGod();
15+
if(godName === "drowned")
16+
return new DrownedGod();
1717
if(godName === "ancient")
1818
return new AncientGods();
1919
return new DefaultGod();
@@ -25,7 +25,7 @@ module Westros.Religion
2525
prayTo():void;
2626
}
2727

28-
export class WateryGod implements God{
28+
export class DrownedGod implements God{
2929
public prayTo(){
3030

3131
}

Chapter 4/Adapter.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
var Westros;
2+
(function (Westros) {
3+
(function (Transportation) {
4+
var SailConfiguration = (function () {
5+
function SailConfiguration() {
6+
}
7+
return SailConfiguration;
8+
})();
9+
Transportation.SailConfiguration = SailConfiguration;
10+
11+
var Ship = (function () {
12+
function Ship() {
13+
}
14+
Ship.prototype.SetRudderAngleTo = function (angle) {
15+
};
16+
Ship.prototype.SetSailConfiguration = function (configuration) {
17+
};
18+
Ship.prototype.SetSailAngle = function (sailId, sailAngle) {
19+
};
20+
Ship.prototype.GetCurrentBearing = function () {
21+
return 7;
22+
};
23+
Ship.prototype.GetCurrentSpeedEstimate = function () {
24+
return 7;
25+
};
26+
Ship.prototype.ShiftCrewWeightTo = function (weightToShift, locationId) {
27+
};
28+
return Ship;
29+
})();
30+
Transportation.Ship = Ship;
31+
32+
var ShipAdapter = (function () {
33+
function ShipAdapter() {
34+
this._ship = new Ship();
35+
}
36+
ShipAdapter.prototype.TurnLeft = function () {
37+
this._ship.SetRudderAngleTo(-30);
38+
this._ship.SetSailAngle(3, 12);
39+
};
40+
ShipAdapter.prototype.TurnRight = function () {
41+
this._ship.SetRudderAngleTo(30);
42+
this._ship.SetSailAngle(5, -9);
43+
};
44+
ShipAdapter.prototype.GoForward = function () {
45+
//do something else to the _ship
46+
};
47+
return ShipAdapter;
48+
})();
49+
Transportation.ShipAdapter = ShipAdapter;
50+
})(Westros.Transportation || (Westros.Transportation = {}));
51+
var Transportation = Westros.Transportation;
52+
})(Westros || (Westros = {}));

Chapter 4/Adapter.ts

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
module Westros.Transportation
2+
{
3+
export interface IShip{
4+
SetRudderAngleTo(angle: number);
5+
SetSailConfiguration(configuration: SailConfiguration);
6+
SetSailAngle(sailId: number, sailAngle: number);
7+
GetCurrentBearing(): number;
8+
GetCurrentSpeedEstimate(): number;
9+
ShiftCrewWeightTo(weightToShift: number, locationId: number);
10+
}
11+
export class SailConfiguration{}
12+
13+
export class Ship implements IShip{
14+
SetRudderAngleTo(angle: number){}
15+
SetSailConfiguration(configuration: SailConfiguration){}
16+
SetSailAngle(sailId: number, sailAngle: number){}
17+
GetCurrentBearing(): number{
18+
return 7;
19+
}
20+
GetCurrentSpeedEstimate(): number{
21+
return 7;
22+
}
23+
ShiftCrewWeightTo(weightToShift: number, locationId: number){}
24+
}
25+
26+
export class ShipAdapter implements SimpleShip{
27+
_ship: Ship;
28+
constructor(){
29+
this._ship = new Ship();
30+
}
31+
32+
TurnLeft(){
33+
this._ship.SetRudderAngleTo(-30);
34+
this._ship.SetSailAngle(3, 12);
35+
}
36+
TurnRight(){
37+
38+
this._ship.SetRudderAngleTo(30);
39+
this._ship.SetSailAngle(5, -9);
40+
}
41+
GoForward(){
42+
//do something else to the _ship
43+
}
44+
}
45+
46+
export interface SimpleShip{
47+
TurnLeft();
48+
TurnRight();
49+
GoForward();
50+
}
51+
}

Chapter 4/Bridege.ts

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
module Westros.Religion
2+
{
3+
export class OldGods{
4+
prayTo(sacrifice){}
5+
}
6+
7+
export class DrownedGod{
8+
prayTo(humanSacrifice){}
9+
}
10+
11+
export class SevenGods{
12+
prayTo(prayerPurpose){}
13+
}
14+
15+
export interface God
16+
{
17+
prayTo():void;
18+
}
19+
20+
export class OldGodsAdapter implements God{
21+
_oldGods:OldGods;
22+
constructor(){
23+
this._oldGods = new OldGods();
24+
}
25+
prayTo(){
26+
var sacrifice= new Sacrifice();
27+
this._oldGods.prayTo(sacrifice);
28+
}
29+
}
30+
31+
export class DrownedGodAdapter implements God{
32+
_drownedGod;
33+
constructor(){
34+
this._drownedGod = new DrownedGod();
35+
}
36+
prayTo(){
37+
var sacrifice = new HumanSacrifice();
38+
this._drownedGod.prayTo(sacrifice);
39+
}
40+
}
41+
42+
export class SevenGodsAdapter implements God{
43+
_sevenGods;
44+
public prayerPurposeProvider = new PrayerPurposeProvider();
45+
constructor(){
46+
this._sevenGods = new SevenGods();
47+
}
48+
prayTo(){
49+
this._sevenGods.prayTo(this.prayerPurposeProvider.GetPurpose());
50+
}
51+
52+
}
53+
54+
export class PrayerPurposeProvider{
55+
GetPurpose(){}
56+
}
57+
export class Sacrifice{}
58+
export class HumanSacrifice{}
59+
60+
}

Chapter 4/Composite.js

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
var Westros;
2+
(function (Westros) {
3+
(function (Food) {
4+
var SimpleIngredient = (function () {
5+
function SimpleIngredient(name, calories, ironContent, vitaminCContent) {
6+
this.name = name;
7+
this.calories = calories;
8+
this.ironContent = ironContent;
9+
this.vitaminCContent = vitaminCContent;
10+
}
11+
SimpleIngredient.prototype.GetName = function () {
12+
return this.name;
13+
};
14+
SimpleIngredient.prototype.GetCalories = function () {
15+
return this.calories;
16+
};
17+
SimpleIngredient.prototype.GetIronContent = function () {
18+
return this.ironContent;
19+
};
20+
SimpleIngredient.prototype.GetVitaminCContent = function () {
21+
return this.vitaminCContent;
22+
};
23+
return SimpleIngredient;
24+
})();
25+
Food.SimpleIngredient = SimpleIngredient;
26+
27+
var CompoundIngredient = (function () {
28+
function CompoundIngredient(name) {
29+
this.name = name;
30+
this.ingredients = new Array();
31+
}
32+
CompoundIngredient.prototype.AddIngredient = function (ingredient) {
33+
this.ingredients.push(ingredient);
34+
};
35+
36+
CompoundIngredient.prototype.GetName = function () {
37+
return this.name;
38+
};
39+
CompoundIngredient.prototype.GetCalories = function () {
40+
var total = 0;
41+
for (var i = 0; i < this.ingredients.length; i++) {
42+
total += this.ingredients[i].GetCalories();
43+
}
44+
return total;
45+
};
46+
CompoundIngredient.prototype.GetIronContent = function () {
47+
var total = 0;
48+
for (var i = 0; i < this.ingredients.length; i++) {
49+
total += this.ingredients[i].GetIronContent();
50+
}
51+
return total;
52+
};
53+
CompoundIngredient.prototype.GetVitaminCContent = function () {
54+
var total = 0;
55+
for (var i = 0; i < this.ingredients.length; i++) {
56+
total += this.ingredients[i].GetVitaminCContent();
57+
}
58+
return total;
59+
};
60+
return CompoundIngredient;
61+
})();
62+
Food.CompoundIngredient = CompoundIngredient;
63+
})(Westros.Food || (Westros.Food = {}));
64+
var Food = Westros.Food;
65+
})(Westros || (Westros = {}));

Chapter 4/Composite.ts

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
module Westros.Food{
2+
export interface IIngredient{
3+
GetName(): string;
4+
GetCalories():number;
5+
GetIronContent():number;
6+
GetVitaminCContent():number;
7+
}
8+
9+
export class SimpleIngredient{
10+
11+
constructor(public name, public calories, public ironContent, public vitaminCContent){}
12+
GetName(): string
13+
{
14+
return this.name;
15+
}
16+
GetCalories():number{
17+
return this.calories;
18+
}
19+
GetIronContent():number{
20+
return this.ironContent;
21+
}
22+
GetVitaminCContent():number
23+
{
24+
return this.vitaminCContent;
25+
}
26+
}
27+
28+
export class CompoundIngredient{
29+
30+
ingredients:IIngredient[] = new Array();
31+
constructor(public name){
32+
33+
}
34+
35+
AddIngredient(ingredient:IIngredient)
36+
{
37+
this.ingredients.push(ingredient);
38+
}
39+
40+
GetName(): string{
41+
return this.name;
42+
}
43+
GetCalories():number{
44+
var total = 0;
45+
for(var i = 0; i< this.ingredients.length; i++)
46+
{
47+
total+=this.ingredients[i].GetCalories();
48+
}
49+
return total;
50+
}
51+
GetIronContent():number{
52+
var total = 0;
53+
for(var i = 0; i< this.ingredients.length; i++)
54+
{
55+
total+=this.ingredients[i].GetIronContent();
56+
}
57+
return total;
58+
}
59+
GetVitaminCContent():number{
60+
var total = 0;
61+
for(var i = 0; i< this.ingredients.length; i++)
62+
{
63+
total+=this.ingredients[i].GetVitaminCContent();
64+
}
65+
return total;
66+
}
67+
}
68+
69+
70+
}

Chapter 4/Decorator.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
var Westros;
2+
(function (Westros) {
3+
(function (Armor) {
4+
var BasicArmor = (function () {
5+
function BasicArmor() {
6+
}
7+
BasicArmor.prototype.CalculateDamageFromHit = function (hit) {
8+
return 1;
9+
};
10+
BasicArmor.prototype.GetArmorIntegrity = function () {
11+
return 1;
12+
};
13+
return BasicArmor;
14+
})();
15+
Armor.BasicArmor = BasicArmor;
16+
17+
var ChainMail = (function () {
18+
function ChainMail(decoratedArmor) {
19+
this.decoratedArmor = decoratedArmor;
20+
}
21+
ChainMail.prototype.CalculateDamageFromHit = function (hit) {
22+
hit.Strength = hit.Strength * .8;
23+
return this.decoratedArmor.CalculateDamageFromHit(hit);
24+
};
25+
ChainMail.prototype.GetArmorIntegrity = function () {
26+
return .9 * this.decoratedArmor.GetArmorIntegrity();
27+
};
28+
return ChainMail;
29+
})();
30+
Armor.ChainMail = ChainMail;
31+
32+
var Hit = (function () {
33+
function Hit() {
34+
}
35+
return Hit;
36+
})();
37+
Armor.Hit = Hit;
38+
})(Westros.Armor || (Westros.Armor = {}));
39+
var Armor = Westros.Armor;
40+
})(Westros || (Westros = {}));

0 commit comments

Comments
 (0)