zoukankan      html  css  js  c++  java
  • 设计模式-单例模式code

    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;
    }

    }
  • 相关阅读:
    [HEOI2015]兔子与樱花
    [HNOI2015]亚瑟王
    [JSOI2011]分特产
    某考试 T3 sine
    [JSOI2015]最小表示
    51NOD 1258 序列求和 V4
    Codeforces 622F The Sum of the k-th Powers
    Loj #6261. 一个人的高三楼
    [HAOI????] 硬币购物
    bzoj4318 OSU!
  • 原文地址:https://www.cnblogs.com/DeskZero/p/11707388.html
Copyright © 2011-2022 走看看