From d8340cbd07f52fa05bec515fdad790f5150dfbd4 Mon Sep 17 00:00:00 2001 From: YuukanOO Date: Thu, 3 Mar 2022 16:52:04 +0100 Subject: [PATCH] =?UTF-8?q?ajout=20d=C3=A9mo=20SOLID?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- solid/dependency-inversion/after.ts | 75 +++++++++++++++++++ solid/dependency-inversion/before.ts | 64 ++++++++++++++++ solid/interface-segregation/after.ts | 68 +++++++++++++++++ solid/interface-segregation/before.ts | 59 +++++++++++++++ solid/interface-segregation/reader_writer.ts | 16 ++++ solid/liskov/after.ts | 50 +++++++++++++ solid/liskov/before.ts | 54 ++++++++++++++ solid/open-close/after.ts | 77 ++++++++++++++++++++ solid/open-close/before.ts | 48 ++++++++++++ solid/segregation/after.ts | 32 ++++++++ solid/segregation/before.ts | 27 +++++++ 11 files changed, 570 insertions(+) create mode 100644 solid/dependency-inversion/after.ts create mode 100644 solid/dependency-inversion/before.ts create mode 100644 solid/interface-segregation/after.ts create mode 100644 solid/interface-segregation/before.ts create mode 100644 solid/interface-segregation/reader_writer.ts create mode 100644 solid/liskov/after.ts create mode 100644 solid/liskov/before.ts create mode 100644 solid/open-close/after.ts create mode 100644 solid/open-close/before.ts create mode 100644 solid/segregation/after.ts create mode 100644 solid/segregation/before.ts diff --git a/solid/dependency-inversion/after.ts b/solid/dependency-inversion/after.ts new file mode 100644 index 0000000..3562e63 --- /dev/null +++ b/solid/dependency-inversion/after.ts @@ -0,0 +1,75 @@ +interface Shape { + area(): number; +} + +interface SolidShape { + volume(): number; +} + +class Circle implements Shape { + constructor(private readonly radius: number) {} + + area(): number { + return Math.PI * Math.pow(this.radius, 2); + } +} + +class Square implements Shape { + constructor(private readonly length: number) {} + + area(): number { + return Math.pow(this.length, 2); + } +} + +class Cube implements Shape, SolidShape { + area(): number { + return 0; + } + + volume(): number { + return 0; + } +} + +interface SumCalculator { + sum(): number; +} + +class AreaCalculator implements SumCalculator { + constructor(private readonly shapes: Shape[]) {} + + public sum(): number { + return this.shapes.reduce((total, shape) => total + shape.area(), 0); + } +} + +class VolumeCalculator implements SumCalculator { + constructor(private readonly shapes: SolidShape[]) {} + + public sum(): number { + return this.shapes.reduce((total, shape) => total + shape.volume(), 0); + } +} + +class SumConsoleOutputter { + constructor(private readonly calculator: SumCalculator) {} + + public output(): void { + const sum = this.calculator.sum(); + console.log(`Somme : ${sum}`); + } +} + +let calculator: SumCalculator = new AreaCalculator([ + new Circle(5), + new Square(4), +]); +let outputter = new SumConsoleOutputter(calculator); + +outputter.output(); // affiche la somme des aires + +calculator = new VolumeCalculator([new Cube(), new Cube()]); +outputter = new SumConsoleOutputter(calculator); + +export default {}; diff --git a/solid/dependency-inversion/before.ts b/solid/dependency-inversion/before.ts new file mode 100644 index 0000000..f70eb86 --- /dev/null +++ b/solid/dependency-inversion/before.ts @@ -0,0 +1,64 @@ +interface Shape { + area(): number; +} + +interface SolidShape { + volume(): number; +} + +class Circle implements Shape { + constructor(private readonly radius: number) {} + + area(): number { + return Math.PI * Math.pow(this.radius, 2); + } +} + +class Square implements Shape { + constructor(private readonly length: number) {} + + area(): number { + return Math.pow(this.length, 2); + } +} + +class Cube implements Shape, SolidShape { + area(): number { + return 0; + } + + volume(): number { + return 0; + } +} + +class AreaCalculator { + constructor(private readonly shapes: Shape[]) {} + + public sum(): number { + return this.shapes.reduce((total, shape) => total + shape.area(), 0); + } +} + +class VolumeCalculator extends AreaCalculator { + public sum(): number { + // logique de calcul, on retournerait la somme des volumes + return 0; + } +} + +class SumConsoleOutputter { + constructor(private readonly calculator: AreaCalculator) {} + + public output(): void { + const sum = this.calculator.sum(); + console.log(`Somme : ${sum}`); + } +} + +const calculator = new VolumeCalculator([new Circle(5), new Square(4)]); +const outputter = new SumConsoleOutputter(calculator); + +outputter.output(); // affiche la somme des aires + +export default {}; diff --git a/solid/interface-segregation/after.ts b/solid/interface-segregation/after.ts new file mode 100644 index 0000000..fe398e2 --- /dev/null +++ b/solid/interface-segregation/after.ts @@ -0,0 +1,68 @@ +interface Shape { + area(): number; +} + +interface SolidShape { + volume(): number; +} + +class Circle implements Shape { + constructor(private readonly radius: number) {} + + area(): number { + return Math.PI * Math.pow(this.radius, 2); + } +} + +class Square implements Shape { + constructor(private readonly length: number) {} + + area(): number { + return Math.pow(this.length, 2); + } +} + +class Cube implements Shape, SolidShape { + area(): number { + return 0; + } + + volume(): number { + return 0; + } +} + +class AreaCalculator { + constructor(private readonly shapes: Shape[]) {} + + public sum(): number { + return this.shapes.reduce((total, shape) => total + shape.area(), 0); + } +} + +class VolumeCalculator extends AreaCalculator { + constructor(private readonly solidShapes: SolidShape[]) { + super([]); + } + + public sum(): number { + // logique de calcul, on retournerait la somme des volumes + return this.solidShapes.reduce((total, shape) => total + shape.volume(), 0); + } +} + +class SumConsoleOutputter { + constructor(private readonly calculator: AreaCalculator) {} + + public output(): void { + const sum = this.calculator.sum(); + console.log(`Somme des aires : ${sum}`); + } +} + +const calculator = new VolumeCalculator([new Cube(),new Cube()]); +const outputter = new SumConsoleOutputter(calculator); + +outputter.output(); // affiche la somme des aires + +export default {}; diff --git a/solid/interface-segregation/before.ts b/solid/interface-segregation/before.ts new file mode 100644 index 0000000..90fbaff --- /dev/null +++ b/solid/interface-segregation/before.ts @@ -0,0 +1,59 @@ +interface Shape { + area(): number; + volume(): number; +} + +class Circle implements Shape { + constructor(private readonly radius: number) {} + + volume(): number { + throw new Error("Method not implemented."); + } + + area(): number { + return Math.PI * Math.pow(this.radius, 2); + } +} + +class Square implements Shape { + constructor(private readonly length: number) {} + + volume(): number { + throw new Error("Method not implemented."); + } + + area(): number { + return Math.pow(this.length, 2); + } +} + +class AreaCalculator { + constructor(private readonly shapes: Shape[]) {} + + public sum(): number { + return this.shapes.reduce((total, shape) => total + shape.area(), 0); + } +} + +class VolumeCalculator extends AreaCalculator { + public sum(): number { + // logique de calcul, on retournerait la somme des volumes + return 0; + } +} + +class SumConsoleOutputter { + constructor(private readonly calculator: AreaCalculator) {} + + public output(): void { + const sum = this.calculator.sum(); + console.log(`Somme des aires : ${sum}`); + } +} + +const calculator = new VolumeCalculator([new Circle(5), new Square(4)]); +const outputter = new SumConsoleOutputter(calculator); + +outputter.output(); // affiche la somme des aires + +export default {}; diff --git a/solid/interface-segregation/reader_writer.ts b/solid/interface-segregation/reader_writer.ts new file mode 100644 index 0000000..d0a7907 --- /dev/null +++ b/solid/interface-segregation/reader_writer.ts @@ -0,0 +1,16 @@ +interface ReaderAndWriter extends Writer, Reader {} + +interface Writer { + write(data: any): void; +} + +interface Reader { + read(): any; +} + +// Beaucoup plus loin dans votre code + +function doSomething(rw: Reader) { + // On fait quelque chose... + rw.read(); +} diff --git a/solid/liskov/after.ts b/solid/liskov/after.ts new file mode 100644 index 0000000..3832cc4 --- /dev/null +++ b/solid/liskov/after.ts @@ -0,0 +1,50 @@ +interface Shape { + area(): number; +} + +class Circle implements Shape { + constructor(private readonly radius: number) {} + + area(): number { + return Math.PI * Math.pow(this.radius, 2); + } +} + +class Square implements Shape { + constructor(private readonly length: number) {} + + area(): number { + return Math.pow(this.length, 2); + } +} + +class AreaCalculator { + constructor(private readonly shapes: Shape[]) {} + + public sum(): number { + return this.shapes.reduce((total, shape) => total + shape.area(), 0); + } +} + +class VolumeCalculator extends AreaCalculator { + public sum(): number { + // logique de calcul, on retournerait la somme des volumes + return 0; + } +} + +class SumConsoleOutputter { + constructor(private readonly calculator: AreaCalculator) {} + + public output(): void { + const sum = this.calculator.sum(); + console.log(`Somme des aires : ${sum}`); + } +} + +const calculator = new VolumeCalculator([new Circle(5), new Square(4)]); +const outputter = new SumConsoleOutputter(calculator); + +outputter.output(); // affiche la somme des aires + +export default {}; diff --git a/solid/liskov/before.ts b/solid/liskov/before.ts new file mode 100644 index 0000000..484bbe7 --- /dev/null +++ b/solid/liskov/before.ts @@ -0,0 +1,54 @@ +interface Shape { + area(): number; +} + +class Circle implements Shape { + constructor(private readonly radius: number) {} + + area(): number { + return Math.PI * Math.pow(this.radius, 2); + } +} + +class Square implements Shape { + constructor(private readonly length: number) {} + + area(): number { + return Math.pow(this.length, 2); + } +} + +class AreaCalculator { + constructor(private readonly shapes: Shape[]) {} + + public sum(): number { + return this.shapes.reduce((total, shape) => total + shape.area(), 0); + } +} + +class VolumeCalculator extends AreaCalculator { + // @ts-ignore + public sum(): number[] { + // logique de calcul + return [ + /** volume pour chaque forme */ + ]; + } +} + +class SumConsoleOutputter { + constructor(private readonly calculator: AreaCalculator) {} + + public output(): void { + const sum = this.calculator.sum(); + console.log(`Somme des aires : ${sum}`); + } +} + +const calculator = new VolumeCalculator([new Circle(5), new Square(4)]); +// @ts-ignore +const outputter = new SumConsoleOutputter(calculator); + +outputter.output(); // affiche la somme des aires + +export default {}; diff --git a/solid/open-close/after.ts b/solid/open-close/after.ts new file mode 100644 index 0000000..c9c46ef --- /dev/null +++ b/solid/open-close/after.ts @@ -0,0 +1,77 @@ +interface Shape { + area(): number; +} + +class Circle implements Shape { + constructor(private readonly radius: number) {} + + area(): number { + return Math.PI * Math.pow(this.radius, 2); + } +} + +class Square implements Shape { + constructor(private readonly length: number) {} + + area(): number { + return Math.pow(this.length, 2); + } +} + +class Triangle implements Shape { + constructor(private readonly length: number) {} + + area(): number { + return Math.pow(this.length, 2); // à modifier pour aire du triangle + } +} + +// Imaginons qu'on ajoute une autre forme ... + +class AreaCalculator { + constructor(private readonly shapes: Shape[]) {} + + public sum(): number { + return this.shapes.reduce((total, shape) => total + shape.area(), 0); + } +} + +class SumConsoleOutputter { + constructor(private readonly calculator: AreaCalculator) {} + + public output(): void { + const sum = this.calculator.sum(); + console.log(`Somme des aires : ${sum}`); + } +} + +const calculator = new AreaCalculator([new Circle(5), new Square(4), new Triangle(3)]); +const outputter = new SumConsoleOutputter(calculator); + +outputter.output(); // affiche la somme des aires + +export default {}; + +class EventBus { + constructor(private readonly handlers: any = {}) {} + + public registerHandler(type: string, handler: (msg: any) => void) { + this.handlers[type] = handler; + } + + public dispatch(msg: any) { + const handler = this.handlers[msg.type] + handler(msg); + } +} + +const bus = new EventBus(); + +bus.registerHandler('msg_a', (msg: any) => console.log(msg)); +bus.registerHandler('msg_b', (msg: any) => console.log(msg)); +bus.registerHandler('msg_c', (msg: any) => console.log(msg)); +bus.registerHandler('msg_d', (msg: any) => console.log(msg)); + +bus.dispatch({ type: 'msg_a' }) +bus.dispatch({ type: 'msg_b' }) +bus.dispatch({ type: 'msg_c' }) diff --git a/solid/open-close/before.ts b/solid/open-close/before.ts new file mode 100644 index 0000000..4c3d0ca --- /dev/null +++ b/solid/open-close/before.ts @@ -0,0 +1,48 @@ +class Circle { + constructor(public readonly radius: number) {} +} + +class Square { + constructor(public readonly length: number) {} +} + +// Imaginons qu'on ajoute une autre forme ... +class Triangle { + constructor(public readonly length: number) {} +} + +class AreaCalculator { + constructor(private readonly shapes: object[]) {} + + public sum(): number { + let result = 0; + + for (const shape of this.shapes) { + if (shape instanceof Circle) { + result += Math.PI * Math.pow(shape.radius, 2); + } else if (shape instanceof Square) { + result += Math.pow(shape.length, 2); + } else if (shape instanceof Triangle) { + result += Math.pow(shape.length, 2); // Calcul aire triangle + } + } + + return result; + } +} + +class SumConsoleOutputter { + constructor(private readonly calculator: AreaCalculator) {} + + public output(): void { + const sum = this.calculator.sum(); + console.log(`Somme des aires : ${sum}`); + } +} + +const calculator = new AreaCalculator([new Circle(5), new Square(4)]); +const outputter = new SumConsoleOutputter(calculator); + +outputter.output(); // affiche la somme des aires + +export default {}; diff --git a/solid/segregation/after.ts b/solid/segregation/after.ts new file mode 100644 index 0000000..346f7cf --- /dev/null +++ b/solid/segregation/after.ts @@ -0,0 +1,32 @@ +class Circle { + constructor(public readonly radius: number) {} +} + +class Square { + constructor(public readonly length: number) {} +} + +class AreaCalculator { + constructor(private readonly shapes: object[]) {} + + public sum(): number { + // Logique pour additionner les aires ... + return 0; + } +} + +class SumConsoleOutputter { + constructor(private readonly calculator: AreaCalculator) {} + + public output(): void { + const sum = this.calculator.sum(); + console.log(`Somme des aires : ${sum}`); + } +} + +const calculator = new AreaCalculator([new Circle(5), new Square(4)]); +const outputter = new SumConsoleOutputter(calculator); + +outputter.output(); // affiche la somme des aires + +export default {}; diff --git a/solid/segregation/before.ts b/solid/segregation/before.ts new file mode 100644 index 0000000..77a0fec --- /dev/null +++ b/solid/segregation/before.ts @@ -0,0 +1,27 @@ +class Circle { + constructor(public readonly radius: number) {} +} + +class Square { + constructor(public readonly length: number) {} +} + +class AreaCalculator { + constructor(private readonly shapes: object[]) {} + + public sum(): number { + // Logique pour additionner les aires ... + return 0; + } + + public printSum(): void { + const sum = this.sum(); + console.log(`Somme des aires : ${sum}`); + } +} + +const calculator = new AreaCalculator([new Circle(5), new Square(4)]); + +calculator.printSum(); // affiche la somme des aires + +export default {};