|
| 1 | +/* |
| 2 | + Interface Segregation Principle (ISP)... |
| 3 | +*/ |
| 4 | + |
| 5 | +console.log('Interface Segregation Principle (ISP)...') |
| 6 | + |
| 7 | +interface IBadAnimal { |
| 8 | + fly: () => void |
| 9 | + run: () => void |
| 10 | + swim: () => void |
| 11 | +} |
| 12 | + |
| 13 | +class BadDog implements IBadAnimal { |
| 14 | + fly(): void { |
| 15 | + throw new Error('A dog can not fly') |
| 16 | + } |
| 17 | + |
| 18 | + run(): void { |
| 19 | + console.log('The dog is running!') |
| 20 | + } |
| 21 | + |
| 22 | + swim(): void { |
| 23 | + console.log('The dog is swimming!') |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +class BadDuck implements IBadAnimal { |
| 28 | + fly(): void { |
| 29 | + console.log('The duck is flying!') |
| 30 | + } |
| 31 | + |
| 32 | + run(): void { |
| 33 | + console.log('The duck is running!') |
| 34 | + } |
| 35 | + |
| 36 | + swim(): void { |
| 37 | + console.log('The duck is swimming!') |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +console.log('\nBad implementation of Interface Segregation Principle (ISP)...') |
| 42 | + |
| 43 | +console.log(`\n\`\`\`\ninterface IBadAnimal { |
| 44 | + fly: () => void |
| 45 | + run: () => void |
| 46 | + swim: () => void |
| 47 | +} |
| 48 | +
|
| 49 | +class BadDog implements IBadAnimal { |
| 50 | + fly(): void { |
| 51 | + throw new Error('A dog can not fly') |
| 52 | + } |
| 53 | +
|
| 54 | + run(): void { |
| 55 | + console.log('The dog is running!') |
| 56 | + } |
| 57 | +
|
| 58 | + swim(): void { |
| 59 | + console.log('The dog is swimming!') |
| 60 | + } |
| 61 | +} |
| 62 | +
|
| 63 | +class BadDuck implements IBadAnimal { |
| 64 | + fly(): void { |
| 65 | + console.log('The duck is flying!') |
| 66 | + } |
| 67 | +
|
| 68 | + run(): void { |
| 69 | + console.log('The duck is running!') |
| 70 | + } |
| 71 | +
|
| 72 | + swim(): void { |
| 73 | + console.log('The duck is swimming!') |
| 74 | + } |
| 75 | +}\n\`\`\``) |
| 76 | + |
| 77 | +console.log( |
| 78 | + '\nThis is a bad implementation of Interface Segregation Principle (ISP),\n' + |
| 79 | + 'because the "BadDog" class should not implement an interface with methods\n' + |
| 80 | + 'that it is going to never use. So, the implemented interface ("IBadAnimal")\n' + |
| 81 | + 'is to general for the "BadDog" class, but perfect for the "BadDuck" class.' |
| 82 | +) |
| 83 | + |
| 84 | +console.log('\nGood implementation of Interface Segregation Principle (ISP)...') |
| 85 | + |
| 86 | +interface IAerialAnimal { |
| 87 | + fly: () => void |
| 88 | +} |
| 89 | + |
| 90 | +interface IAquaticAnimal { |
| 91 | + swim: () => void |
| 92 | +} |
| 93 | + |
| 94 | +interface ITerrestrialAnimal { |
| 95 | + run: () => void |
| 96 | +} |
| 97 | + |
| 98 | +class GoodDog implements ITerrestrialAnimal, IAquaticAnimal { |
| 99 | + run(): void { |
| 100 | + console.log('The dog is running!') |
| 101 | + } |
| 102 | + |
| 103 | + swim(): void { |
| 104 | + console.log('The dog is swimming!') |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +class GoodDuck implements IAerialAnimal, IAquaticAnimal, ITerrestrialAnimal { |
| 109 | + fly(): void { |
| 110 | + console.log('The duck is flying!') |
| 111 | + } |
| 112 | + |
| 113 | + run(): void { |
| 114 | + console.log('The duck is running!') |
| 115 | + } |
| 116 | + |
| 117 | + swim(): void { |
| 118 | + console.log('The duck is swimming!') |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +console.log(`\n\`\`\`\ninterface IAerialAnimal { |
| 123 | + fly: () => void |
| 124 | +} |
| 125 | +
|
| 126 | +interface IAquaticAnimal { |
| 127 | + swim: () => void |
| 128 | +} |
| 129 | +
|
| 130 | +interface ITerrestrialAnimal { |
| 131 | + run: () => void |
| 132 | +} |
| 133 | +
|
| 134 | +class GoodDog implements ITerrestrialAnimal, IAquaticAnimal { |
| 135 | + run(): void { |
| 136 | + console.log('The dog is running!') |
| 137 | + } |
| 138 | +
|
| 139 | + swim(): void { |
| 140 | + console.log('The dog is swimming!') |
| 141 | + } |
| 142 | +} |
| 143 | +
|
| 144 | +class GoodDuck implements IAerialAnimal, IAquaticAnimal, ITerrestrialAnimal { |
| 145 | + fly(): void { |
| 146 | + console.log('The duck is flying!') |
| 147 | + } |
| 148 | +
|
| 149 | + run(): void { |
| 150 | + console.log('The duck is running!') |
| 151 | + } |
| 152 | +
|
| 153 | + swim(): void { |
| 154 | + console.log('The duck is swimming!') |
| 155 | + } |
| 156 | +}\n\`\`\``) |
| 157 | + |
| 158 | +console.log( |
| 159 | + '\nThis is a good implementation of Interface Segregation Principle (ISP),\n' + |
| 160 | + 'because the "GoodDog" class only implements the necessary methods, without\n' + |
| 161 | + 'any extras. Also, the "GoodDuck" class utilizes an interface perfectly suited\n' + |
| 162 | + 'to its needs, with no extra methods.' |
| 163 | +) |
| 164 | + |
| 165 | +console.log( |
| 166 | + '\n# ---------------------------------------------------------------------------------- #\n' |
| 167 | +) |
| 168 | + |
| 169 | +/* |
| 170 | + Additional challenge... |
| 171 | +*/ |
| 172 | + |
| 173 | +console.log('Additional challenge...') |
| 174 | + |
| 175 | +interface IBlackAndWhitePrinter { |
| 176 | + print: () => void |
| 177 | +} |
| 178 | + |
| 179 | +class BlackAndWhitePrinter implements IBlackAndWhitePrinter { |
| 180 | + constructor() {} |
| 181 | + |
| 182 | + print(): void { |
| 183 | + console.log('Paper printed in black and white!') |
| 184 | + } |
| 185 | +} |
| 186 | + |
| 187 | +interface IColorPrinter { |
| 188 | + print: () => void |
| 189 | +} |
| 190 | + |
| 191 | +class ColorPrinter implements IColorPrinter { |
| 192 | + constructor() {} |
| 193 | + |
| 194 | + print(): void { |
| 195 | + console.log('Paper printed in color!') |
| 196 | + } |
| 197 | +} |
| 198 | + |
| 199 | +interface IMultifunctionalPrinter extends IBlackAndWhitePrinter, IColorPrinter { |
| 200 | + scan: () => void |
| 201 | + sendFax: () => void |
| 202 | +} |
| 203 | + |
| 204 | +class MultifunctionalPrinter implements IMultifunctionalPrinter { |
| 205 | + constructor() {} |
| 206 | + |
| 207 | + print(): void { |
| 208 | + console.log('Paper printed!') |
| 209 | + } |
| 210 | + |
| 211 | + scan(): void { |
| 212 | + console.log('Scan completed!') |
| 213 | + } |
| 214 | + |
| 215 | + sendFax(): void { |
| 216 | + console.log('Fax sent!') |
| 217 | + } |
| 218 | +} |
| 219 | + |
| 220 | +const blackAndWhitePrinter: BlackAndWhitePrinter = new BlackAndWhitePrinter() |
| 221 | +const colorPrinter: ColorPrinter = new ColorPrinter() |
| 222 | +const multifunctionalPrinter: MultifunctionalPrinter = |
| 223 | + new MultifunctionalPrinter() |
| 224 | + |
| 225 | +console.log() |
| 226 | +blackAndWhitePrinter.print() |
| 227 | + |
| 228 | +console.log() |
| 229 | +colorPrinter.print() |
| 230 | + |
| 231 | +console.log() |
| 232 | +multifunctionalPrinter.print() |
| 233 | + |
| 234 | +console.log() |
| 235 | +multifunctionalPrinter.scan() |
| 236 | + |
| 237 | +console.log() |
| 238 | +multifunctionalPrinter.sendFax() |
0 commit comments