Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update the solid principle example #24

Merged
merged 1 commit into from
Jan 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file removed src/solid/dip/assets/dip.png
Binary file not shown.
21 changes: 0 additions & 21 deletions src/solid/dip/bad.ts

This file was deleted.

58 changes: 0 additions & 58 deletions src/solid/dip/good.ts

This file was deleted.

79 changes: 79 additions & 0 deletions src/solid/dip/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/**
* Dependency Inversion Principle:
* 1. High-level modules should not depend on low-level modules.
* Both should depend on interfaces or abstract classes (abstractions).
* 2. Interfaces or abstract classes (abstractions) should not depend on concrete implementations (details).
* Concrete implementations should depend on interfaces or abstract classes.
*/

// Bad: (Violating DIP - Tightly Coupled)
class CarBad {
private petrolEngine: PetrolEngine; // Directly depends on PetrolEngine

constructor() {
this.petrolEngine = new PetrolEngine();
}

start(): void {
this.petrolEngine.start();
}
}

class PetrolEngine {
start(): void {
console.log("Petrol Engine Started")
}
}

const myCarBad = new CarBad()
myCarBad.start()

/**
* Problems:
* 1. If we want to create a DieselCar, we have to create a new class (DieselCar) and duplicate a lot of code.
* 2. The CarBad class is tightly coupled to PetrolEngine, making it difficult to test and maintain.
*/

// Good: (Adhering to DIP - Loosely Coupled)
interface Engine {
start(): void;
}

class PetrolEngineGood implements Engine {
start(): void {
console.log("Petrol Engine Started");
}
}

class DieselEngineGood implements Engine {
start(): void {
console.log("Diesel Engine Started");
}
}

class ElectricEngineGood implements Engine {
start(): void {
console.log("Electric Engine Started")
}
}

class CarGood {
private engine: Engine; // Depends on the abstraction (Engine interface)

constructor(engine: Engine) {
this.engine = engine;
}

start(): void {
this.engine.start();
}
}

const myPetrolCarGood = new CarGood(new PetrolEngineGood());
myPetrolCarGood.start();

const myDieselCarGood = new CarGood(new DieselEngineGood());
myDieselCarGood.start();

const myElectricCarGood = new CarGood(new ElectricEngineGood())
myElectricCarGood.start()
Binary file modified src/solid/isp/.DS_Store
Binary file not shown.
Binary file removed src/solid/isp/assets/isp.png
Binary file not shown.
41 changes: 0 additions & 41 deletions src/solid/isp/bad.ts

This file was deleted.

51 changes: 0 additions & 51 deletions src/solid/isp/good.ts

This file was deleted.

50 changes: 50 additions & 0 deletions src/solid/isp/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/**
* Interface Segregation Principle (ISP)
*
* Tt states that clients should not be forced to depend on methods they do not use.
* In other words, large interfaces should be split into smaller,
* more specific interfaces so that clients only need to know about the methods that are relevant to them.
*/

// Bad: Vehicle interface forces unrelated methods on some implementations
interface VehicleActions {
drive(): void;
fly(): void;
sail(): void;
}

class Car implements VehicleActions {
drive() { }
fly(): void {
throw new Error("Cars cannot fly.");
}
sail(): void {
throw new Error("Cars cannot sail.");
}
}

// Good: Separate interfaces for specific actions
interface Drivable {
drive(): void;
}

interface Flyable {
fly(): void;
}

interface Sailable {
sail(): void;
}

class CarISP implements Drivable {
drive() { }
}

class AirplaneISP implements Flyable, Drivable {
fly() { }
drive() { } // Can taxi on the ground
}

class BoatISP implements Sailable {
sail() { }
}
18 changes: 0 additions & 18 deletions src/solid/isp/readme.md

This file was deleted.

Binary file modified src/solid/lsp/.DS_Store
Binary file not shown.
Binary file removed src/solid/lsp/assets/lsp.png
Binary file not shown.
43 changes: 0 additions & 43 deletions src/solid/lsp/bad.ts

This file was deleted.

Loading
Loading