Skip to content

Commit f50973a

Browse files
Mayur KadamMayur Kadam
authored andcommitted
Added abstract factory pattern
1 parent 40c658a commit f50973a

File tree

7 files changed

+153
-0
lines changed

7 files changed

+153
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { Vehicle } from "./Vehicle";
2+
3+
class Bicycle implements Vehicle {
4+
isEngine(): boolean {
5+
return false;
6+
}
7+
8+
isElectric(): boolean {
9+
return true;
10+
}
11+
12+
getEngineCount(): number {
13+
return 0;
14+
}
15+
}
16+
17+
export { Bicycle };
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { Vehicle } from "./Vehicle";
2+
3+
class Bike implements Vehicle {
4+
isEngine(): boolean {
5+
return true;
6+
}
7+
8+
isElectric(): boolean {
9+
return true;
10+
}
11+
12+
getEngineCount(): number {
13+
return 1;
14+
}
15+
}
16+
17+
export { Bike };
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { Vehicle } from "./Vehicle";
2+
3+
class Car implements Vehicle {
4+
5+
isEngine(): boolean {
6+
return true;
7+
}
8+
isElectric(): boolean {
9+
return true;
10+
}
11+
getEngineCount(): number {
12+
return 1;
13+
}
14+
}
15+
16+
17+
export { Car };
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# Vehicle Factory Pattern
2+
3+
## Overview
4+
This project implements the **Factory Pattern** to manage the creation of different types of vehicles. The Factory Pattern is a **Creational Design Pattern** that provides an interface for creating objects in a superclass but allows subclasses to alter the type of objects that will be created.
5+
6+
## Project Structure
7+
```
8+
- Bicycle.ts
9+
- Bike.ts
10+
- Car.ts
11+
- Vehicle.ts (Interface)
12+
- VehicleFactory.ts (Factory Class)
13+
- index.ts (Usage Example)
14+
```
15+
16+
### Components:
17+
1. **Vehicle Interface (`Vehicle.ts`)**
18+
- Defines the common methods all vehicle types must implement.
19+
20+
2. **Concrete Vehicle Classes**
21+
- `Bicycle.ts` → Implements `Vehicle` (No Engine, Electric)
22+
- `Bike.ts` → Implements `Vehicle` (Engine, Electric)
23+
- `Car.ts` → Implements `Vehicle` (Engine, Electric)
24+
25+
3. **Factory Class (`VehicleFactory.ts`)**
26+
- Provides a static method `getVehicleObject(vehicleType: string)` to create vehicle objects based on input.
27+
28+
4. **Usage (`index.ts`)**
29+
- Demonstrates how to use the factory to create vehicle objects dynamically.
30+
31+
## Flow Explanation
32+
1. The user requests a vehicle by name using `VehicleFactory.getVehicleObject("Car")`.
33+
2. The factory checks the type and returns the corresponding instance (e.g., `new Car()`).
34+
3. If the requested vehicle type is invalid, `null` is returned (could be improved using a `NullVehicle` class).
35+
4. The created object can then be used via the `Vehicle` interface methods (`isEngine()`, `isElectric()`, `getEngineCount()`).
36+
37+
## Benefits of the Factory Pattern
38+
- **Encapsulation** – Hides object creation logic from the client.
39+
- **Scalability** – New vehicle types can be added with minimal changes.
40+
- **Maintainability** – Reduces code duplication and centralizes object creation.
41+
42+
## Suggested Improvements
43+
- **Use an Enum** instead of a string for `vehicleType`.
44+
- **Implement the Null Object Pattern** to handle unknown vehicle types gracefully.
45+
46+
## Example Usage
47+
```typescript
48+
const myVehicle = VehicleFactory.getVehicleObject("Bicycle");
49+
console.log(myVehicle?.isEngine()); // false
50+
console.log(myVehicle?.isElectric()); // true
51+
console.log(myVehicle?.getEngineCount()); // 0
52+
```
53+
54+
This implementation demonstrates the Factory Pattern effectively while leaving room for enhancements like improved type safety and better handling of unknown types.
55+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
interface Vehicle {
2+
isEngine(): boolean;
3+
isElectric(): boolean;
4+
getEngineCount(): number;
5+
}
6+
7+
export { Vehicle };
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { Bicycle } from "./Bicycle";
2+
import { Bike } from "./Bike";
3+
import { Car } from "./Car";
4+
import { Vehicle } from "./Vehicle";
5+
6+
class VehicleFactory {
7+
static getVehicleObject(vehicleType: string): Vehicle | null {
8+
switch (vehicleType) {
9+
case "Car":
10+
return new Car();
11+
case "Bike":
12+
return new Bike();
13+
case "Bicycle":
14+
return new Bicycle();
15+
default:
16+
/*
17+
Here you can also implement Null Object Pattern,
18+
but for this example We mainly focus on Factory
19+
*/
20+
return null;
21+
}
22+
}
23+
}
24+
25+
export { VehicleFactory };
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { VehicleFactory } from "./VehicleFactory";
2+
3+
const myVehicle = VehicleFactory.getVehicleObject("Bicycle");
4+
5+
console.log(myVehicle?.isEngine()); // false
6+
console.log(myVehicle?.isElectric()); // true
7+
console.log(myVehicle?.getEngineCount()); // 0
8+
console.log()
9+
10+
11+
const myNewVehicle = VehicleFactory.getVehicleObject("Lambo-Car");
12+
13+
console.log(myNewVehicle?.isEngine()); // undefined
14+
console.log(myNewVehicle?.isElectric()); // undefined
15+
console.log(myNewVehicle?.getEngineCount()); // undefined

0 commit comments

Comments
 (0)