1、SingleObject.java
package com.ssm.cts.pattern.singleton; public class SingleObject { private static SingleObject instance; private SingleObject() { } public static synchronized SingleObject getInstance() { if (instance == null) { instance = new SingleObject(); } return instance; } public void initConfig() { System.out.println("初始化配置"); } }
2、Demo.java
package com.ssm.cts.pattern.singleton; public class Demo { // 测试 public static void main(String[] args) { SingleObject obj = SingleObject.getInstance(); obj.initConfig(); } }