Java单例模式

应用场景:多个类共用一个方法,想批量执行这些类,但是方法仅执行一次。一般为初始化方法。

单例的初始化方法的写法

 

public class ParentTest {

private ParentTest() {

}

private static ParentTest intance = null;

static IDRSFactory factory;

static IDRSClient client;

 

public static ParentTest getInstance() {

if (intance == null) {

intance = new ParentTest();

// 初始化factory对象

Bootstrap.start();

IDRSFactory factory = DRSFactoryManager.getInstance().getFactory();

Map<String, String> params = new HashMap<String, String>();

params.put(RPCParamType.call_timeout.getName(), "10000");

factory.init("zookeeper://10.24.1.84:2181", "drs", "CS", "1.0",

params);

client = factory.getDRSClient();

}

return intance;

}

}

 

在其他类中调用这个方法

Java单例模式

 

来自 <http://lxw198902165221.blog.163.com/blog/static/258950022201741812849557/>