Factory Pattern

Factory pattern is one of the most used design patterns in OOP. This type of design pattern comes under creational pattern as this pattern provides one of the best ways to create an object.

In Factory pattern, we create object without exposing the creation logic to the client and refer to newly created object using a common interface.

Implementation

We’re going to create a Vehicle interface and concrete classes implementing the Vehicle interface. A factory class VehicleFactory is defined as a next step.

Vehicle.java

Car.java

Motorcicle.java

Truck.java

VehicleFactory.java

Main.java

Output

 

 

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *