Monday, February 28, 2011

Design Patterns I Used

Java has several design patterns Singleton Pattern being the most commonly used.
Java Singleton pattern belongs to the family of design patterns, that govern the instantiation process.
What it says??

This design patterns says that at any time there can be only one instance of object created by the JVM.

How it is done??

The class’s default constructor is made private, which prevents the direct instantiation of the object by others (Other Classes).
A static modifier is applied to the instance method that returns the object as it then makes this method a class level method that can be accessed without creating an object.(called factory method)

One such scenario where it might prove useful is when we develop the help Module in a project.

Singletons can be used to create a Connection Pool. If programmers create a new connection object in every class that requires it, then its clear waste of resources. In this scenario by using a singleton connection class we can maintain a single connection object which can be used throughout the application.

No comments: