|
| 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... |
0 commit comments