Friday, 14 March 2014

Java Design Patterns Part - 1

Design Pattern In Java

Web Service                                                                                              Java and It's Version - Features

Question:- What is design pattern?

Answer:- As per GOF(Gang of Four), "Design Patterns: Elements of Reusable Object-Oriented Software"

It help to provide solutions to common software design problems. Its a kind of a powerful tool for software development. Its an independent strategies for solving common object-oriented design problems. It explains a general design that addresses a recurring design problem in object-oriented systems.Design pattern is nothing but a general resuable solution to commonly occurring problem within the given context in software design.

History of software patterns :-
  • 1987 – Ward Cunningham and Kent Beck used Alexander's ideas to develop a small pattern language.
  • 1990 – The Gang of Four ( Erich Gamma, Richard Helm, Ralph Johnson & John Vlissides) begin work compiling a catalog of design patterns.
  • 1991 – Bruce Anderson gives first patterns Workshop at OOPSLA
  • 1993 – Kent Beck and Grady Booch sponsor the first meeting – what is now known as Hillside group
  • 1995 – The Gang of Four published the design pattern book.
  • 2002 – Martin Fowler published the book “Patterns of Enterprise Application Architecture”.

Advantage of Design pattern
  • Reusable in nature.
  • Provide the solution that help to define the system architecture
  • Provide transparency towards application design
  • Easily maintenable
  • Reducing complexity by providing the clarity towards the system architecture

Design patterns is mainly categorized in three parts.
  • Creational : - It concerns the process of object creation
  • Structural :- This deals with the composition of classes & objects.
  • Behavioral :- This Characterize the ways in which classes/objects interact and distribute responsibility.
Creational

1) Singleton- Only one instance of the class :- This design pattern is recognized by creational methods returning the same instance everytime.

For example :

a) java.lang.Runtime#getRuntime()
b) java.awt.Desktop#getDesktop()

2) Abstract Factory: An abstract factory to produce factory of objects. This design pattern is having one level of abstraction higher than factory design pattern. This pattern returns the factory of classes. In case of factory pattern, it returns object of one of the subclass in several subclasses and this(Abstract factory) design pattern will return factory which later will return one of the sub classes. This design pattern is recognized by creational methods returning the factory itself which in turn can be used to create another abstract/interface type. Finally we can tell that this design pattern create factory which later creates products.

a) javax.xml.parsers.DocumentBuilderFactory#newInstance()
b) javax.xml.xpath.XPathFactory#newInstance()
c) javax.xml.transform.TransformerFactory#newInstance()

For example : 

 DocumentBuilderFactory is the best example of Abstract design pattern which creates a factory which is DocumentBuilder which later help us to create Document objects. Here we can find that DocumentBuilderFactory is the AbstractFactory where as DocumentBuilder is the Factory and Document is the Product.

DocumentBuilderFactory docBuildAbstractFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuildFactory = docBuildAbstractFactory.newDocumentBuilder();
Document doc = docBuildFactory.newDocument();

3)Factory A factory that produces objects. This design pattern is used to create instance of our classes which is somehow similar in features. For example :- If we will take “Human” class and want to create objects of “man” and “woman” classes dynamically which is subclass of human then we can go with this design pattern because man & woman classes are somehow similar to human class in terms of features.This design pattern is recognized by creational methods returning an implementation of an abstract/interface type. Finally we can tell that this design pattern creates products instead of factory.

a) java.text.NumberFormat#getInstance()
b) java.util.Calendar#getInstance()
c) java.nio.charset.Charset#forName()
d) java.util.ResourceBundle#getBundle()
e) java.net.URLStreamHandlerFactory#createURLStreamHandler(String) (Returns singleton object per protocol)

Difference between AbstractFactory & Factory design pattern

[I] AbstractFactory uses compositionto delegate responsibility of creating object to another class while Factory pattern uses inheritanceand relies on derived classes or sub classes for object creation.

[II] AbstractFactory creates Factory which later creates objects of their sub classes where as Factory creates products instead of factory.

4) Builder :-Its a pattern used to construct a complex object in step by step process and at last or final step will return the object. These separates the construcution of complex objects from their representation. It is recognized by creational methods returning the instance itself. 

For example :

a) java.lang.StringBuffer#append()
b) java.lang.StringBuilder#append()
c) java.nio.ByteBuffer#put() (also on IntBuffer, CharBuffer, ShortBuffer, LongBuffer, FloatBuffer and DoubleBuffer)
d) All implementations of java.lang.Appendable

According to Gang of Four(Gof):- The Builder Pattern separates the construction of a complex object from its representation so that the same construction process can create different representations.For example, we can think for making of a Mobile Handset. Making of a cellphone involves various steps such as embedding the processor chip, Ram, camera, screen, and supporting softwares. Finally we will get a complete Mobile phone object nothing but a complex object obtained by going through step by step process. With the help of the same process, we can make cellphones having different features.

Question:- When to use Builder design pattern?
Answer :- This design pattern is useful when we need to do lots of things to build an object. 

For example : 

In the creation of DOM object, we have to create plenty of nodes and attributes to get our final object.

    5) Prototype Prototype meaning itself is telling that making a clone. Prototype design pattern is used when an application needs to create many instances of a class, which is almost same as original one. So this we can do using clone. If the object creation is a expensive process then we can clone that object. While appling cloning, we should know which cloning we have to use Shallow or Deep cloning

    For example:-
      a) java.lang.Object#clone() (the class has to implement java.lang.Cloneable)

No comments:

Post a Comment