Saturday 9 March 2013

Factory Method in JAVA


Factory Methods are static methods that return a same (native) class object.

Example:
Sample Factory Class:

Below is the sample Factory method from Example of Singleton Design Pattern.

public class Singleton{
    private static Singleton objSingleton;
    private Singleton(){
    }
    
    // Factory Method
    public  static Singleton getInstance(){
        if(objSingleton == null)
            objSingleton = new Singleton();
        return objSingleton;
    }
}


No comments:

Post a Comment