单例模式

1,饿汉模式

1 //饿汉单例模式
2 public class SingleInstance01 {
3     public static SingleInstance01 instance = new SingleInstance01();
4 
5     private SingleInstance01() {
6     }
7 }

2,懒汉模式

 1 public class SingleInstance02 {
 2     public static SingleInstance02 instance02;
 3     private SingleInstance02(){
 4 
 5     }
 6     public static SingleInstance02 getInstance02(){
 7         if(instance02==null){
 8             instance02 = new SingleInstance02();
 9         }
10         return  instance02;
11     }
12 }

 

posted @ 2022-06-13 06:40  青山不语  阅读(23)  评论(0)    收藏  举报