zoukankan      html  css  js  c++  java
  • Java单例设计模式

     懒汉式参考:http://blog.csdn.net/haitaofeiyang/article/details/44124375

    没有使用单例模式:

    package java_basic;
    class Singleton{
    	public Singleton(){																	
    	}
    	
    	public void print(){
    		System.out.println("Hello Word");
    	}
    }
    
    public class singleton {
    
    	public static void main(String[] args) {
    		// TODO 自动生成的方法存根
    		Singleton s1 = new Singleton();
    		Singleton s2 = new Singleton();
    		Singleton s3 = new Singleton();
    		System.out.println(s1.hashCode());
    		System.out.println(s2.hashCode());
    		System.out.println(s3.hashCode());
    		s1.print();
    		s2.print();
    		s3.print();
    	}
    
    }
    

     

    单例设计模式:
    1):饿汉式:(一定是安全的,只有一份对象)
    1.构造器私有化
    2.声明私有的静态属性,同时创建该对象
    3.对外提供访问属性的静态方法,确保该对象存在。

    (1):写法:初始化就加载

    package java_basic;
    class Singleton{
    	private static Singleton instance = new Singleton();	//声明私有的静态属性,同时创建该对象
    	private Singleton(){					//构造函数私有化
    	}
    	
    	public static Singleton getInstance(){			//对外提供访问属性的静态方法,确保该对象存在
    		return instance;
    	}
    	
    	public void print(){
    		System.out.println("Hello Word");
    	}
    }
    
    
    public class singleton {
    
    	public static void main(String[] args) {
    		// TODO 自动生成的方法存根
    		Singleton s1 = Singleton.getInstance();
    		Singleton s2 = Singleton.getInstance();
    		Singleton s3 = Singleton.getInstance();
    		System.out.println(s1.hashCode());
    		System.out.println(s2.hashCode());
    		System.out.println(s3.hashCode());
    		s1.print();
    		s2.print();
    		s3.print();
    	}
    
    }
    

     可以看到输出的hashCode只有一个,证明new出的对象是同一个

    (2):写法:类在使用的时候加载,延时加载

    package java_basic;
    
    class Singleton{
    	//类在使用的时候加载,延时加载 
    	private static class Single{  
    		private static Singleton instance = new Singleton();  //声明私有的静态属性,同时创建该对象
    	}
    
    	private Singleton(){					//构造函数私有化
    	}
    	
    	public static Singleton getInstance(){			//对外提供访问属性的静态方法,确保该对象存在
    		return Single.instance;
    	}
    	
    	public void print(){
    		System.out.println("Hello Word");
    	}
    }
    
    
    public class singleton {
    
    	public static void main(String[] args) {
    		// TODO 自动生成的方法存根
    		Singleton s1 = Singleton.getInstance();
    		Singleton s2 = Singleton.getInstance();
    		Singleton s3 = Singleton.getInstance();
    		System.out.println(s1.hashCode());
    		System.out.println(s2.hashCode());
    		System.out.println(s3.hashCode());
    		s1.print();
    		s2.print();
    		s3.print();
    	}
    
    }
    

     

    单例设计模式:
    2):懒汉式:(不一定安全,确保只有一份对象需要synchronized)
    1.构造器私有化
    2.声明私有的静态属性
    3.对外提供访问属性的静态方法,确保该对象存在。
    (1):写法,多线程下不安全

    package java_basic;
    
    class Singleton{		//懒汉模式,多线程下不安全
    	private static Singleton instance;	//声明私有的静态属性,但是不创建该对象
    	private Singleton(){			//构造函数私有化
    	}
    	
    	public static Singleton getInstance(){	//对外提供访问属性的静态方法,确保该对象存在
    		 if(instance == null){  
    	            instance = new Singleton();  
    	        }  
    		return instance;
    	}
    	
    	public void print(){
    		System.out.println("Hello Word");
    	}
    }
    
    public class singleton {
    
    	public static void main(String[] args) {
    		// TODO 自动生成的方法存根
    		Singleton s1 = Singleton.getInstance();
    		Singleton s2 = Singleton.getInstance();
    		Singleton s3 = Singleton.getInstance();
    		System.out.println(s1.hashCode());
    		System.out.println(s2.hashCode());
    		System.out.println(s3.hashCode());
    		s1.print();
    		s2.print();
    		s3.print();
    	}
    
    }
    

     

    (2):写法,加锁

    package java_basic;
    
    class Singleton{		//懒汉模式,加锁
    	private static Singleton instance;	//声明私有的静态属性,但是不创建该对象
    	private Singleton(){			//构造函数私有化
    	}
    	
    	public static Singleton getInstance(){	//对外提供访问属性的静态方法,确保该对象存在
    		if(instance == null){  
    			synchronized(Singleton.class){//加锁
    				if(null == instance){
    					instance = new Singleton();  
    				}
    			}
    		}  
    		return instance;
    	}
    	
    	public void print(){
    		System.out.println("Hello Word");
    	}
    }
    
    public class singleton {
    
    	public static void main(String[] args) {
    		// TODO 自动生成的方法存根
    		Singleton s1 = Singleton.getInstance();
    		Singleton s2 = Singleton.getInstance();
    		Singleton s3 = Singleton.getInstance();
    		System.out.println(s1.hashCode());
    		System.out.println(s2.hashCode());
    		System.out.println(s3.hashCode());
    		s1.print();
    		s2.print();
    		s3.print();
    	}
    
    }
    
  • 相关阅读:
    学习Python的一些Tips
    读书笔记《深入理解计算机系统》(第三版) 第二章 信息的表示
    读书笔记《深入理解计算机系统》(第三版) 第一章 计算机系统漫游
    C 标准库系列之locale.h
    C 标准库系列之limits.h
    C 标准库系列之float.h
    C 标准库系列之errno.h
    C 标准库系列之ctype.h
    C 标准库系列之assert.h
    C 标准库系列之概述
  • 原文地址:https://www.cnblogs.com/tonglin0325/p/5196818.html
Copyright © 2011-2022 走看看