A Java implementation of Abstract Factory and Singleton design patterns developed for the SLIIT OOP (Object-Oriented Programming) module.
This project simulates a Vehicle Factory that produces Cars and Buses from specific factories.
This system demonstrates the use of:
- Abstract Factory Pattern → To create families of related objects (CarFactory and BusFactory).
- Singleton Pattern → To ensure only one instance of each factory and vehicle type exists.
- Interfaces → To achieve loose coupling between products (Cars and Buses).
- Acts as the abstract factory selector.
- Returns
CarFactoryorBusFactoryvia thegetVehicle(String choice)method.
- Implement the Singleton Pattern.
- Provide the
getModel(String vehicle)method to return the correct car or bus object.
BMW,Benz, andRollsRoyce- Each follows Singleton design and implements
ICarinterface. - Each overrides the
displayVehicle()method.
Volvo,Fuso, andTATA- Each follows Singleton design and implements
IBusinterface. - Each overrides the
displayVehicle()method.
ICar: DeclaresdisplayVehicle()for car models.IBus: DeclaresdisplayVehicle()for bus models.VehicleFactory: DeclaresgetModel(String vehicle)for factory classes.
- Demonstrates factory selection and vehicle creation:
VehicleFactory carFactory = VehicleProducer.getVehicle("Car"); ICar car1 = (ICar) carFactory.getModel("BMW"); car1.displayVehicle();
| Pattern | Purpose |
|---|---|
| Abstract Factory | To encapsulate creation of related vehicle families |
| Singleton | To ensure a single shared instance of each factory and model |
| Interface Segregation | To separate car and bus behaviors clearly |
⚙️ How to Run
Clone the Repository
git clone https://github.com/dyneth02/AbstractFactory-VehicleSystem-Java.git cd AbstractFactory-VehicleSystem-Java
Compile the Java Files
javac *.java
Run the Demo
java VehicleDemo
Expected Output Example
--- Vehicle Factory Demo --- Selected Factory: CarFactory BMW: Luxury performance car displayed. Benz: Premium comfort car displayed. RollsRoyce: Ultimate luxury car displayed.
Selected Factory: BusFactory Volvo: City transport bus displayed. Fuso: Commercial transport bus displayed. TATA: Long-distance travel bus displayed.
🧠 Concepts Demonstrated
Abstract Factory Pattern Singleton Pattern Factory Method Pattern Polymorphism and Interfaces Encapsulation and Object Reuse