Skip to content

Commit

Permalink
#29 - TypeScript
Browse files Browse the repository at this point in the history
  • Loading branch information
Sac-Corts committed Sep 25, 2024
1 parent 920c965 commit c80a1ea
Showing 1 changed file with 157 additions and 0 deletions.
157 changes: 157 additions & 0 deletions Roadmap/29 - SOLID ISP/typescript/Sac-Corts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
// Wrong way
class Device {
turnOn(): void {}
turnOff(): void {}
charge(): void {}
connectToInternet(): void {}
}

class Laptop extends Device {
turnOn(): void {
console.log("Laptop turned on");
}

turnOff(): void {
console.log("Laptop turned off");
}

charge(): void {
console.log("Laptop charging");
}

connectToInternet(): void {
console.log("Laptop connected to the internet");
}
}

class Microwave extends Device {
turnOn(): void {
console.log("Microwave turned on");
}

turnOff(): void {
console.log("Microwave turned off");
}

charge(): void {
throw new Error("A microwave doesn't need to be charged");
}

connectToInternet(): void {
throw new Error("A microwave doesn't need to be connected to the internet");
}
}

const microwave = new Microwave();
// microwave.charge(); // Error

// Correct way
class Switchable {
turnOn(): void {}
turnOff(): void {}
}

class Chargeable {
charge(): void {}
}

class InternetConnectable {
connectToInternet(): void {}
}

class Laptop2 extends Switchable {
turnOn(): void {
console.log("Laptop turned on");
}

turnOff(): void {
console.log("Laptop turned off");
}
}

class AdvancedLaptop extends Laptop2 implements Chargeable, InternetConnectable {
charge(): void {
console.log("Laptop charging.");
}

connectToInternet(): void {
console.log("Laptop connected to the internet.");
}
}

class Microwave2 extends Switchable {
turnOn(): void {
console.log("Microwave turned on");
}

turnOff(): void {
console.log("Microwave turned off");
}
}

const laptop2 = new AdvancedLaptop();
laptop2.charge();
laptop2.connectToInternet();

const microwave2 = new Microwave2();
microwave2.turnOn();
microwave2.turnOff();

// Extra Exercise //
class BlackAndWhitePrintable {
printBlackAndWhite(): void {}
}

class ColorPrintable {
printColor(): void {}
}

class Scannable {
scan(): void {}
}

class Faxable {
sendFax(): void {}
}

class BlackAndWhitePrinter extends BlackAndWhitePrintable {
printBlackAndWhite(): void {
console.log("Printing in black and white...");
}
}

class ColorPrinter extends ColorPrintable {
printColor(): void {
console.log("Printing in color...");
}
}

class MultiFunctionPrinter extends BlackAndWhitePrintable implements ColorPrintable, Scannable, Faxable {
printBlackAndWhite(): void {
console.log("Printing in black and white...");
}

printColor(): void {
console.log("Printing in color...");
}

scan(): void {
console.log("Scanning...");
}

sendFax(): void {
console.log("Sending fax...");
}
}

const blackAndWhitePrinter = new BlackAndWhitePrinter();
blackAndWhitePrinter.printBlackAndWhite();

const colorPrinter = new ColorPrinter();
colorPrinter.printColor();

const multiFunctionPrinter = new MultiFunctionPrinter();
multiFunctionPrinter.printBlackAndWhite();
multiFunctionPrinter.printColor();
multiFunctionPrinter.scan();
multiFunctionPrinter.sendFax();

0 comments on commit c80a1ea

Please sign in to comment.