Monday, May 14, 2012

Factory Method Pattern


Introduction

Suppose we have a super class (abstract class or interface) and any number of sub classes. Based on some parameters we have to return an object of any sub class without knowing the logic inside. So in this case Factory Method Pattern can be used.

Example

By assuming you are familiar with the playing cards, I'm going to illustrate the example using playing cads. So you might know in the playing cards there are 4 kinds of kings. So lets create an interface name is King and 4 classes for 4 kinds of kings.  Lets assume passing some parameters ("C", "D", "H", "S") we want to get the suit of the object. Following are the created classes.

King.java

package com.blog.patterns.factorymethod;

/**
 * @author uditha
 */
public interface King {
 
 public String getSuit();

}

Club.java

package com.blog.patterns.factorymethod;

/**
 * @author uditha
 */
public class Club implements King {

 @Override
 public String getSuit() {
  return "Club";
 }

}

Diamond.java

package com.blog.patterns.factorymethod;

/**
 * @author uditha
 */
public class Diamond implements King {

 @Override
 public String getSuit() {
  return "Diamond";
 }
 
}

Heart.java

package com.blog.patterns.factorymethod;

/**
 * @author uditha
 */
public class Heart implements King {

 @Override
 public String getSuit() {
  return "Heart";
 }
 
}

Spade.java

package com.blog.patterns.factorymethod;

/**
 * @author uditha
 */
public class Spade implements King {

 @Override
 public String getSuit() {
  return "Spade";
 }
 
}

So far so good. Now we want the factory class, passing some parameter and get the instance of King. Following is the factory class.

Factory.java

package com.blog.patterns.factorymethod;

/**
 * @author uditha
 */
public class Factory {

 public King getKing(String key) {
  if ("C".equals(key)) {
   return new Club();
  } else if ("D".equals(key)) {
   return new Diamond();
  } else if ("H".equals(key)) {
   return new Heart();
  } else if ("S".equals(key)) {
   return new Spade();
  } else {
   throw new RuntimeException("No king found for the key : " + key);
  }
 }
 
}

If you wish to test this pattern I created a main class for you.

Main.java

package com.blog.patterns.factorymethod;

/**
 * @author uditha
 */
public class Main {

 /**
  * @param args
  */
 public static void main(String[] args) {
  Factory factory = new Factory();
  King king = factory.getKing("C");
  System.out.println(king.getSuit());
  king = factory.getKing("D");
  System.out.println(king.getSuit());
  king = factory.getKing("H");
  System.out.println(king.getSuit());
  king = factory.getKing("S");
  System.out.println(king.getSuit());
  king = factory.getKing("K");
  System.out.println(king.getSuit());
 }

}

Friday, May 4, 2012

Singleton Pattern

Introduction

Singleton design pattern is the most simplest design pattern among all the other design patterns. This pattern gives you a single instance for you throughout the system or program.

Example

Suppose you have a class , say Singleton, and you want to have only one instance throughout your program. Is it clear? Just one instance. No more than one instance. So in this case you are aware to create only one instance throughout the program. Ok, in this case you have to use singleton design pattern. Let's see how it should be done.

Singleton.java

package com.blog.patterns.singleton;

/**
 * @author uditha
 */
public final class Singleton {

 private static final Singleton INSTANCE = new Singleton();
 
 private Singleton() {
  System.out.println("Singleton instance is created.");
 }
 
 public static Singleton getInstance() {
  return INSTANCE;
 }
 
 @Override
 public Object clone() throws CloneNotSupportedException {
  throw new CloneNotSupportedException("Clone is prohibited.");
 }
 
}

Discussion

In this class you can see the constructor is private.
 private Singleton() {
  System.out.println("Singleton instance is created.");
 }
That means constructor is not visible to out side world. It is the tricky part of this pattern to prevent instance creation from out side the world. And another thing is you can see the field name INSTANCE is static and final.
 private static final Singleton INSTANCE = new Singleton();
As you already know static fields are created at class loading time and share the static fields with the instances created from its class. So that means static fields will never be created at the time it create instances. In this case the constructor is private. There is no way to create instances from out side the word. But you can see INSTANCE field was assigned a object of Singleton class. That means there is one instance of Singleton , created by itself. So making this field static, it will take care of sharing a single INSTANCE field with other instances of Singleton class (in any case). But according to above example it is not possible to create more than one instance.

Then next question is why this field is final. As you might know final fields can be assigned only once. So making that field final, guaranteed we use the same instance assigned at class loading time. In other words assigned object will not be changed.

If this field is private then how can access this INSTANCE variable from out side of this class. That is why we should provide a static method to access INSTANCE variable as follows.

 public static Singleton getInstance() {
  return INSTANCE;
 }

Advanced

  • You might have known about the java clone method of Object class. So using this method we can create a copy of any object. Why I mention this method because if we want to use singleton design pattern we should aware of creating clones of singleton class because we need only one object. So as a best practice we should use following code snippet to your class.

 @Override
 public Object clone() throws CloneNotSupportedException {
  throw new CloneNotSupportedException("Clone is prohibited.");
 }

  • When creating a singleton class you should check about the thread safety of that class. Above example is thread safety one.
  • Next fact is avoiding sub classes. So you can do it this easily making the singleton class final.
  • Using == operator you can check getInstance()  method provide references that refers to same object or not.
package com.blog.patterns.singleton;

/**
 * @author uditha
 */
public class Main {
 
 public static void main(String[] args) {
  System.out.println("First instance creation begin");
  Singleton firstInstance = Singleton.getInstance();
  System.out.println("First instance creation end");
  System.out.println("Second instance creation begin");
  Singleton secondInstance = Singleton.getInstance();
  System.out.println("Second instance creation end");
  if (firstInstance == secondInstance) {
   System.out.println("Those instances refer to same object.");
  } else {
   System.out.println("Two different objects.");
  }
 }

}

Output
First instance creation begin
Singleton instance is created.
First instance creation end
Second instance creation begin
Second instance creation end
Those instances refer to same object.