Skip to content

Commit c8b07d2

Browse files
author
Badacadabra
committed
Add all creational patterns in TypeScript
1 parent 08a2e69 commit c8b07d2

File tree

5 files changed

+506
-0
lines changed

5 files changed

+506
-0
lines changed
Lines changed: 224 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,224 @@
1+
// ==============================
2+
// ABSTRACT GNU/LINUX DISTRO
3+
// ==============================
4+
5+
abstract class LinuxDistro {
6+
protected name: string;
7+
8+
constructor(name: string) {
9+
this.name = name;
10+
}
11+
12+
public bootLinux(): string {
13+
return `${this.name} is booting...`;
14+
}
15+
}
16+
17+
// ==============================
18+
// ABSTRACT MAC OS RELEASE
19+
// ==============================
20+
21+
class MacRelease {
22+
protected name: string;
23+
24+
constructor(name: string) {
25+
this.name = name;
26+
}
27+
28+
public bootMac(): string {
29+
return `${this.name} is booting...`;
30+
}
31+
}
32+
33+
// ==============================
34+
// ABSTRACT WINDOWS VERSION
35+
// ==============================
36+
37+
class WindowsVersion {
38+
protected name: string;
39+
40+
constructor(name: string) {
41+
this.name = name;
42+
}
43+
44+
public bootWindows(): string {
45+
return `${this.name} is booting...`;
46+
}
47+
}
48+
49+
// ==============================
50+
// CONCRETE GNU/LINUX DISTROS
51+
// ==============================
52+
53+
class Debian extends LinuxDistro {
54+
constructor() {
55+
super("Debian");
56+
}
57+
}
58+
59+
class RedHat extends LinuxDistro {
60+
constructor() {
61+
super("RedHat");
62+
}
63+
}
64+
65+
// ==============================
66+
// CONCRETE MAC OS RELEASES
67+
// ==============================
68+
69+
class OS9 extends MacRelease {
70+
constructor() {
71+
super("Mac OS 9");
72+
}
73+
}
74+
75+
class OSX extends MacRelease {
76+
constructor() {
77+
super("Mac OS X");
78+
}
79+
}
80+
81+
// ==============================
82+
// CONCRETE WINDOWS VERSIONS
83+
// ==============================
84+
85+
class XP extends WindowsVersion {
86+
constructor() {
87+
super("Windows XP");
88+
}
89+
}
90+
91+
class Vista extends WindowsVersion {
92+
constructor() {
93+
super("Windows Vista");
94+
}
95+
}
96+
97+
// ==============================
98+
// ABSTRACT FACTORY OF OPERATING SYSTEMS
99+
// ==============================
100+
101+
abstract class OSFactory {
102+
public static readonly LINUX: number = 0;
103+
public static readonly MAC: number = 1;
104+
public static readonly WINDOWS: number = 2;
105+
106+
public static getOSFactory(id: number): OSFactory {
107+
switch (id) {
108+
case this.LINUX:
109+
return new LinuxFactory();
110+
case this.MAC:
111+
return new MacFactory();
112+
case this.WINDOWS:
113+
return new WindowsFactory();
114+
default:
115+
throw new Error("The factory you are looking for has not been found");
116+
}
117+
}
118+
119+
public abstract getLinuxDistro(id: number): LinuxDistro | never;
120+
public abstract getMacRelease(id: number): MacRelease | never;
121+
public abstract getWindowsVersion(id: number): WindowsVersion | never;
122+
}
123+
124+
// ==============================
125+
// CONCRETE GNU/LINUX FACTORY
126+
// ==============================
127+
128+
class LinuxFactory extends OSFactory {
129+
public static readonly DEBIAN: number = 0;
130+
public static readonly REDHAT: number = 1;
131+
132+
public getLinuxDistro(id: number): LinuxDistro {
133+
switch (id) {
134+
case LinuxFactory.DEBIAN:
135+
return new Debian();
136+
case LinuxFactory.REDHAT:
137+
return new RedHat();
138+
default:
139+
throw new Error("The Linux distribution you are looking for has not been found");
140+
}
141+
}
142+
143+
public getMacRelease(id: number): never {
144+
throw new Error("If you want a Mac release, use the Mac factory.");
145+
}
146+
147+
public getWindowsVersion(id: number): never {
148+
throw new Error("If you want a Windows version, use the Windows factory.");
149+
}
150+
}
151+
152+
// ==============================
153+
// CONCRETE MAC OS FACTORY
154+
// ==============================
155+
156+
class MacFactory extends OSFactory {
157+
public static readonly OS9: number = 0;
158+
public static readonly OSX: number = 1;
159+
160+
public getMacRelease(id: number): MacRelease {
161+
switch (id) {
162+
case MacFactory.OS9:
163+
return new OS9();
164+
case MacFactory.OSX:
165+
return new OSX();
166+
default:
167+
throw new Error("The Mac release you are looking for has not been found");
168+
}
169+
}
170+
171+
public getLinuxDistro(id: number): never {
172+
throw new Error("If you want a Linux distribution, use the Linux factory.");
173+
}
174+
175+
public getWindowsVersion(id: number): never {
176+
throw new Error("If you want a Windows version, use the Windows factory.");
177+
}
178+
}
179+
180+
// ==============================
181+
// CONCRETE WINDOWS FACTORY
182+
// ==============================
183+
184+
class WindowsFactory extends OSFactory {
185+
public static readonly XP: number = 0;
186+
public static readonly VISTA: number = 1;
187+
188+
public getWindowsVersion(id: number): WindowsVersion {
189+
switch (id) {
190+
case WindowsFactory.XP:
191+
return new XP();
192+
case WindowsFactory.VISTA:
193+
return new Vista();
194+
default:
195+
throw new Error("The Windows version you are looking for has not been found");
196+
}
197+
}
198+
199+
public getLinuxDistro(id: number): never {
200+
throw new Error("If you want a Linux distribution, use the Linux factory.");
201+
}
202+
203+
public getMacRelease(id: number): never {
204+
throw new Error("If you want a Mac release, use the Mac factory.");
205+
}
206+
}
207+
208+
// ==============================
209+
// CLIENT CODE
210+
// ==============================
211+
212+
// We can get a concrete factory from the abstract factory
213+
let linuxFactory: OSFactory = OSFactory.getOSFactory(OSFactory.LINUX),
214+
macFactory: OSFactory = OSFactory.getOSFactory(OSFactory.MAC),
215+
windowsFactory: OSFactory = OSFactory.getOSFactory(OSFactory.WINDOWS);
216+
217+
// Then we can get real objects from these concrete factories
218+
let debian: LinuxDistro = linuxFactory.getLinuxDistro(LinuxFactory.DEBIAN),
219+
osx: MacRelease = macFactory.getMacRelease(MacFactory.OSX),
220+
xp: WindowsVersion = windowsFactory.getWindowsVersion(WindowsFactory.XP);
221+
222+
console.log(debian.bootLinux()); // Debian is booting...
223+
console.log(osx.bootMac()); // Mac OS X is booting...
224+
console.log(xp.bootWindows()); // Windows XP is booting...
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
// ==============================
2+
// PC (PRODUCT)
3+
// ==============================
4+
5+
class PC {
6+
private motherboard: string;
7+
private cpu: string;
8+
private ram: string;
9+
private ssd: string;
10+
private nic: string;
11+
private powerSupply: string;
12+
private caseDesign: string;
13+
14+
public setMotherboard(motherboard): void {
15+
this.motherboard = motherboard;
16+
}
17+
18+
public setCpu(cpu): void {
19+
this.cpu = cpu;
20+
}
21+
22+
public setRam(ram): void {
23+
this.ram = ram;
24+
}
25+
26+
public setSsd(ssd): void {
27+
this.ssd = ssd;
28+
}
29+
30+
public setNic(nic): void {
31+
this.nic = nic;
32+
}
33+
34+
public setPowerSupply(powerSupply): void {
35+
this.powerSupply = powerSupply;
36+
}
37+
38+
public setCaseDesign(caseDesign): void {
39+
this.caseDesign = caseDesign;
40+
}
41+
42+
public toString(): string {
43+
return `Motherboard: ${this.motherboard}
44+
CPU: ${this.cpu}
45+
RAM: ${this.ram}
46+
SSD: ${this.ssd}
47+
NIC: ${this.nic}
48+
Power Supply: ${this.powerSupply}
49+
Case design: ${this.caseDesign}`
50+
}
51+
}
52+
53+
// ==============================
54+
// ABSTRACT PC BUILDER
55+
// ==============================
56+
57+
interface Workforce {
58+
assemblePC(): string;
59+
setMotherboard(motherboard: string): void;
60+
setCpu(cpu: string): void;
61+
setRam(ram: string): void;
62+
setSsd(ssd: string): void;
63+
setNic(nic: string): void;
64+
setPowerSupply(powerSupply: string): void;
65+
setCaseDesign(caseDesign: string): void;
66+
}
67+
68+
// ==============================
69+
// CONCRETE PC BUILDER
70+
// ==============================
71+
72+
class Geek implements Workforce {
73+
private pc: PC = new PC();
74+
75+
public assemblePC(): string {
76+
return this.pc.toString();
77+
}
78+
79+
public setMotherboard(motherboard: string): void {
80+
this.pc.setMotherboard(motherboard);
81+
}
82+
83+
public setCpu(cpu: string): void {
84+
this.pc.setCpu(cpu);
85+
}
86+
87+
public setRam(ram: string): void {
88+
this.pc.setRam(ram);
89+
}
90+
91+
public setSsd(ssd: string): void {
92+
this.pc.setSsd(ssd);
93+
}
94+
95+
public setNic(nic: string): void {
96+
this.pc.setNic(nic);
97+
}
98+
99+
public setPowerSupply(powerSupply: string): void {
100+
this.pc.setPowerSupply(powerSupply);
101+
}
102+
103+
public setCaseDesign(caseDesign: string): void {
104+
this.pc.setCaseDesign(caseDesign);
105+
}
106+
}
107+
108+
// ==============================
109+
// MANUFACTURER (DIRECTOR)
110+
// ==============================
111+
112+
class Manufacturer {
113+
public static manufacture(builder: Workforce): string {
114+
builder.setMotherboard("Asus Z170-A ATX LGA1151");
115+
builder.setCpu("Intel Core i7 6950X");
116+
builder.setRam("HyperX Fury 8 GB");
117+
builder.setSsd("SanDisk SSD PLUS 240 GB");
118+
builder.setNic("D-Link DGE-528T");
119+
builder.setPowerSupply("Corsair RM750x");
120+
builder.setCaseDesign("Cooler Master HAF X");
121+
return builder.assemblePC();
122+
}
123+
}
124+
125+
// ==============================
126+
// CLIENT CODE
127+
// ==============================
128+
129+
let geek: Geek = new Geek(),
130+
pc: string = Manufacturer.manufacture(geek);
131+
132+
console.log(pc);

0 commit comments

Comments
 (0)