美文网首页
JAVA中单例模式的几种写法

JAVA中单例模式的几种写法

作者: Jetlag时 | 来源:发表于2018-03-24 11:03 被阅读9次

      1.懒汉式

    public class jetlag{

            private static Jetlag jetllag = null;

            priavte Jetlag(){}

             private static Jetlag getJetlag(Jetlag jetllag){

                if(jetllag == null){

                        jetlag = new Jetlag();

    }

    return jetlag;

    }

    }

    2.饿汉式

    public class Jetlag{

            private static Jetlag  jetlag= new Jetlag();

            private Jetlag(){}

            public static Jetlag(){

            return jetlag;

    }

    }

    3.变种的饿汉式

    public class Jetlag{

    private statci Jetlag jetlag = null;

    static {    

    jetlag = new Jetlag();

    }

    private Jetlag(){}

    public static Jetlag(){

    return this.jetlag;

    }

    }

    相关文章

      网友评论

          本文标题:JAVA中单例模式的几种写法

          本文链接:https://www.haomeiwen.com/subject/ufkqcftx.html