首页 > 其他 > 详细

设计模式-单例模式code

时间:2019-10-20 14:52:31      阅读:63      评论:0      收藏:0      [点我收藏+]
package singeton;


import java.security.SecureRandom;

/**
* @author Zero
* @since 2019-08-13.
* Description:
*/
public class HungrySingleton {
private static final HungrySingleton singleton = new HungrySingleton();
private final int ID = new SecureRandom().nextInt();

private HungrySingleton() {
}

public static HungrySingleton getSingleton() {
return singleton;
}

public int doSomething() {
// System.out.println("I‘m HungrySingeton " + ID + "!");
return ID;
}
}

package singeton;

import java.security.SecureRandom;

/**
* @author Zero
* @since 2019-08-13.
* Description:
*/
public class LazySingleton {
private static LazySingleton singeton = null;
private final int ID = new SecureRandom().nextInt();

private LazySingleton() {
}

public static synchronized LazySingleton getSingleton() {
if (singeton == null) {
singeton = new LazySingleton();
}
return singeton;
}

public int doSomething() {
// System.out.println("I‘m LazySingeton " + ID + "!");
return ID;
}

}

设计模式-单例模式code

原文:https://www.cnblogs.com/DeskZero/p/11707388.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!