---------------------- <a href="http://www.itheima.com" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="blank">ASP.Net+Unity開發</a>、<a href="http://www.itheima.com" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="blank">.Net教育訓練</a>、期待與您交流! ----------------------
單例模式:
特點:1、單例類隻能有一個執行個體
2、單例類必須自己建立自己的唯一執行個體
3、單例類必須給所有其他對象提供這一執行個體
好處:1、節省系統開銷
2、儲存該執行個體的狀态
分類:
懶漢式
核心代碼:
public class LazySingleton {
public static LazySingleton lazySingleton = null;
private LazySingleton() {
}
synchronized public static LazySingleton getInstance() {
if (lazySingleton == null) {
lazySingleton = new LazySingleton();
}
return lazySingleton;
}
}
餓漢式
核心代碼
public class HungerSingleton {
private static final HungerSingleton HUNGERSINGLETON = new HungerSingleton();
private HungerSingleton() {
}
public HungerSingleton getInstance() {
return HUNGERSINGLETON;
}
}
推薦使用餓漢式
因為在懶漢式中,由于存在多線程通路時,懶漢式可能會出現建立出多個執行個體,而若對其使用synchronized的話,則又會降低程式性能,是以推薦使用餓漢式。
登記式
import java.util.HashMap;
import java.util.Map;
public class RegistrationSingleton {
public static Map<String, RegistrationSingleton> map = new HashMap<String, RegistrationSingleton>();
static {
RegistrationSingleton rs = new RegistrationSingleton();
map.put(rs.getClass().getName(), rs);
}
protected RegistrationSingleton()// 此處為public或者protected都可,因為外界都無法通路
{
}
public static RegistrationSingleton getInstance(String name) {
if (name == null) {
name = "RegistrationSingleton";
}
if (map.get(name) == null) {
try {
map.put(name, (RegistrationSingleton) Class.forName(name)
.newInstance());
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return map.get(name);
}
}
如果有個學生類不是單例,但是我們想要把他當成單例模式,就是也隻能建立出一個學生執行個體,我們就可以用到這個,
Student s1=RegistrationSingleton.getInstance(“Student”);//注意此處Student必須為完整包名。注意學習這種思想,當然public static RegistrationSingleton getInstance(String name) 此時RegistrationSingleton必須寫成Object,裡面好多東西也需要改改。
修改如下:
import java.util.HashMap;
import java.util.Map;
public class RegistrationSingleton {
public static Map<String, Object> map = new HashMap<String, Object>();
static {
RegistrationSingleton rs = new RegistrationSingleton();
map.put(rs.getClass().getName(), rs);
}
protected RegistrationSingleton()// 此處為public或者protected都可,因為外界都無法通路
{
}
public static Object getInstance(String name) {
if (map.get(name) == null) {
try {
map.put(name, Class.forName(name).newInstance());
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return map.get(name);
}
}
--------------------- <a href="http://www.itheima.com" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="blank">ASP.Net+Unity開發</a>、<a href="http://www.itheima.com" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="blank">.Net教育訓練</a>、期待與您交流! ----------------------