-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathFlyweight.ts
66 lines (44 loc) · 1.2 KB
/
Flyweight.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
export class CloudFactory {
private static cloudTypes: CloudType[] = []
static getCloudType(color: string) : CloudType {
let found = this.cloudTypes.find((cloud: CloudType) => cloud.color === color)
if(!found || found === null){
found = new CloudType(color)
this.cloudTypes.push(found)
}
return found
}
static getInstances(): number {
return this.cloudTypes.length
}
}
export class CloudType {
color: string
constructor(_color: string) {
this.color = _color
}
}
export class Cloud {
x: number
y: number
type: CloudType
constructor(_x: number, _y: number, _type: CloudType) {
this.x = _x
this.y = _y
this.type = _type
}
rain(): void {
console.log(`Rain falls from ${this.type.color} cloud`)
}
}
export class Sky {
clouds: Cloud[] = []
addCloud(x: number, y: number, color: string): void {
let type = CloudFactory.getCloudType(color)
let cloud = new Cloud(x, y, type)
this.clouds.push(cloud)
}
rain(): void {
this.clouds.forEach((cloud: Cloud) => cloud.rain())
}
}