Creational Design Patterns

Factory Pattern, Abstract Factory Pattern, Singleton Pattern


Factory Pattern

It is responsible for creating the objects. As name depicts, it is a factory for creating any specific kind of objects. It abstracts the user from the details of creation mechanism, and also abstracts the actual implementation class of instantiated object. The only requirement is that the ‘object to create’ should be of some already defined type. The actual implementation may differ based on the current scenario.

Example

  • Maruti Car Factory Object: We can ask this object to return us a Maruti car based on any runtime parameter which defines its model like Wagon R, or Esteem. It is the responsibility of this factory object to return us required car implementation based on specific parameters.
  • Row Renderer Factory Object: We can ask it to return the renderer like structured row renderer, or a designable row renderer.


When to Use
  • We can’t decide in advance that which class of object we may require at runtime, however type is known. So the type is known but the implementing class may vary depending upon runtime scenario.
  • We want to localize the knowledge of object creation into some class, so that responsibility resides with one object only.
  • We want to make the system configurable and extensible and so want to program to interface not to implementation.


What are Core Design Patterns

What are design patterns?

  • Recurring solutions to design problems
  • Constitutes a set of rules to accomplish certain task in software development realm
  • Convenient ways of reusing object oriented code between projects and between programmers.
  • These describe how different objects should interact with each other without getting entangled with others data model and methods.
  • Started from early 1980, formally recognized in 1990(Helm) and 1992 (Enrich Gamma)
  • Suggests to program to an interface not to an implementation
  • Recommends composition over inheritance
Types of Core Design Patterns
  • Creational – These help us to create the objects in an optimized and flexible way, instead of directly instantiating the instances and give the flexibility to create the objects depending on the scenario.
  • Structural – Help us to compose a number of objects into a large structure in optimized way.
  • Behavioral – It defines and helps us to define the communication between objects and flow of control in the system.
Specific Design Patterns are described separately with other postings.