ajout démo SOLID
This commit is contained in:
parent
4143bc665e
commit
d8340cbd07
75
solid/dependency-inversion/after.ts
Normal file
75
solid/dependency-inversion/after.ts
Normal file
@ -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 {};
|
||||
64
solid/dependency-inversion/before.ts
Normal file
64
solid/dependency-inversion/before.ts
Normal file
@ -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 {};
|
||||
68
solid/interface-segregation/after.ts
Normal file
68
solid/interface-segregation/after.ts
Normal file
@ -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 {};
|
||||
59
solid/interface-segregation/before.ts
Normal file
59
solid/interface-segregation/before.ts
Normal file
@ -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 {};
|
||||
16
solid/interface-segregation/reader_writer.ts
Normal file
16
solid/interface-segregation/reader_writer.ts
Normal file
@ -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();
|
||||
}
|
||||
50
solid/liskov/after.ts
Normal file
50
solid/liskov/after.ts
Normal file
@ -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 {};
|
||||
54
solid/liskov/before.ts
Normal file
54
solid/liskov/before.ts
Normal file
@ -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 {};
|
||||
77
solid/open-close/after.ts
Normal file
77
solid/open-close/after.ts
Normal file
@ -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' })
|
||||
48
solid/open-close/before.ts
Normal file
48
solid/open-close/before.ts
Normal file
@ -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 {};
|
||||
32
solid/segregation/after.ts
Normal file
32
solid/segregation/after.ts
Normal file
@ -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 {};
|
||||
27
solid/segregation/before.ts
Normal file
27
solid/segregation/before.ts
Normal file
@ -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 {};
|
||||
Loading…
x
Reference in New Issue
Block a user