單態的作用就不用說了,經常用過,就是一個類只產生一個對象。
它有兩個種實現方法。實現在大同小異,可以根據需要改進。
例程如下:
package com.pdw.pattern;
class SingletonA{
?private SingletonA(){
??
?}
?private static SingletonA _instance=new SingletonA();
?public static SingletonA getInstance(){
??return _instance;
?}
}
class SingletonB{
?private static SingletonB _instance;
?public static SingletonB getInstance(){
??if(_instance==null){
???return new SingletonB();
??}else{
???return _instance;
??}
??
?}
}
public class SingletonImpl {
?/**
? * @param args
? */
?public static void main(String[] args) {
??// TODO Auto-generated method stub
?}
}
posted on 2006-06-29 23:01
有貓相伴的日子 閱讀(632)
評論(0) 編輯 收藏 所屬分類:
Patterns