Assume that you have a class or an interfaces called Vehicle. You can create various types of vehicles which are instances of this class(or interface). For an example an instance can be a car or a motorcycle.
Assume that the below class is your Vehicle class which contains complex methods.(Since I need to keep this tutorial short, I am not using an interface here. But note that the best design is making the vehicle class an interface and implementing the Car and Motorcycle classes using that.)
public class Vehicle {
String name;
public void addWheels(int count){}
public void addController(){}
public void addFuelTank(){}
public void setName(String name){this.name = name;}
public String getName(){return this.name;}
String name;
public void addWheels(int count){}
public void addController(){}
public void addFuelTank(){}
public void setName(String name){this.name = name;}
public String getName(){return this.name;}
}
Building a car or a motorcycle is a complex and multi stepped process. In such cases you can use a VehicleBuilder to make this process organized. In a VehicleBuilder instance there is an instance of Vehicle class and getVehicle() method returns that after constructed.
This is your VehicleBuilder interface.
public interface VehicleBuilder {
public void setWheels();
public void setController();
public void setFuelTank();
public void setName();
public Vehicle getVehicle();
}
public void setWheels();
public void setController();
public void setFuelTank();
public void setName();
public Vehicle getVehicle();
}
You can have multiple implementations of this interface. For an example CarBuilder and MotorcycleBuilder.
public class CarBuilder implements VehicleBuilder {
Vehicle vehicle = new Vehicle();
public void setWheels() {
//Fixing 4 wheels (complex logic)
vehicle.addWheels(4);
}
public void setController() {
//Fix a steering wheel complex logic
vehicle.addController();
}
public void setFuelTank() {
//Fixing car fuel tank (complex logic)
vehicle.addFuelTank();
}
public void setName() {
//seting name (complex logic)
vehicle.setName("car");
}
public Vehicle getVehicle() {
return vehicle;
}
}
Vehicle vehicle = new Vehicle();
public void setWheels() {
//Fixing 4 wheels (complex logic)
vehicle.addWheels(4);
}
public void setController() {
//Fix a steering wheel complex logic
vehicle.addController();
}
public void setFuelTank() {
//Fixing car fuel tank (complex logic)
vehicle.addFuelTank();
}
public void setName() {
//seting name (complex logic)
vehicle.setName("car");
}
public Vehicle getVehicle() {
return vehicle;
}
}
public class MotorcycleBuilder implements VehicleBuilder {
Vehicle vehicle = new Vehicle();
public void setWheels() {
//Fixing 2 wheels (complex logic)
vehicle.addWheels(2);
}
public void setController() {
//Fixing a handle (complex logic)
vehicle.addController();
}
public void setFuelTank() {
//Fixing motorcycle fuel tank (complex logic)
vehicle.addFuelTank();
}
public void setName() {
//setting name (complex logic)
vehicle.setName("Motorcycle");
}
public Vehicle getVehicle() {
return vehicle;
}
}
Vehicle vehicle = new Vehicle();
public void setWheels() {
//Fixing 2 wheels (complex logic)
vehicle.addWheels(2);
}
public void setController() {
//Fixing a handle (complex logic)
vehicle.addController();
}
public void setFuelTank() {
//Fixing motorcycle fuel tank (complex logic)
vehicle.addFuelTank();
}
public void setName() {
//setting name (complex logic)
vehicle.setName("Motorcycle");
}
public Vehicle getVehicle() {
return vehicle;
}
}
Now in your class you can use this builders to create cars or motorcycles as below.
public class Test {
public static void main(String[] args) {
//Building a Car
VehicleBuilder carBuilder = new CarBuilder();
carBuilder.setName();
carBuilder.setWheels();
carBuilder.setController();
carBuilder.setFuelTank();
Vehicle car = carBuilder.getVehicle();
System.out.println(car.getName());
//Building a Motorcycle
VehicleBuilder motorcycleBuilder = new MotorcycleBuilder();
motorcycleBuilder.setName();
motorcycleBuilder.setWheels();
motorcycleBuilder.setController();
motorcycleBuilder.setFuelTank();
Vehicle bike = motorcycleBuilder.getVehicle();
System.out.println(bike.getName());
}
}
public static void main(String[] args) {
//Building a Car
VehicleBuilder carBuilder = new CarBuilder();
carBuilder.setName();
carBuilder.setWheels();
carBuilder.setController();
carBuilder.setFuelTank();
Vehicle car = carBuilder.getVehicle();
System.out.println(car.getName());
//Building a Motorcycle
VehicleBuilder motorcycleBuilder = new MotorcycleBuilder();
motorcycleBuilder.setName();
motorcycleBuilder.setWheels();
motorcycleBuilder.setController();
motorcycleBuilder.setFuelTank();
Vehicle bike = motorcycleBuilder.getVehicle();
System.out.println(bike.getName());
}
}
No comments:
Post a Comment