Factory Methods are static methods that return a same (native)
class object.
Example:
- java.lang.Runtime #getRuntime()
- java.text.DateFormat # getInstance ()
- java.util.Calendar #getInstance()
- java.util.Collections #synchronizedCollection()
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