Design Patterns
Design Patterns
Private Constructor: The Singleton class has a private constructor to prevent the
instantiation of the class from external entities.
Private Instance: The class contains a private static instance of itself.
Static Method: A static method is provided to access the instance, and it ensures
that only one instance is created if it doesn’t exist.
private Singleton() {
// Private constructor to prevent external instantiation
}
Now you are someone who want to rent a vehicle so you go to vehicle rental shop and
asked them for a desired vehicle and they provide it to you ( if available ) . So
here we can see that you are client and vehicle rental is our factory which is
having different vehicles as concrete products .
In the above image we have a Null Vehicle class this is used to handle any
unavailable vehicle as it will give us relevant information about unavailability of
any vehicle . To understand more let’s have a look on its code -:
Vehicle Interface -:
public abstract class Vehicle
{
String name;
int seatingCap;
abstract int getSeatingCapacity();
abstract String getDiscription();
}
This abstract class contains all the basic functionality that a vehicle can have .
Car class -:
public class Car extends Vehicle{
private Car()
{
}
public Car(String name,int seatingCap)
{
this.name = name;
this.seatingCap = seatingCap;
}
@Override
int getSeatingCapacity() {
return this.seatingCap;
}
@Override
String getDiscription() {
return this.name;
}
}
Bike Class -:
public class Bike extends Vehicle
{
private Bike()
{
}
public Bike(String name,int seatingCap)
{
this.name = name;
this.seatingCap = seatingCap;
}
@Override
int getSeatingCapacity() {
return this.seatingCap;
}
@Override
String getDiscription() {
return this.name;
}
}
Bus Class -:
public class Bus extends Vehicle{
private Bus()
{
}
public Bus(String name,int seatingCap)
{
this.name = name;
this.seatingCap = seatingCap;
}
@Override
int getSeatingCapacity() {
return this.seatingCap;
}
@Override
String getDiscription() {
return this.name;
}
}
Null Vehicle Class -:
public class NullVechicle extends Vehicle
{
//default behavior or do nothing behaviour
//invalid type of vechicle
//invalid property display
public NullVechicle()
{
this.name = "invalid Type";
this.seatingCap = 0;
}
@Override
String getDiscription() {
return "Not a valid vechicle type";
}
@Override
int getSeatingCapacity() {
return 0;
}
}
So these are basic vehicles which are available in vehicle rental shop . Now let’s
have a look on Vehicle Factory class.
Output -:
So let’s see the working of this code . Suppose you are a customer and you need
Hondacity as your desired vehicle so you will ask Vehicle factory for it .
Now the Vehicle Factory will check if any of the object matches with the type of
vehicle you want , if found it will return you that object ,
otherwise it will give you some relevant information about unavailability of
vehicle .
To answer this question let’s consider our client and vehicle factory classes .
Suppose we don’t have any Null Vehicle class so in the vehicle factory class
we will return null if vehicle is unavailable . But when a client makes a request
for any unavailable vehicle then our code will throw a null pointer exception .
import java.util.Objects;
So in this way we are not getting any relevant information about unavailability of
any vehicle . So to resolve this we will impose some conditions .
public class Main {
public static void main(String[] args)
{
Vehicle hondacity = VehicleFactory.getVehicle("xyz",5,"hondacity");
if(hondacity!=null)
{
System.out.println(hondacity.getSeatingCapacity());
System.out.println(hondacity.getDiscription());
}
else {
System.out.println("Vehicle is unavailable");
}
}
}
This will solve our problem of not getting relevant information of unavailability
but in real life implementations we have thousands of product classes
which are going to have thousands of methods and writing if conditions for each
one of them will make our code complex . So to resolve this issue we have
made Null Vehicle class which is going to give us relevant information about
unavailability without making our code complex .
-----------------------------------------------------------------------------------
------------------------------------------------------