Skip to content

Commit 1026914

Browse files
Mayur KadamMayur Kadam
authored andcommitted
Added Decorator design pattern
1 parent 0791250 commit 1026914

File tree

8 files changed

+112
-0
lines changed

8 files changed

+112
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { Vehicle } from "../Vehicle";
2+
3+
export class BaseBike implements Vehicle {
4+
getDescription(): string {
5+
return "Base Bike";
6+
}
7+
8+
getCost(): number {
9+
return 100000; // Base price
10+
}
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { Vehicle } from "../Vehicle";
2+
3+
export class BaseCar implements Vehicle {
4+
getDescription(): string {
5+
return "Base Car";
6+
}
7+
8+
getCost(): number {
9+
return 500000; // Base price
10+
}
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { Vehicle } from "../Vehicle";
2+
3+
export class VehicleDecorator implements Vehicle {
4+
decoratedVehicle: Vehicle;
5+
6+
constructor(decoratedVehicle: Vehicle) {
7+
this.decoratedVehicle = decoratedVehicle;
8+
}
9+
10+
getCost(): number {
11+
return this.decoratedVehicle.getCost();
12+
}
13+
getDescription(): string {
14+
return this.decoratedVehicle.getDescription();
15+
}
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { VehicleDecorator } from "../Decorators/VehicleDecorator";
2+
3+
export class GPS extends VehicleDecorator {
4+
getDescription(): string {
5+
return `${this.decoratedVehicle.getDescription()}, GPS`;
6+
}
7+
8+
getCost(): number {
9+
return this.decoratedVehicle.getCost() + 30000;
10+
}
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { VehicleDecorator } from "../Decorators/VehicleDecorator";
2+
3+
export class SportsMode extends VehicleDecorator {
4+
getDescription(): string {
5+
return `${this.decoratedVehicle.getDescription()}, Sport Mode.`;
6+
}
7+
8+
getCost(): number {
9+
return this.decoratedVehicle.getCost() + 70000;
10+
}
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { VehicleDecorator } from "../Decorators/VehicleDecorator";
2+
3+
export class Sunroof extends VehicleDecorator {
4+
getDescription(): string {
5+
return `${this.decoratedVehicle.getDescription()}, Sunroof`;
6+
}
7+
8+
getCost(): number {
9+
return this.decoratedVehicle.getCost() + 50000;
10+
}
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export interface Vehicle {
2+
getDescription(): string;
3+
getCost(): number;
4+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { BaseBike } from "./Base/BaseBike";
2+
import { BaseCar } from "./Base/BaseCar";
3+
import { GPS } from "./Features/GPS";
4+
import { SportsMode } from "./Features/SportsMode";
5+
import { Sunroof } from "./Features/Sunroof";
6+
7+
8+
// CAR
9+
let myCar = new BaseCar();
10+
11+
console.log(`${myCar.getDescription()} costs ₹${myCar.getCost()}`);
12+
console.log();
13+
14+
myCar = new GPS(myCar);
15+
console.log(`${myCar.getDescription()} costs ₹${myCar.getCost()}`);
16+
console.log();
17+
18+
myCar = new Sunroof(myCar);
19+
console.log(`${myCar.getDescription()} costs ₹${myCar.getCost()}`);
20+
console.log();
21+
22+
myCar = new SportsMode(myCar);
23+
console.log(`${myCar.getDescription()} costs ₹${myCar.getCost()}`);
24+
console.log();
25+
26+
// BIKE
27+
let myBike = new BaseBike();
28+
console.log(`${myBike.getDescription()} costs ₹${myBike.getCost()}`);
29+
console.log();
30+
31+
myBike = new GPS(myBike);
32+
console.log(`${myBike.getDescription()} costs ₹${myBike.getCost()}`);
33+
console.log();
34+
35+
myBike = new SportsMode(myBike);
36+
console.log(`${myBike.getDescription()} costs ₹${myBike.getCost()}`);
37+
console.log();

0 commit comments

Comments
 (0)