zoukankan      html  css  js  c++  java
  • Head First设计模式-单例模式

    一、整体代码

            Singleton.java

    public class Singleton {
    	private static Singleton uniqueInstance;
     
    	// other useful instance variables here
     
    	private Singleton() {}
     
    	public static synchronized Singleton getInstance() {
    		if (uniqueInstance == null) {
    			uniqueInstance = new Singleton();
    		}
    		return uniqueInstance;
    	}
     
    	// other useful methods here
    }


            Singleton.java

    public class Singleton {
    	private static Singleton uniqueInstance = new Singleton();;
     
    	// other useful instance variables here
     
    	private Singleton() {}
     
    	public static Singleton getInstance() {
    		return uniqueInstance;
    	}
     
    	// other useful methods here
    }

    二、解析

          1、第一种单件模式,在多线程时需要同步,造成了额外开销。

           2、第二种不用同步。


  • 相关阅读:
    数据库查找
    关于购买功能的相关学习
    信息登记功能例子
    总结
    团队作业
    团队作业
    团队作业
    团队作业
    团队作业
    第一节:库的管理
  • 原文地址:https://www.cnblogs.com/fuhaots2009/p/3372043.html
Copyright © 2011-2022 走看看