• Skip to primary navigation
  • Skip to content
  • Skip to primary sidebar
  • Skip to footer
  • Core Java
  • Design Patterns
  • JSP
  • Servlets
  • Building Tools
  • jQuery
  • Spring
  • Hibernate
  • Mongo DB
  • More
    • HTML
    • SCJP
    • AJAX
    • UML
    • Struts
    • J2EE
    • Testing
    • Angular JS

J2EE Reference

  • Home
  • About Us
    • Java Learning Centers
  • Contact Us

Singleton

December 16, 2010 By j2eereference Leave a Comment

Singleton design pattern

Sometimes we want just a single instance of a class to exist in the system

We use the Singleton pattern for achieving the below.

To make sure that there can be one and only one instance of a class and provide global point of access to it.

The Singleton pattern ensures that only one instance of a class is created.

All objects that use an instance of that class use the same instance.

Here is the implementation of a Singleton design pattern:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
public class Singleton
{
private static Singleton instance;
//To make sure that multiple objects are getting created with new keyword
private Singleton()
{ }
//To get the instance of the singleton class
public static synchronized Singleton getInstance()
{
if (instance == null)
{
instance = new Singleton();
}
return instance;
}
 
public Object clone() throws CloneNotSupportedException
        {
             /*For preventing the singleton object from being cloned*/
            throw new CloneNotSupportedException();
        }
 // other methods here
}

The singleton instance is not created until the getInstance() method is called for the first time. This technique ensures that singleton instances are created only when needed.

Recommended Books

  

Related Posts

  • Observer pattern
  • Façade pattern
  • What is Design pattern ?
  • Design pattern – Service Locator

Filed Under: Design Patterns

Reader Interactions

Leave a Reply Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

FOLLOW US ONLINE

  • View J2eereference-166104970118637’s profile on Facebook
  • View j2eereference’s profile on Twitter
  • View j2eereference’s profile on LinkedIn

Subscribe by email

Recent posts

  • Java Buzzwords
  • Anonymous Inner Class in Java
  • Network Programming – java.net Package
  • Java Regular Expressions
  • Method Local Inner Class in Java
  • URL Processing in Java
  • Iterator Design Pattern Implementation using Java
  • Strategy Design Pattern Implementation using Java
  • Decorator Design Pattern
  • Adapter Design Pattern Implementation using Java
  • JSF Composite Components
  • JSF UI Components
  • What is JavaServer Faces (JSF)?
  • GOF Design Patterns
  • History and Need for Design Patterns

Footer

Core Java
Design Patterns
JSP
Servlets
HTML
Building Tools
AJAX
SCJP
jQuery
Testing
Spring
UML
Struts
Java Centers
Java Training
Home
About Us
Contact Us
Copyright © j2eereference.com. All right reserved.