:::: MENU ::::
Browsing posts in: design patterns

When is a Singleton not a Singleton?

In this article, Joshua Fox describes those phenomenas and how to avoid them. After discussing how to implement the Singleton, Joshua goes over the sometimes surprising causes for the phenomena one by one, showing you how they occur and how you can avoid making those mistakes.

I hope that in the process you will learn about classloading, multithreading, distributed systems, Design Patterns, and other interesting topics, as I did.

Design Patterns – Head First

Here you could get more information related with design patterns.


Please take a look at it.

Singleton – Pattern Desing

public class Singleton {
    * The constructor could be made private
    * to prevent others from instantiating this class.
    * But this would also make it impossible to
    * create instances of Singleton subclasses.
   protected Singleton() {
     // ...
    * A handle to the unique Singleton instance.
   static private Singleton _instance = null;
    * @return the unique instance of this class.
   static public Singleton instance() {
      if(null == _instance) {
         _instance = new Singleton();
      return _instance;

Decorator Pattern JAVA

Decorator Pattern


Attach additional responsibilities or functions to an object dynamically or statically. Also known as Wrapper.

Where to use & benefits


A JScrollPane object can be used to decorate a JTextArea object or a JEditorPane object. A window can be decoratedwith different borders like BevelBorder, CompoundBorder, EtchedBorder TitledBorder etc. These border classes working as decorators are provided in Java API.

Decorator pattern can be used in a non-visual fashion. For example, BufferedInputStream, DataInputStream, and CheckedInputStream are decorating objects of FilterInputStream class. These decorators are standard Java API classes.

To illustrate a simple decorator pattern in non-visual manner, we design a class that prints a number. We create adecorator class that adds a text to the Number object to indicate that such number is a random number. Of course we can subclass the Number class to achieve the same goal. But the decorator pattern provides us an alternative way.

import java.util.Random;
class Number {
   public void print() {
       System.out.println(new Random().nextInt());

class Decorator {
    public Decorator() {
        System.out.print("Random number: ");//add a description to the number printed
        new Number().print();

class SubNumber extends Number{
    public SubNumber() {
       System.out.print("Random number: ");

class Test {
    public static void main(String[] args) {
        new Decorator();
        new SubNumber();
java Test
Random number: 145265744
Random number: 145265755