Skip to content

Commit 0791250

Browse files
Mayur KadamMayur Kadam
authored andcommitted
Added Strategy Pattern
1 parent 128f635 commit 0791250

File tree

17 files changed

+343
-0
lines changed

17 files changed

+343
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { DriveStrategy } from "./DriveStrategy/DriveStrategy";
2+
3+
class Car {
4+
protected driveStrategy: DriveStrategy;
5+
constructor(driveStrategy: DriveStrategy) {
6+
this.driveStrategy = driveStrategy;
7+
}
8+
9+
drive(): void {
10+
this.driveStrategy.drive();
11+
}
12+
13+
hasEngine(): void {
14+
console.log("Yes, engine present.");
15+
}
16+
};
17+
18+
export { Car };
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
interface DriveStrategy {
2+
drive(): void;
3+
}
4+
5+
6+
export { DriveStrategy };
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { DriveStrategy } from "./DriveStrategy";
2+
3+
class NormalDrive implements DriveStrategy {
4+
drive(): void {
5+
console.log("Car can normally drive.");
6+
}
7+
}
8+
9+
export { NormalDrive };
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { DriveStrategy } from "./DriveStrategy";
2+
3+
class NotANormalDrive implements DriveStrategy {
4+
drive(): void {
5+
console.log("Car can't normally drive.");
6+
}
7+
}
8+
9+
export { NotANormalDrive };
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { Car } from "./Car";
2+
import { NotANormalDrive } from "./DriveStrategy/NotANormalDrive";
3+
4+
class MountainCar extends Car {
5+
6+
constructor() {
7+
super(new NotANormalDrive());
8+
}
9+
};
10+
11+
export { MountainCar };
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { Car } from "./Car";
2+
import { NormalDrive } from "./DriveStrategy/NormalDrive";
3+
4+
class NormalCar extends Car {
5+
constructor() {
6+
super(new NormalDrive());
7+
}
8+
};
9+
10+
export { NormalCar };
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
# Strategy Pattern in Vehicle System
2+
3+
## Overview
4+
The **Strategy Design Pattern** is used to encapsulate different behaviors in separate classes and make them interchangeable. In our **Vehicle System**, different types of cars have different driving behaviors. Instead of implementing multiple `drive()` methods within each car class, we extract the driving behavior into separate strategy classes, following the **Strategy Pattern**.
5+
6+
## Problem Statement
7+
Initially, every car class had its own `drive()` method, even if some of them shared the same behavior. This led to **code duplication** and **maintenance difficulties**. To resolve this, we use the **Strategy Pattern**, which allows each car to delegate its driving behavior to an independent strategy class.
8+
9+
## Solution with Strategy Pattern
10+
### **Structure**
11+
```
12+
/VehicleSystem
13+
│── index.ts
14+
│── /DriveStrategy
15+
│ │── DriveStrategy.ts # Interface defining the strategy
16+
│ │── NormalDrive.ts # Concrete Strategy for normal driving
17+
│ │── NotANormalDrive.ts # Concrete Strategy for non-normal driving
18+
│── Car.ts # Base class that uses a drive strategy
19+
│── NormalCar.ts # Uses NormalDrive strategy
20+
│── MountainCar.ts # Uses NotANormalDrive strategy
21+
│── SportsCar.ts # Uses NotANormalDrive strategy
22+
```
23+
24+
### **1. DriveStrategy.ts (Strategy Interface)**
25+
```typescript
26+
export interface DriveStrategy {
27+
drive(): void;
28+
}
29+
```
30+
31+
### **2. Concrete Strategies**
32+
#### **NormalDrive.ts**
33+
```typescript
34+
import { DriveStrategy } from "./DriveStrategy";
35+
36+
export class NormalDrive implements DriveStrategy {
37+
drive(): void {
38+
console.log("Driving on a normal road.");
39+
}
40+
}
41+
```
42+
#### **NotANormalDrive.ts**
43+
```typescript
44+
import { DriveStrategy } from "./DriveStrategy";
45+
46+
export class NotANormalDrive implements DriveStrategy {
47+
drive(): void {
48+
console.log("Car can't normally drive.");
49+
}
50+
}
51+
```
52+
53+
### **3. Vehicle Base Class Using Strategy**
54+
#### **Car.ts**
55+
```typescript
56+
import { DriveStrategy } from "./DriveStrategy/DriveStrategy";
57+
58+
export class Car {
59+
protected driveStrategy: DriveStrategy;
60+
61+
constructor(driveStrategy: DriveStrategy) {
62+
this.driveStrategy = driveStrategy;
63+
}
64+
65+
drive(): void {
66+
this.driveStrategy.drive();
67+
}
68+
}
69+
```
70+
71+
### **4. Specific Car Implementations**
72+
#### **NormalCar.ts**
73+
```typescript
74+
import { Car } from "./Car";
75+
import { NormalDrive } from "./DriveStrategy/NormalDrive";
76+
77+
export class NormalCar extends Car {
78+
constructor() {
79+
super(new NormalDrive());
80+
}
81+
}
82+
```
83+
84+
#### **MountainCar.ts**
85+
```typescript
86+
import { Car } from "./Car";
87+
import { NotANormalDrive } from "./DriveStrategy/NotANormalDrive";
88+
89+
export class MountainCar extends Car {
90+
constructor() {
91+
super(new NotANormalDrive());
92+
}
93+
}
94+
```
95+
96+
#### **SportsCar.ts**
97+
```typescript
98+
import { Car } from "./Car";
99+
import { NotANormalDrive } from "./DriveStrategy/NotANormalDrive";
100+
101+
export class SportsCar extends Car {
102+
constructor() {
103+
super(new NotANormalDrive());
104+
}
105+
}
106+
```
107+
108+
### **5. Example Usage in index.ts**
109+
```typescript
110+
import { NormalCar } from "./NormalCar";
111+
import { MountainCar } from "./MountainCar";
112+
113+
const normalCar = new NormalCar();
114+
normalCar.drive(); // Output: Car can normally drive.
115+
116+
const mountainCar = new MountainCar();
117+
mountainCar.drive(); // Output: Car can't normally drive.
118+
```
119+
120+
## **Benefits of Using Strategy Pattern**
121+
1. **Avoids Code Duplication** – Drive logic is encapsulated in separate strategy classes.
122+
2. **Enhances Maintainability** – New driving behaviors can be added without modifying existing vehicle classes.
123+
3. **Supports Open/Closed Principle** – Allows extension without modifying existing code.
124+
4. **Promotes Flexibility** – The driving behavior can be changed dynamically at runtime.
125+
126+
This implementation ensures a scalable, flexible, and maintainable system by adhering to **SOLID** principles. 🚀
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { Car } from "./Car";
2+
import { NotANormalDrive } from "./DriveStrategy/NotANormalDrive";
3+
4+
class SportsCar extends Car {
5+
constructor() {
6+
super(new NotANormalDrive());
7+
}
8+
};
9+
10+
export { SportsCar };
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { MountainCar } from "./MountainCar";
2+
import { NormalCar } from "./NormalCar";
3+
import { SportsCar } from "./SportsCar";
4+
5+
const myNormalCar = new NormalCar();
6+
const mySportsCar = new SportsCar();
7+
const myMountainCar = new MountainCar();
8+
9+
myNormalCar.drive(); // Car can normally drive.
10+
myNormalCar.hasEngine(); // Yes, engine present.
11+
12+
mySportsCar.drive(); // Car can't normally drive.
13+
mySportsCar.hasEngine(); // Yes, engine present.
14+
15+
myMountainCar.drive(); // Car can't normally drive.
16+
mySportsCar.hasEngine(); // Yes, engine present.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Car {
2+
drive(): void {
3+
console.log("Car can normally drive.");
4+
}
5+
6+
hasEngine(): void {
7+
console.log("Yes, engine present.");
8+
}
9+
};
10+
11+
export { Car };

0 commit comments

Comments
 (0)