Friday, April 1, 2011

Design Patterns in Java

This blog is going to scratch surface of vast and subjective topic called design patterns .To use them is not science but it is an art .To judge the situation and put effort to create generic code is important otherwise one might waste lot of time in creating world class design of piece code which is going to be used once in a while .So it is combination of proper judgment , situation , time , resources available , importance of functionality etc .
Many people do not understand very concept of usage of them , most of them understand only singleton pastern .As usage is situation based it is very difficult to teach the proper situation in which one has to implement one .Many time we have to use combination of many design pasterns to tackle the situation .To use the cocktail of such pasterns need much experience and keen mind .But even though design patterns are used and other developers do not understand them then they can not maintain that code .So point is this more intellectual solution could confuse other developed if they are not aware of design patterns and ultimate delay in bug fixing.


What is design pattern and why to use them ?

What is a design pattern?

If a problem occurs over and over again, a solution to that problem has been used effectively. That solution is described as a pattern.
The design patterns are language-independent strategies for solving common object-oriented design problems.

What are the advantages?

  • Reusable foolproof Design , and code
  • Time saving as they are tested strategies for particular situation .
  • Change extensive and flexible .
  • Easy maintenance of code
  • Better clean code improve communication between developers who work on it.

What are the disadvantages?
  • Complexity of code as solution is generalized.
Design Pattern Categories

  • Creational Design Pattern
Deals with creation of instance of object . Whole idea revolves around theme to control
instantiation of object .

Exp. Window window = new Window();
  • Structural Design Pattern
  • Behavioral Design Pattern
Creational Design Pattern

  • Factory
  • Abstract factory
  • Builder
  • Singleton/Multiton
  • Prototype
  • Lazy initialization/Eager initialization
1. Factory Method

Concept

  • It deals with the problem of creating objects without specifying the exact class of object that will be created.
  • This return the children classes in form of parent class .
  • It uses inheritance .(subclasses + interface )
  • Factory class have only one instance .

Example -




Use –
  • Flexible way to create object without bothering its actual class.


2. Abstract factory

Concept –

Provide interface for creating families of related or dependent objects without specifying their concrete class.
Concrete factory have only one instance .

Example –




Use –
Abstract factory isolates concrete class to be generated .
Creator don’t have idea which kind of class of object it must create
Return several group of related objects .

3. Builder

Concept –

To build Object part by part than creating whole at a time .
The intention is to abstract steps of construction of objects so that different implementations of these steps can construct different representations of objects.

Example –


Use –
  • Let you vary product internal representation
  • Isolates code for construction and representation
  • Finer control over construction process.


4. Prototype

Concept -

Create new Object by copying or cloning the prototype object.
Reduce the cost of creation
Cloning may use shallow copy / deep copy of cloning
May use serialization for deep copy .

Example –




Use –
  • To reduce for creation of complex object .
  • There is an object instance in memory almost similar to object wanted .Deep Clone that object and set new few fields and use for further purpose.
5. Singleton

Concept

Ensure that class has only one instance and provide global point to access it .
Most of times services and factory methods uses this form .

Example-




Use –

While creating factories for object creation
Creating services like file manager , process manager etc .

7. Lazy initialization/Eager initialization

Concept -

  1. Create new object on first request for the object .
  2. Create object before the first request for the object .

Comparison

Lazy

Eager

public class View{

private int courseCount;

private boolean isCounted;

public int countNbCourses()

{

if(!isCounted){

courseCount = countCourses();

}

return courseCount;

}

}

public class View{

private int courseCount;

private boolean isCounted;

public View

{

courseCount = countCourses();

}

public int countNbCourses()

{

return courseCount;

}

}



Summary of creational patterns

Pattern Name

Generalized Situation

Factory Pattern

Used to choose and return instance of class in form of super class / interface .

This uses inheritance .

Abstract Factory Pattern

Return one of several group of classes .

Return object based on chosen factory .

Uses Factory pattern internally .

Builder Pattern

Assemble number of objects to create new composite object this based on data it represent .

Many time objects are assembled using factory .

Prototype Pattern

Copies or clones existing instance than creating new expensive one .

Singleton pattern

Ensure there is only one instance of object and provide global access to that point .




Structural Patterns

  • Adapter
  • Bridge
  • Composite
  • Proxy
  • Façade
  • Decorator

Behavioral Pattern

  • Chain of responsibility
  • Command
  • Interpreter
  • Iterator

Reference -

Design Patterns - Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides

No comments: