76 lines
1.5 KiB
TypeScript
76 lines
1.5 KiB
TypeScript
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 {};
|