What you read in this article:
Design patterns in java:
Factory Pattern a Design Patterns
As the name implies, this type of pattern can build a factory, which allows us to instruct the factory to build an object instead of building it ourselves.
This pattern has many advantages, including the ease of producing various objects, the reduction of coding errors, and more. You also do not need to make any changes using this method if there are any changes to the class because these changes are made by the factory. To understand how this template works, consider the example below.
We assume we have an interface. An interface is a device used to play music other than recording in the car:
public interface Vehicle { void showVehicleType(); }
We have 2 other classes for cars and engines as well:
public class Car implements Vehicle { @Override public void showVehicleType() { System.out.println("Vehicle: Car"); } } public class MotorCycle implements Vehicle { @Override public void showVehicleType() { System.out.println("Vehicle: MotorCycle"); } }
In order to be able to run the factory pattern, we create another class called the VehicleFactory.
public class VehicleFactory { public Vehicle createVehicle(String vehicleType){ if(vehicleType == null){ return null; } else if(vehicleType.equals("Car")){ return new Car(); } else if(vehicleType.equals("MotorCycle")){ return new MotorCycle(); } return null; }
VehicleFactory vehicleFactory = new VehicleFactory(); Vehicle car = vehicleFactory.createVehicle("Car"); car.showVehicleType(); Vehicle Output: Car Vehicle motorCycle = vehicleFactory.createVehicle("MotorCycle"); motorCycle.showVehicleType(); Vehicle: MotorCycle Output //
In this example you saw that using this method is not difficult at all. And we used a string to determine the vehicle. It is clear that if you use any discipline other than car or motorcycle, some null will return.
Obviously, you can use other methods to indicate the type of your device. One of these strings is the static string, which you can see below in the sample code:
public class VehicleFactory { public static final String TYPE_CAR = "Car" public static final String TYPE_MOTORCYCLE = "MotorCycle" public Vehicle createVehicle(String vehicleType){ if(vehicleType == null){ return null; } else if(vehicleType.equals(TYPE_CAR)){ return new Car(); } else if(vehicleType.equals(TYPE_MOTORCYCLE)){ return new MotorCycle(); } return null; } }
You can also define the object in the following way:
Vehicle motorCycle = vehicleFactory.createVehicle(VehicleFactory.TYPE_MOTORCYCLE)
public class VehicleFactory { public enum VehicleTypeEnum{ CAR, MOTOR_CYCLE } public Vehicle createVehicle(VehicleTypeEnum vehicleTypeEnum){ if(vehicleTypeEnum== null){ return null; } else if(vehicleTypeEnum.equals(VehicleTypeEnum.CAR)){ return new Car(); } else if(vehicleTypeEnum.equals(VehicleTypeEnum.MOTOR_CYCLE)){ return new MotorCycle(); } return null; } }
To generate an object, you must do the following:
Vehicle car = vehicleFactory.createVehicle(VehicleFactory.VehicleTypeEnum.CAR);
public class VehicleFactory { public Vehicle createCar(){ return new Car(); } public Vehicle createMotorCycle(){ return new MotorCycle(); } {
To create an object, you can also do the following coding:
Vehicle carObject = vehicleFactory.createCar(); Vehicle motorCycleObject = vehicleFactory.createMotorCycle();