File tree Expand file tree Collapse file tree 8 files changed +112
-0
lines changed
05 - Low Level Design/03 - TypeScript Design Pattern/Structural Design Patterns/Proxy/Decorator/Vehicle System Expand file tree Collapse file tree 8 files changed +112
-0
lines changed Original file line number Diff line number Diff line change 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 number Diff line number Diff line change 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 number Diff line number Diff line change 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 number Diff line number Diff line change 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 number Diff line number Diff line change 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 number Diff line number Diff line change 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 number Diff line number Diff line change 1+ export interface Vehicle {
2+ getDescription ( ) : string ;
3+ getCost ( ) : number ;
4+ }
Original file line number Diff line number Diff line change 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 ( ) ;
You can’t perform that action at this time.
0 commit comments