Design patterns in java:

I am Arsalan Mirbozorgi, and in this article, I want to introduce you Design Patterns and tell you what features these items have in Java.  Design patterns are a great way to solve problems you may encounter when designing software.

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;
    }

At this stage, you only need to code the following to build a machine 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 //

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)

There is another way to use enum and you can use it to determine the type of class you want.  You can see an example of it 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;
        }
}

To generate an object, you must do the following:

Vehicle car = vehicleFactory.createVehicle(VehicleFactory.VehicleTypeEnum.CAR);
To follow the principle of clos/ open, which is the five principles of solid, you can also run the following as your factory class:
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();

at last

In this article, we get acquainted with the general definition of a pattern design in Java. Note that this pattern, like any other pattern, can make the changes you want when using it. For example, you can define the function and constructor functions of objects within a static factory. You can also use the positive features of these patterns by passing the time of making objects to various specific parameters and using multi-level inheritance.

Leave A Comment

13 − 6 =

Please Send Email

Your message sent successfully
There has been an error