top of page
hand-businesswoman-touching-hand-artificial-intelligence-meaning-technology-connection-go-

Creational Design Patterns

Hello everyone. I am going to share my knowledge about Creational pattern.

What is Creational Design Patterns

Creational design patterns are concerned with the way of creating objects. They reduce complexities and instability by creating objects in a controlled manner.

There are different types of Creational Design Pattern:

  • Builder Pattern

  • Singleton Pattern

  • Prototype Pattern

  • Factory Method Pattern

  • Abstract Factory Pattern

  • Object Pool Pattern


Builder Design Pattern – Constructs complex objects using step-by-step approach

Singleton Pattern – Ensures that at most only one instance of an object exists throughout application or we can says it defines a class that has only one instance and provides a global point of access to it.

There are two types of singleton design pattern such as

  • Early Instantiation: creation of instance at load time.

  • Lazy Instantiation: creation of instance when required.

Factory Method – Creates objects of several related classes without specifying the exact object to be created . It define a abstract class for creating an object but let the subclasses decide which class to instantiate.

Abstract Factory – Creates families of related dependent objects

Prototype Pattern- In this Pattern the cloning of an existing object instead of creating new one and can also be customized as per the requirement.

Object Pool Pattern-In Object Pool Pattern, basically we use to reuse the object that are expensive to create. An Object pool is a container which contains a specified amount of objects. Objects in the pool have a lifecycle such as creation, validation and destroy.


I am going to discuss about Builder Design pattern and Singleton Pattern


What is Builder Design Pattern


Builder is a creational design pattern which says that construct a complex object from simple objects using step-by-step approach.

This type of design pattern comes under creational design pattern as this pattern provides one of the best ways to create an object.

Builder doesn't require products to have a common interface.

When object can't be created in a single step of a complex object, in that case we use this builder design pattern.

To solve some of the problems with Factory and Abstract Factory design patterns when a object contains a lot of attributes, in that case builder pattern was introduced.




Why is Builder Design Pattern Used


  • This Builder Design pattern is used to separate the construction of a complex object from it's representation.

  • This Encapsulate code for construction and representation.

  • It provides better control over construction process.

  • It support to change the internal representation of the objects.

  • It allows for creation of immutable objects.

  • This Builder pattern improves code readability.

  • It gives scalability to the program to add new parameters to the object without changing the existing code.

  • The Builder Pattern allows us to use single Builder class to avoid constructor overloading.

  • It provides control over the construction process.




Disadvantages of the Builder Pattern


  • Builder classes must be mutable.

  • A concreteBuilder must be created for each type of product.

  • It may complicate dependency injection.

  • It can increase complexity to the codebase.

  • It might lead to tight coupling between the build and product classes


Sequence Diagram for the Builder Design Pattern of a UML class



In the above example, the Director class doesn't create and assemble the ProductA1 and ProductB1 objects directly. Instead, the Director refers to the Builder interface for creating the parts of a complex object, which makes the Director independent of which concrete classes are instantiated .The Builder1 class implements the Builder interface by creating and assembling the ProductA1 and ProductB1 objects.The Director object calls buildPartA() on the Builder1 object, which creates and assembles the ProductA1 object. Thereafter, the Director calls buildPartB() on Builder1, which creates the ProductB1 object.


Example of Builder pattern in Java


public class Student {

private String name;

private String place;

private int id;

private int phnNo;


private Student(StudentBuilder builder) {

this.name = builder.name;

this.place = builder.place;

this.id = builder.id;

this.phnNo = builder.phnNo;

}


public static class StudentBuilder {

private String name;

private String place;

private int id;

private int phnNo;


public StudentBuilder setName(String name) {

this.name = name;

return this;

}


public StudentBuilder setPlace(String place) {

this.place = place;

return this;

}


public StudentBuilder setId(int id) {

this.id = id;

return this;

}


public StudentBuilder setPhnNo(int phnNo) {

this.phnNo = phnNo;

return this;

}


public Student build() {

return new Student(this);

}

}


@Override

public String toString() {

return "Student [name=" + name + ", place=" + place + ", id=" + id + ", phnNo=" + phnNo + "]";

}

}


public class Main {

public static void main(String[] args) {

Student myStudent = new Student.StudentBuilder()

.setName("John")

.setPlace("NY")

.setId(32)

.setPhnNo(2017560912)

.build();


System.out.println(myStudent);

}


Code Explanation :-

Student Class:

This Student class Contains private fields for its attributes.

The constructor is private and takes parameter.

The StudentBuilder class has methods to set different attributes and a build() method that creates a Student instance.


StudentBuilder Class:

It has the same fields as the Student class .

Individual setter method returns the StudentBuilder itself, with the process of method chaining.

The build() method creates a Student object using the builder's attributes.


Usage:

In Main class, we are creating a Student object using the StudentBuilder .

The toString() method of the Student class is overridden .

Output :-

Student [name=John, place=NY, id=32, phnNo=2017560912]


Singleton Pattern



In Singleton pattern, it saves memory because object is not created at each request. Only single instance is reused again and again.

  • The singleton pattern allows classes to

  • Ensure they only have one instance

  • Provide easy access to that instance

  • Control their instantiation


When to use Singleton Design Pattern

There must be exactly one instance of a class and it must be accessible to clients from a well-known access point.

When the sole instance should be extensible by subclassing and clients should be able to use an extended instance without modifying

Singleton classes are used for logging, driver objects, caching, and thread pool, database connections.

Thank You.

9 views0 comments

Comentários

Avaliado com 0 de 5 estrelas.
Ainda sem avaliações

Adicione uma avaliação
bottom of page