Monday, 7 November 2011

Factory Design Pattern

Whenever we thought of a factory, following picture comes to our mind –





That means a huge plant where some laborers manufacture “goods” or supervise machine processing one product to another. In a generic term, a factory produces different products of similar kind (Mobira Cityman 900, Nokia N90) based on requirement in a single roof (sorry, roof might be different).


 
But when we are talking about Factory in object oriented design, first question come to mind is – factory of what?? Of course – Object. A Factory pattern is one that returns an instance of one of several possible classes depending on the data provided to it. In simple words, if we have a base class (e.g. B) and n derived classes (d1, d2 … dN), and based on data provided to a class, it returns any instance of the base classes, then the class is a factory.


In this figure, the factory returns d1Obj depending on the argument it receives (i.e. One). The client should not have any concern as they are using same method with different behavior. How to return is purely “Factory” decision but what to return should be based on client input.


Let us discuss the concept of Factory Design Pattern with an example. Suppose that we need to create a “Tax Calculator” for our application which needs to calculate Tax for different country.  

            if(lCountryName == "US") {
                        lTaxObj = new USTaxCalculator();
            } else if (lCountryName == "UK") {
                        lTaxObj = new UKTaxCalculator();
            } else if (lCountryName == "India") {
                        lTaxObj = new IndiaCalculator();
            }

But this is not an efficient way as
  1. Lots of new keywords scattered in client code. In other words, the client is loaded with lots of object creation activities and hence the design will be complicated
  2. The client needs to be aware of all Class and needs to know the way to handle it
  3. The client code needs to change or recompile if a different kind of Tax Calculator require to support. In nutshell, the client code is tightly coupled with different “Tax Calculator”

To overcome these deficiencies, let us “establish” a Factory that “manufactures” different Tax Calculator objects. We need to introduce an Interface (so that the user know the behavior of the object return by the factory) and the Factory Class (which returns require object as per the user need)


public interface TaxCalcFactInterface {
            public void CalculateTax(String pCountry);
}

And the factory class looks like

public class TaxCalcFactory {
            public TaxCalcFactInterface getFactObj(String name) {
             if(name.equalsIgnoreCase("US")) {
                         return new USTaxCalculator();
             } else {
                         return new UKTaxCalculator();
             }
    }
}

The client knows only the interface and the factory. There is no connection between the client and the concrete classes (USTaxCalculator and UKTaxCalculator). So, if a new TaxCalculator needs to support, we can simply add it without changing any client code.

Below is the code snippet of how the client code looks like-

public class MainClass {

            public static void main(String[] args) throws IOException {

                        System.out.println("Enter the country name ");
                        String lCountryName = new java.io.BufferedReader(new java.io.InputStreamReader(System.in)).readLine();

                        TaxCalcFactInterface usTaxCalc = new TaxCalcFactory().getFactObj(lCountryName);
                        usTaxCalc.CalculateTax();
            }
}

The corresponding concrete classes can be written as

public class UKTaxCalculator implements TaxCalcFactInterface {
            @Override
            public void CalculateTax() {
                        System.out.println(" Calculatinf Tax for UK");
            }
}

And

public class USTaxCalculator implements TaxCalcFactInterface {
            @Override
            public void CalculateTax() {
                        System.out.println(" Calculating Tax for United State " );
            }
}

In order to avoid recompiling the client we have introduced the TaxCalcFactInterface interface. Both the concrete classes USTaxCalculator and UKTaxCalculator inherit and implement the TaxCalcFactInterface interface.




And that’s the fundamental principle of Factory patterns. This type of factory design pattern can be named as “parameterized factory” as the object creation is decided based on “parameter” passed by the client. The main disadvantage of this approach is that it is not very flexible. To add a new concrete class, the factory class needs to be modified (we are using hard coded data – if else).   

Use of reflection: Instead of “hard coded” let’s use reflection to instantiate the desire object. To achieve that, we can register our class information in property object and instantiate the object without even changing the factory itself.

My property file looks like

UK=simple.factory.dp.UKTaxCalculator
US=simple.factory.dp.USTaxCalculator

And we can change our factory class as

public class TaxCalcFactoryReflect {
            public static String getClassName(String pCountry) throws IOException {
                        Properties prop = new Properties();
                        FileInputStream propFile = new FileInputStream("../class.properties");
                        prop.load(propFile);
                        propFile.close();
                        return prop.getProperty(pCountry);
            }
            public TaxCalcFactInterface getFactObj(String pCountry) throws
                        ClassNotFoundException,
                        InstantiationException,
                        IllegalAccessException, IOException {
                         Class<?> clazz = Class.forName(TaxCalcFactoryReflect.
                        getClassName(pCountry),true, ClassLoader.getSystemClassLoader());
                         return (TaxCalcFactInterface) clazz.newInstance();
            }
}

Thursday, 20 October 2011

Singleton not behaving as a singleton


What is Singleton Design Pattern:

Singleton is the simplest creational design pattern and is widely used by program designer. So, I thought of sharing my understanding on singleton DP as my first post.

A singleton is a class that can be instantiated only one time in a JVM per class loader. Repeated calls always return the same instance. Ensures that a class has only one instance, and provide a global point of access.

The UML diagram of Singleton Class is surprisingly simple because Singleton consists of a simple class that holds a reference to a single instance of itself



The purpose of Singleton design pattern is to control the object creation process and limiting it to one instance. But as a design engineer, we need to make it flexible enough such that it can easily extendible to “doubleton” or any N-ton class.

Well, a singleton can be created, but it can't be instantiated by developers - meaning that the singleton class itself takes the control of creating object. The restriction on the singleton is that there can be only one instance of a singleton created by the Java Virtual Machine (JVM) - by prevent direct instantiation we can ensure that developers don't create a second copy.

As a starting point, we can define a class as –

public class MySingleton {
            public static MySingleton mMySingletonObj = new MySingleton();
}

Now, the question is, is it a Singleton? Apart from design limitation, this class is having many other drawbacks.

Let’s try with a main class

public class mainC {

            public static void main(String[] args) {   
                        MySingleton objOne = new MySingleton();                     
                        MySingleton objTwo = new MySingleton();
            }
}

Oops, we got multiple objects. How to prevent it? Well, provide a default constructor and mark it private

public class MySingleton {
            public static MySingleton mySingletonObj = new MySingleton();
            private MySingleton() {}
}

Now, try to compile the previous main class (mainC.java). So, to some extent, it is a singleton. But, extend it and perform some crazy experiment, this class will lose its property. Think over it !!!

Now, let’s try out another way to define a Singleton

public class MySingleTon { 
    // flag to check the instance
    private static MySingleTon _myInst= null;
    private MySingleTon() {
    }
    public static MySingleTon getInstance() {
            if (_myInst== null) {
                        _myInst = new MySingleTon();
            }
            return _myInst;
    }
}

So far so good. We prevent direct instantiation (private constructor) and provide a way to get the reference to an instance of the singleton object. We are intelligent enough and that is why we made getInstance a static member and store the instance in a private member.

When first called, the getInstance() method creates a singleton instance, assigns it to a member variable, and returns the singleton. Subsequent calls will return the same singleton. The design is flexible enough to add new functionality to the singleton object by adding new methods. But, still we are not constructing THE perfect singleton class. Think from multithreaded environment prospective. Is it thread safe? Oops!!!

Well, we can protect out getInstance() method from threat of Thread Synchronization problem by using synchronized  keyword.

public static synchronized MySingleTon getInstance() {
            if (_myInst== null) {
                         _myInst = new MySingleTon();
            }
            return _myInst;
 }

Happy??? But being a good java designer you should not. Assume that our getInstance()method is not as simple as mentioned above and it is having other stuff also (I am also assuming).  So, we can do something else. It is always good idea to block the race condition code rather than blocking the entire method. So let’s block the sensitive info

public static MySingleTon getInstance() {
            if (_myInst== null) {
                        synchronized (MySingleTon.class) {
                                    if (_myInst== null) {
                                                _myInst = new MySingleTon();
                                    }
                                    }
            }
            return _myInst;
}

We moved the synchronization to the block level rather than the method level.

Advantage: Fine Gain Control, if multiple threads are entering the code, we are making sure that the waiting time is minimal for all the threads, as we have synchronized only the code which really requires synchronization. It is resolved using “Double Type Checking”.

For the very first time this problem may arise. Let say Thread1 has executed “if (_myInst== null)” statement, and swapped out (may be CPU time slice), another thread say Thread2 got the chance to run and it executed the same statement “if (_myInst== null)” and acquired the lock on “MySingleTon.class”, and now Thread2 is running and will create the new Singleton() class , now when Thread1 wakes up it will also acquire the lock on “Singleton.class” and will   create one more instance of the Singleton class, which is what we don’t want.

Are we finished?? Sorry we are still not done. If you serialize your class and deserialize it twice, there will be multiple instances of Singleton class. Where there's a will, there's a way – we can use readResolve() method, in order to return the Singleton object read, it will make sure only one instance is returned back.

public class Singleton implements Serializable
               {
                        //Code for singleton here.
                        // This method is called immediately after an object
                // of this class is deserialized.
                        // This method returns the singleton instance.
                        protected Object readResolve()
                        {
                                    return getInstance();
                        }
           
 }

Do you think we are done? Not yet.

public class CloneMainClass {
            public static void main(String[] args) {
                        // Get a singleton
                          MySingleTon mySingObj = MySingleTon.getInstance();                           
                          MySingleTon mySingCloneObj = (MySingleTon) mySingObj.clone();
            }
}

This is correct. So, we are virtually getting two Singleton Objects. To make out class a perfect singleton, we have to add clone() method and simply through CloneNotSupportedException exception.

public Object clone() throws CloneNotSupportedException {
            throw new CloneNotSupportedException();
}

Still there are few ways using which you can make multiple instances of this Singleton implementation - Multiple Singletons Simultaneously Loaded by Different Class Loaders. To avoid this we have to make sure that that Singleton object is loaded by only one class loader peeking through code.

To summarize it, let us find out the merits and demerits of singleton design pattern




Merits
Self Control: Singleton prevents other objects from instantiating their own copies of the Singleton object, ensuring that all objects access the single instance.
Flexibility: Because the class controls the instantiation process, the class has the flexibility to change the instantiation process.

Demerits

Overhead: There is some overhead involved in checking whether an instance of the class already exists every time an object requests a reference
Object lifetime: Though, as a java programmer we don’t have to think much on cleaning the object, other programmer needs to find out a way to clean up the singleton object

So, I am singing off now. Please share you thought with us.