java算法面试题:写一个Singleton出来

package com.swift;

public class Singleton {

    public static void main(String[] args) {
        /*
         * 写一个Singleton出来
         */
        ORC_Hungry.getOrc().fun();
        ORC_Lazy.getOrc().fun();
    }

}

class ORC_Hungry{
    //饿汉式
    private static ORC_Hungry orc=new ORC_Hungry();
    private ORC_Hungry() {}
    public static ORC_Hungry getOrc() {
        return orc;
    }
    public void fun() {
        System.out.println("This is a hungry Singleton.....");
    }
}

class ORC_Lazy{
    //懒汉式
    private static ORC_Lazy orc;
    private ORC_Lazy() {}
    public static synchronized ORC_Lazy getOrc() {
        if(orc==null) {
            orc=new ORC_Lazy();
        }
        return orc;
    }
    public void fun() {
        System.out.println("This is a lazy Singleton.....");
    }
}
原文地址:https://www.cnblogs.com/qingyundian/p/8360149.html