-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathLazyInstantiation.ts
49 lines (42 loc) · 1.14 KB
/
LazyInstantiation.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
module Westeros.FoodSuppliers{
export class Bakery{
breads: Bread[];
requiredBreads: string[];
constructor() {
this.requiredBreads = [];
}
public orderBreadType(breadType: string){
this.requiredBreads.push(breadType);
}
public pickUpBread(breadType: string):Bread{
console.log("Picup of bread " + breadType + " requested")
if(!this.breads)
{
this.createBreads();
}
for(var i=0; i<this.breads.length; i++){
if(this.breads[i].breadType == breadType)
return this.breads[i];
}
}
createBreads(){
this.breads = [];
for(var i =0; i<this.requiredBreads.length; i++)
{
this.breads.push(new Bread(this.requiredBreads[i]));
}
}
}
export class Bread{
constructor(public breadType: string){
//some complex, time consuming operation
console.log("Bread " + breadType + " created.");
}
}
}
var bakery = new Westeros.FoodSuppliers.Bakery();
bakery.orderBreadType("Brioche");
bakery.orderBreadType("Anadama bread");
bakery.orderBreadType("Chapati");
bakery.orderBreadType("Focaccia");
console.log(bakery.pickUpBread("Brioche").breadType);