Skip to content

Commit 128f635

Browse files
Mayur KadamMayur Kadam
authored andcommitted
Added abstract factory pattern
1 parent f50973a commit 128f635

File tree

19 files changed

+199
-154
lines changed

19 files changed

+199
-154
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { Bicycle } from "../interfaces";
2+
3+
class NormalBicycle implements Bicycle {
4+
ride(): void {
5+
console.log("Riding a normal bicycle!");
6+
}
7+
}
8+
9+
export { NormalBicycle };
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { Bicycle } from "../interfaces";
2+
3+
class SportsBicycle implements Bicycle {
4+
ride(): void {
5+
console.log("Riding a sports bicycle!");
6+
}
7+
}
8+
9+
export { SportsBicycle };
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { Bike } from "../interfaces";
2+
3+
class NormalBike implements Bike {
4+
ride(): void {
5+
console.log("Riding a normal bike!");
6+
}
7+
}
8+
9+
export { NormalBike };
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { Bike } from "../interfaces";
2+
3+
class SportsBike implements Bike {
4+
ride(): void {
5+
console.log("Riding a sports bike!");
6+
}
7+
}
8+
9+
export { SportsBike };
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { Car } from "../interfaces";
2+
3+
class NormalCar implements Car {
4+
drive(): void {
5+
console.log("Driving a normal car!");
6+
}
7+
}
8+
9+
export { NormalCar };
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { Car } from "../interfaces";
2+
3+
class SportsCar implements Car {
4+
drive(): void {
5+
console.log("Driving a fast sports car!");
6+
}
7+
}
8+
9+
export { SportsCar };
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { NormalBicycle } from "./Bicycle/NormalBicycle";
2+
import { NormalBike } from "./Bike/NormalBike";
3+
import { NormalCar } from "./Car/NormalCar";
4+
import { Bicycle, Bike, Car, VehicleFactory } from "./interfaces";
5+
6+
class NormalVehicleFactory implements VehicleFactory {
7+
createBike(): Bike {
8+
return new NormalBike();
9+
}
10+
11+
createCar(): Car {
12+
return new NormalCar();
13+
}
14+
15+
createBicycle(): Bicycle {
16+
return new NormalBicycle();
17+
}
18+
}
19+
20+
export { NormalVehicleFactory };
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# Abstract Factory Pattern - Vehicle System
2+
3+
## Overview
4+
The **Abstract Factory Pattern** is a creational design pattern that provides an interface for creating families of related or dependent objects without specifying their concrete classes. This pattern is useful when multiple objects with a shared theme need to be instantiated dynamically based on some criteria.
5+
6+
In this **Vehicle System**, the Abstract Factory Pattern is implemented to create **Normal** and **Sports** variants of different vehicles like **Cars, Bikes, and Bicycles**.
7+
8+
## Folder Structure
9+
```
10+
/VehicleSystem
11+
│── index.ts
12+
│── interfaces.ts
13+
│── NormalVehicleFactory.ts
14+
│── SportsVehicleFactory.ts
15+
│── /Car
16+
│ │── NormalCar.ts
17+
│ │── SportsCar.ts
18+
│── /Bike
19+
│ │── NormalBike.ts
20+
│ │── SportsBike.ts
21+
│── /Bicycle
22+
│ │── NormalBicycle.ts
23+
│ │── SportsBicycle.ts
24+
```
25+
26+
## Design Flow
27+
1. **Define an Abstract Interface**
28+
- The `interfaces.ts` file contains an interface that defines the common structure of all vehicle types.
29+
30+
2. **Concrete Vehicle Implementations**
31+
- `NormalCar.ts` and `SportsCar.ts` implement the `Car` interface.
32+
- `NormalBike.ts` and `SportsBike.ts` implement the `Bike` interface.
33+
- `NormalBicycle.ts` and `SportsBicycle.ts` implement the `Bicycle` interface.
34+
35+
3. **Abstract Factory Interface**
36+
- The `interfaces.ts` file also contains an abstract factory interface that declares methods for creating different types of vehicles.
37+
38+
4. **Concrete Factory Implementations**
39+
- `NormalVehicleFactory.ts` implements the abstract factory to create **Normal** variants.
40+
- `SportsVehicleFactory.ts` implements the abstract factory to create **Sports** variants.
41+
42+
5. **Client Code Usage**
43+
- The `index.ts` file contains the client code that dynamically selects the appropriate factory (Normal or Sports) and creates the required vehicle objects.
44+
45+
## Code Flow
46+
- The client decides which factory to use based on the requirement.
47+
- The factory generates an instance of the specific vehicle type.
48+
- The created vehicle object follows the structure defined in the interface, ensuring consistency across different vehicle types.
49+
50+
## Benefits of Abstract Factory Pattern
51+
- **Encapsulation of Object Creation:** Clients do not need to know the exact class names; they interact with the abstract factory.
52+
- **Scalability & Maintainability:** New types of vehicles can be added without modifying existing client code.
53+
- **Consistency Across Products:** Ensures all created objects follow a uniform structure and behavior.
54+
- **Separation of Concerns:** The object creation logic is separated from its usage, improving modularity.
55+
56+
## When to Use Abstract Factory Pattern?
57+
- When a system needs to create multiple related objects dynamically.
58+
- When enforcing consistency across multiple families of objects.
59+
- When object creation logic needs to be encapsulated and separated from the main business logic.
60+
61+
This implementation of the **Abstract Factory Pattern** in a **Vehicle System** provides a structured approach to vehicle creation, allowing flexibility and scalability for future enhancements.
62+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { SportsBicycle } from "./Bicycle/SportsBicycle";
2+
import { SportsBike } from "./Bike/SportsBike";
3+
import { SportsCar } from "./Car/SportsCar";
4+
import { Bicycle, Bike, Car, VehicleFactory } from "./interfaces";
5+
6+
class SportsVehicleFactory implements VehicleFactory {
7+
createBike(): Bike {
8+
return new SportsBike();
9+
}
10+
11+
createCar(): Car {
12+
return new SportsCar();
13+
}
14+
15+
createBicycle(): Bicycle {
16+
return new SportsBicycle();
17+
}
18+
}
19+
20+
export { SportsVehicleFactory };
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { NormalVehicleFactory } from "./NormalVehicleFactory";
2+
import { SportsVehicleFactory } from "./SportsVehicleFactory";
3+
import { VehicleFactory } from "./interfaces";
4+
5+
function testFactory(factory: VehicleFactory) {
6+
const car = factory.createCar();
7+
const bike = factory.createBike();
8+
const bicycle = factory.createBicycle();
9+
10+
car.drive();
11+
bike.ride();
12+
bicycle.ride();
13+
}
14+
15+
// Example Usage
16+
console.log("Normal Vehicles:");
17+
testFactory(new NormalVehicleFactory());
18+
console.log();
19+
20+
console.log("Sports Vehicles:");
21+
testFactory(new SportsVehicleFactory());

0 commit comments

Comments
 (0)