Creational Design Patterns

Creational design pattern abstracts the instantiation process. It helps in making the system independent of how its objects are created, composed and represented. Object creation is a complex task. To resolve this problem we have design patterns. You can read more about

Hard coding of the program is not always a good approach for programming. We create the instance using a new keyword. The nature of objects usually changes with respect to the nature of the program. Thus design pattern is essential for instantiation of the object in the application.

Let us see the various different creational design patterns are and how to use these design patterns:

Factory Method Pattern

In factory method design pattern, we define an interface and abstract classes for creating object and we let the subclasses to decide which class to instantiate. In short subclasses are responsible for instantiating the subclasses.
The major advantage of using such a pattern is that subclasses have an option to choose the interface or abstract class for creating object.
Secondly the codes are loosely-coupled, as the class which needs the object creation code will only implement or extend the interface or class.
Let us understand it with help of example

class PGStudent extends Student
{
  long numberOfLectures;
  PGStudent(String name ,String PRN, String specialization,long numberOfLectures)
 {
  super(name,PRN,specialization);
  this.numberOfLectures = numberOfLectures;
 }
  void display()
 {
   super.display();
   System.out.println("No of lectures for PG student : "+numberOfLectures);
  }
 }
  class UGStudent extends Student
  {
    long numberOfLectures;
    UGStudent(String name ,String PRN, String specialization,long numberOfLectures)
    {
         super(name,PRN,specialization);
         this.numberOfLectures = numberOfLectures;
     }
  void display()
   {
      super.display();
      System.out.println("No of lectures for PG student: "+numberOfLectures);
    }
 }
  class GetStudentFactory{
   Public Student getPlan(String studentType)
 {
    if(studentType==null)
    return NULL;
    if(studentType.equalIgnoreCase(“PGStudent”)
    {return new PGStudent;}
   }
  else if(studenttype.equalIgnoreCase(“UGStudent”)
{
   Return new UGStudent;
  }
}

Abstract Factory Pattern

Abstract Factory class is that which defines an interface or abstract class for creating families of related dependent or independent objects. It is similar to Factory method but it produces a collection of the classes or objects.
It is very useful when families of related objects need to be created.System requirement of multiple objects can be fulfilled with the help of abstract factory pattern for creation of family of objects.

Singleton Pattern

Singleton design pattern in Java is used to define a class that has only one instance .The instance can be created at the run time or compile time.
There are two possible ways through which the objects can be created:
  • Early instantiation : Instantiation at the load time.
  • Lazy instantiation: Instantiation whenever required.

Singleton saves memory as the objects are not created at every request. Same object is used every time.
Example of the singleton pattern using JAVA language

class A{ //definition of class A
 private static A obj=new A();
 private A(){}
 public static A get A(){
 return obj;
   }
 public void doSomething(){

   }
}

Builder Pattern

Builder pattern is the pattern through which complex objects are created from simple objects using step by step process.
Basically it provides clear separation between construction and representation of objects. Construction of the code can be done easily with help of Builders pattern. It will support if there is any change in the representation of objects.

Object Pool Pattern

Creation of objects is costly affair. So the basic principle behind the object pool pattern is that an object container has been created and whenever any of the objects are required, we can take object from that container and once there is no object in the container then we put more objects in the container.
A pool is basically used because it manages objects properly.
Some of the advantages of the object pool patterns are as follow:
  • It helps in good performance of the application which has used the object pool pattern.
  • It is the most effective when a bulk of the object’s initialization is required in the application.
  • With the help of Object pool design pattern, we can easily reuse the objects.
  • Object pool design pattern restricts the developer’s use objects.

Read More:
Method Overloading V/s Method Overriding In OOPs
Pros and Cons of Inheritance in OOPS
Advantages & Disadvantages of Polymorphism in Object Oriented Programming

Copyright © ianswer4u.com