Creational Design Patterns

Creational design patterns provide the best way in which an object can be instantiated.

Creational Design Patterns

Creational Design Patterns

     

Creational design patterns provide the best way in which an object can be instantiated. These describes the best way to CREATE object instances . Everyone knows the object instance in Java can be created using a new operator.

Employee emp = new emp ();

The new Operator creates the instance of an object, but this is complex-coding. Creating good application is difficult and so on, complex coding is the last thing one should do. At the present time the very nature of the object, which is created, can change according to the nature of the program. In such scenarios, we can use this pattern to give you more flexible approach.

There are five types of Creational Patterns.

  1. Factory Design Pattern
  2. Abstract Design Factory Pattern
  3. Builder Design Pattern
  4. Prototype Design Pattern
  5. Singleton Design Pattern

Factory Design Pattern :

Let us suppose we have a super class and a sub-class, based on data provided, we have to return the object of one of the sub-classes, we can use factory design pattern. Let's see an example to understand this pattern.

How a Factory Works

In this figure, Employee is a base class and classes Male and FeMale are derived from it. The Factory is a class that decides which of these subclasses to return depending on the arguments you give it. On the right, we define a getEmployee method to be one that passes in some value Name and Sex, and that returns some instance of the class Employee. Which one it returns doesn't matter to the programmer since they all have the same methods, but different implementations. How it decides which one to return is entirely up to the factory. It could be some very complex function but it is often quite simple

Example: Let's suppose an application asks for entering the name and sex of a Employee. If the sex is Male (M), it displays welcome message saying Hello Mr. <Name> and if the sex is Female (F), it displays message saying Hello Miss <Name>.

Sample Code:

Package com.javasree.rajnish;

class Employee {

  //** name string
  public String name;
  // **gender : Male or Female
  private String gender;
  public String getName()
   {
  return name;
  }
  public String getGender()
  {
  return gender;
  }
}

//** End of class

This is a simple class Employee having methods for name and gender. Now, we have two sub-classes, Male and Female, which will print the welcome message on the screen.

class Male extends Employee
{
   public Male(String fullName)
  {
  System.out.println("Hello Mr. "+fullName);
  }
}

//** End of class

Also, Second class is Female

class Female extends Employee {

  public Female(String fullNname)
   {
   System.out.println("Hello Miss. "+fullNname);
   }
}

//** End of class

Now, we have to create a client, or a SalutationFactory which will return the welcome message depending on the data provided.

public class SalutationFactory
{

   public static void main(String args[])
  {
  SalutationFactory factory = new SalutationFactory();
  factory.getEmployee(args[0], args[1]);
  }
  public Employee getEmployee(String name, String gender)
  {
  if (gender.equals("Male"))
  return new Male(name);
  else if(gender.equals("Female"))
  return new Female(name);
  else
  return null;
  }

}

//** End of class

This class accepts two arguments from the system at runtime and prints the names.

Compile this Code:

Running this Code: with two argument Name: Rajnish and Sex: Male 0

Output: Hello Mr. Rajnish

1

Again Running this Code: with two argument Name: Bhaskar and Sex: FeMale

Output: Hello Miss. Bhaskar

When to use a Factory Pattern? 2

The Factory patterns can be used in following cases:

  1. When a class does not know which class of objects it must create.
  2. A class specifies its sub-classes to specify which objects to create.
  3. In programmer's language (very raw form), you can use factory pattern where you have to create an object of any one of sub-classes depending on the data provided.

Download Source Code