Design Patterns in Java:

I, Arslan Mirbzergi,in this article, I’m going to familiarize you with Design Patterns in Java and provide you with a simple description. Design Patterns or design patterns are actually the best solutions to the problems that exist in the software design process.

Factory Pattern a Design Patterns

This pattern is a Design Patterns. As is evident from the name of this Design Patterns, this pattern allows us to order the factory to build what we intend when making an object, rather than making an object by ourselves, and after that, the object is manufactured and prepared by the factory.
This template also has advantages such as ease of production of various objects, reducing the coded error. It also has no need to make changes if any changes are made to the class (these changes will be made by the factory). To understand how the Factory pattern works as a Design Patterns, you can pay attention to the example below.
Let’s assume we have an interface (a means of playing music from a recording other than a recording installed on the machine):
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");
     }
 }
To implement the Factory template, we also build another class called 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;
    }

At this point, we just need to code as follows as long as we want to build a car or engine:
VehicleFactory vehicleFactory = new VehicleFactory();

Vehicle car = vehicleFactory.createVehicle("Car");
car.showVehicleType();
Vehicle Output: Car

Vehicle motorCycle = vehicleFactory.createVehicle("MotorCycle");
 motorCycle.showVehicleType();
Vehicle: MotorCycle Output //
You saw it wasn’t that hard. In this example, we used strings to determine the type of vehicle. It is clear that if we use any string other than Car or MotorCycle, the null value will return.
Clearly, other methods can also be used to show the type of vehicle. One of these strings is the static string that you can see in the code below the sample.
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;
    } 
}
To define an object, we can also do as follows:
Vehicle motorCycle = vehicleFactory.createVehicle(VehicleFactory.TYPE_MOTORCYCLE)

There is another way to use enum to determine the type of class requested below:
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;
        }
}
And at the time of object production, we do as follows:
Vehicle car = vehicleFactory.createVehicle(VehicleFactory.VehicleTypeEnum.CAR);
In order to comply with the open/close principle, which is one of solid’s five principles, we can also implement our factory class in the opposite way:
public class VehicleFactory {
    public Vehicle createCar(){
         return new Car();
    }
    public Vehicle createMotorCycle(){
        return new MotorCycle();
    }
{

To create an object, we can also do coding as follows:

Vehicle carObject = vehicleFactory.createCar();
Vehicle motorCycleObject = vehicleFactory.createMotorCycle();

at last

In this article, you are familiar with the general definition of design patterns in Java. Like any other Design Patterns, this template can make the desired changes when you use it. For example, the definition of the function or functions of the manufacturer of objects inside the factory is static. Also, by passing the time of making objects to specific parameters and going for inheritances, accept multilevel.