-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.ts
158 lines (137 loc) · 2.93 KB
/
index.ts
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
/**
* The Facade Pattern
*
* The Facade Pattern provides a unified interface to a set of interfaces in a
* subsystem. Facade defines a higher level interface that makes the subsystem
* easier to use.
*/
class BadCoffee {
prepareRecipe() {
this.boilWater();
this.brewCoffeeGrind();
this.pourInCup();
this.addSugarAndMilk();
}
boilWater() {}
brewCoffeeGrind() {}
pourInCup() {}
addSugarAndMilk() {}
}
class BadTea {
prepareRecipe() {
this.boilWater();
this.steepTheTeabag();
this.pourInCup();
this.addLemon();
}
boilWater() {}
steepTheTeabag() {}
pourInCup() {}
addLemon() {}
}
abstract class CaffeineBeverage {
/**
* This is what's known as a template method
*/
prepareRecipe() {
this.boilWater();
this.brew();
this.pourInCup();
this.addCondiments();
}
boilWater() {
console.log('Boil! That! Water!');
}
pourInCup() {
console.log('In! The! Cup!');
}
abstract brew: () => void;
abstract addCondiments: () => void;
}
class Coffee extends CaffeineBeverage {
brew = () => {
console.log('In a pot!');
};
addCondiments = () => {
console.log('Add those condiments!');
};
}
class Tea extends CaffeineBeverage {
brew = () => {
console.log('In a kettle?!');
};
addCondiments = () => {
console.log('Add those condiments! But tea.');
};
}
abstract class CaffeineBeverageWithHook {
/**
* This is what's known as a template method
*/
prepareRecipe() {
this.boilWater();
this.brew();
this.pourInCup();
if (this.customerWantsCondiments()) {
this.addCondiments();
}
}
boilWater() {
console.log('Boil! That! Water!');
}
pourInCup() {
console.log('In! The! Cup!');
}
customerWantsCondiments() {
return true;
}
abstract brew: () => void;
abstract addCondiments: () => void;
}
class CoffeeWithHook extends CaffeineBeverageWithHook {
brew = () => {
console.log('In a pot!');
};
addCondiments = () => {
console.log('Add those condiments!');
};
/*
* We are just manually overwriting the call here, for getting the user input
* wrapping the readline call in a promise will let you write a nice async
* function TODO
*/
customerWantsCondiments() {
return false;
}
}
function exampleOne() {
const coffee = new Coffee();
const hookedCoffee = new CoffeeWithHook();
coffee.prepareRecipe();
hookedCoffee.prepareRecipe();
}
/**
* The text makes reference to Java's approach to sorting arrays.
*
* Each element implements a `compateTo` function. I'll mock up something
* similar here
*/
interface IValued {
value: number;
}
interface IComparable<T extends IValued> {
compareTo: (e: T) => number;
}
interface IValueNom {
value: number;
}
class Clown implements IComparable<IValueNom> {
value: number;
constructor(v: number) {
this.value = v;
}
compareTo(e: IValueNom) {
return this.value - e.value;
}
}
export default {exampleOne};