0% found this document useful (0 votes)
18 views

Design Patterns

Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Design Patterns

Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

design patterns:

singleton design pattern :


The Singleton Design Pattern is a creational design pattern that ensures a class
has only one instance and provides a global point of access to it.
This pattern is particularly useful when exactly one object is needed to
coordinate actions across the system

Key Concepts of Singleton Method

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.

public class Singleton {


private static Singleton instance;

private Singleton() {
// Private constructor to prevent external instantiation
}

public static Singleton getInstance() {


if (instance == null) {
instance = new Singleton();
}
return instance;
}

// Other methods and properties can be added here


}
-----------------------------------------------------------------------------------
-----------

The Factory Design Pattern is a creational design pattern that provides an


interface for creating objects without specifying their concrete classes.
It encapsulates the object creation logic within a separate class, known as the
factory, which provides a centralized point for object instantiation.
This pattern is particularly useful when the exact types of objectsto be created
may vary or need to be determined at runtime, enabling flexibility and
extensibility in object creation

Implementation of Factory Design Pattern


To understand implementation of Factory Design Pattern let’s consider our vehicle
rental example that I gave you above 👆.

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.

Vehicle Factory Class -:


import java.util.Objects;

public class VehicleFactory


{
public static Vehicle getVehicle(String Type,int Cap,String name)
{
if(Objects.equals(Type, "Bike"))
return new Bike(name,Cap);
else if (Objects.equals(Type, "Car"))
return new Car(name,Cap);
else if(Objects.equals(Type, "Bus"))
return new Bus(name,Cap);
return new NullVechicle();
}
}
Now this is our Vehicle Factory which will give us our desired vehicle .
Client Class -:

public class Main {


public static void main(String[] args)
{
Vehicle hondacity = VehicleFactory.getVehicle("Car",5,"hondacity");
System.out.println(hondacity.getSeatingCapacity());
System.out.println(hondacity.getDiscription());
System.out.println();
Vehicle cycle=VehicleFactory.getVehicle("Cycle",2,"Bicycle");
System.out.println(cycle.getSeatingCapacity());
System.out.println(cycle.getDiscription());
}
}
This is our Client class where we can make request for our desired vehicle .

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 .

Now the point is why do even need Null Vehicle class ?

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;

public class VehicleFactory


{
public static Vehicle getVehicle(String Type,int Cap,String name)
{
if(Objects.equals(Type, "Bike"))
return new Bike(name,Cap);
else if (Objects.equals(Type, "Car"))
return new Car(name,Cap);
else if(Objects.equals(Type, "Bus"))
return new Bus(name,Cap);
return null;
}
}
public class Main {
public static void main(String[] args)
{
Vehicle hondacity = VehicleFactory.getVehicle("xyz",5,"hondacity");
System.out.println(hondacity.getSeatingCapacity());
System.out.println(hondacity.getDiscription());
}
}

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 .

So this was complete implementation of Factory Design Pattern .

Real Life Examples of Factory Design Pattern


Database Connection Factories: Database connection factories are a common example
of the Factory pattern. The factory class can examine the parameters (such as
database type, credentials, etc.)
and create the appropriate database connection object, hiding the specific
implementation details from the client.

-----------------------------------------------------------------------------------
------------------------------------------------------

You might also like