java singleton pattern

来源:互联网 发布:移动网络了解 编辑:程序博客网 时间:2024/04/29 22:22

1.advantage: ensure a class only has one instance and provide a global point of access to it.

2.lazily created

public class Singleton{private static Singleton uniqueInstance;        private Singleton(){};//private means only Singleton can instantiate this class!        public static Singleton getInstance(){if(uniqueInstance == null){uniqueInstance = new Singleton();}return uniqueInstance;        }}


3.eagerly created

public class Singleton{private static Singleton uniqueInstance = new Singleton();        private Singleton(){};//private means only Singleton can instantiate this class!        public static Singleton getInstance(){return uniqueInstance;        }}












0 0
原创粉丝点击