首页 > 其他 > 详细

单例、工厂、适配器、装饰器

时间:2019-05-12 16:00:58      阅读:148      评论:0      收藏:0      [点我收藏+]

单例

单例分为懒汉式和饿汉式

 1 public class Singleton {
 2     
 3     
 4     //饿汉加载
 5     //应用时才加载,节省内存
 6     private static Singleton instance;   
 7     private Singleton() {  
 8     }  
 9     public static Singleton getInstance(){  
10         if (instance == null) {  
11             synchronized (Singleton.class) {  
12                 if (instance == null) {  
13                     instance = new Singleton();  
14                 }  
15             }  
16         }  
17         return instance;  
18     } 
19     
20     
21     //饿汉加载 
22     //浪费内存
23 //    private static Singleton singleton=new Singleton();
24 //    public static Singleton getInstance(){
25 //        return singleton;
26 //    }
27     
28     public void run(){
29         System.out.println("hellowolrd");
30     }
31 }

 

工厂模式:

 

单例、工厂、适配器、装饰器

原文:https://www.cnblogs.com/siyyawu/p/10852333.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!