- 单例设计模式:饿汉式
- class Single
- {
- private static final Single s = new Single();
- private single(){};
- public static void getStance()
- {
- return s;
- }
- }
- 单例设计模式:懒汉式
- class Single2
- {
- private Single2(){};
- private static final Single2 s ;
- if(s==null)
- {
- synchronized(Single2.class)
- {
- if(s==null)
- {
- return s=new Single2;
- }
- }
- }
- }
- */
复制代码 |