-
Notifications
You must be signed in to change notification settings - Fork 33
/
abstractFactory.ats
78 lines (63 loc) · 1.5 KB
/
abstractFactory.ats
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
import {Logger} from '../logger';
export class PetShop {
constructor(petFactory:AbstractFactory) {
this.petFactory = petFactory;
this.logger = new Logger();
}
showPet() {
var pet = this.petFactory.getPet();
this.logger.log("We have a lovely " + pet.toString());
this.logger.log("It says " + pet.speak());
this.logger.log("We also have " + this.petFactory.getFood());
}
}
/* Stuff that our factory makes */
export class Animal {
speak():string {
throw new Error("Abstract method!");
}
toString():string {
throw new Error("Abstract method!");
}
}
export class Dog extends Animal {
speak():string {
return "woof";
}
toString():string {
return "Dog";
}
}
export class Cat extends Animal {
speak():string {
return "meow";
}
toString():string {
return "Cat";
}
}
/* Factory classes */
export class AbstractFactory {
getPet():Animal {
throw new Error("Abstract method!");
}
getFood():string {
throw new Error("Abstract method!");
}
}
export class DogFactory extends AbstractFactory {
getPet():Animal {
return new Dog();
}
getFood():string {
return "dog food"
}
}
export class CatFactory extends AbstractFactory {
getPet():Animal {
return new Cat();
}
getFood():string {
return "cat food"
}
}