求JAVA单子设计模式一个示例

来源:百度知道 编辑:UC知道 时间:2024/06/11 08:36:19
只需要一小段能说明这个是单子设计模式的示例
谢谢了 ^ _ ^

public class MyBean {
private static MyBean instance = null;
private MyBean(){
//do something
}
public static synchronized MyBean getInstance(){
if(instance == null){
instance = new MyBean();
}
return instance;
}
你是说单例模式是吧,这个单例模式是延迟加载的模式.还有一种就是即时加载,不需要判断是否有实例存在,先自己new出一个实例.
}

单例模式?没见过单子模式的。
public class SingleExample{
private static SingleExample p = null;
public static SingleExample createExample(){
if(p==null){
p = new SingleExample ();
}
return p;
}
}

标准单例模式
public class SingleSample {
private static SingleSample s = new SingleSample();
private SingleSample(){}
public static SingleSample getInstance(){
return s;
}
}

单态么?
class xx{
private static xx p = null;

public static xx createInstance(){
if(p==null){
p = new xx ()