Singleton pattern - To instantiate only one object in an application, and the object cannot be duplicate at all.
HOW TO:
class Singleton {
private static Singleton instance= null;
/** do protected constructor to prevent from the direct creation by an user */
protected void Singleton {
}
/** return instance of Object if it's never instantiated, create it first.*/
public static Singleton getInstance {
if (instance==null)
instance = new Singleton();
return instance;
}
}
Why to use it?
Sometimes, an object should have only one instance in application, for example, Preference dialog, any kind of setting dialog, or any atomic data structure class. In the case, it's useful to consider Singleton pattern.
댓글 없음:
댓글 쓰기