top of page
Search
Abhinav Tripathi

Creational Design Patterns


This design pattern is all about the class instantiation.These design patterns tries to create objects in a manner suitable to the situation.

Again there are two sub-categories into it

1) Object-Creational Design Pattern

2)Class-Creational Design Pattern

Object-Creational DP Class-Creational DP

1) Abstract Factory DP 1)Factory Method DP

2)Builder DP

3)Prototype DP

4)Singleton DP

Factory Method Design Pattern:

It is used to instantiate an object from one among a set of classes based on some logic.It is also used instead of constructor or in addition to existing constructor.

eg .sample program for it

public interface Pet{ public String petSound();}

public class Dog implements Pet{ public String petSound(){return "Bow Bow" } }

public class Cat implements Pet{ public String petSound(){return "Meaw Meaw" } }

public class PetFactory{ public Pet getPet(String petType){ if("Bow Bow".equals(petType)){pet=new Dog(); else if("Meaw Meaw.equals(petType){ pet=new Cat();}") } return pet}}

publi class SampleFactoryMethodTest

{

public static void main(String args[])

{

PetFactory factory=new PetFattory();

Pet pet=factory.getPet("Bow");

System.out.println(pet.petSound());

}

}

When to use Factory Method Design Pattern

1) when a class cant guess the type of the object it is supposed to create.

2)When a class wants its subclass to be the ones to specific the type of a newly created object.

3)when we want to localize the knowledge of which class gets created.

7 views0 comments

Recent Posts

See All
bottom of page