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

    版权声明:本文为博主原创文章,未经博主同意不得转载。 https://blog.csdn.net/u012515223/article/details/28595349

    单件模式(singleton pattern) 具体解释


    本文地址: http://blog.csdn.net/caroline_wendy/article/details/28595349


    单件模式(singleton pattern) : 确保一个类仅仅有一个实例, 并提供一个全局訪问点.

    单位价格模式包含3个部分: 私有构造器, 静态变量, 静态方法.


    具体方法:

    1. 标准的单例模式:

    /**
     * @time 2014.6.5
     */
    package singleton;
    
    /**
     * @author C.L.Wang
     *
     */
    public class Singleton {
    	private static Singleton uniqueInstance; //静态变量
    	
    	private Singleton() {} //私有构造函数
    	
    	public static Singleton getInstance() { //静态方法
    		if (uniqueInstance == null)
    			uniqueInstance = new Singleton();
    		return uniqueInstance;
    	}
    
    }
    

    2. 考虑多线程的三种方法:

    同步(synchronized)方法, 加入"synchronized",  会导致性能下降, 每次调用演示样例, 都须要同步, 可是使用简单.

    /**
     * @time 2014.6.5
     */
    package singleton;
    
    /**
     * @author C.L.Wang
     *
     */
    public class Singleton {
    	private static Singleton uniqueInstance; //静态变量
    	
    	private Singleton() {} //私有构造函数
    	
    	public static synchronized Singleton getInstance() { //静态方法
    		if (uniqueInstance == null)
    			uniqueInstance = new Singleton();
    		return uniqueInstance;
    	}
    
    }
    


    急切(eagerly)方法, 開始时创建实例, 会在不须要时, 占用实例空间, 即占用空间时间过长.

    /**
     * @time 2014.6.5
     */
    package singleton;
    
    /**
     * @author C.L.Wang
     *
     */
    public class Singleton {
    	private static Singleton uniqueInstance = new Singleton(); //静态变量
    	
    	private Singleton() {} //私有构造函数
    	
    	public static synchronized Singleton getInstance() { //静态方法
    		//if (uniqueInstance == null)
    			//uniqueInstance = new Singleton();
    		return uniqueInstance;
    	}
    
    }
    

    双重检查加锁(double-checked locking)方法, 使用"volatile"和"synchronized (Singleton.class)", 降低时间消耗, 适用于java1.4以上版本号.

    /**
     * @time 2014.6.5
     */
    package singleton;
    
    /**
     * @author C.L.Wang
     *
     */
    public class Singleton {
    	private volatile static Singleton uniqueInstance; //静态变量
    	
    	private Singleton() {} //私有构造函数
    	
    	public static synchronized Singleton getInstance() { //静态方法
    		if (uniqueInstance == null) {
    			synchronized (Singleton.class) {
    				if (uniqueInstance == null)
    					uniqueInstance = new Singleton();
    			}
    		}
    		return uniqueInstance;
    	}
    
    }
    

    3. 使用单件模式的样例:

    代码:

    /**
     * @time 2014.6.5
     */
    package singleton;
    
    /**
     * @author C.L.Wang
     *
     */
    public class ChocolateBoiler { //巧克力锅炉
    	private boolean empty;
    	private boolean boiled;
    	
    	public static ChocolateBoiler uniqueInstance; //静态变量
    	
    	private ChocolateBoiler() { //私有构造函数
    		empty = true;
    		boiled = false;
    	}
    	
    	public static ChocolateBoiler getInstance() { //静态方法
    		if (uniqueInstance == null) 
    			uniqueInstance = new ChocolateBoiler();
    		return uniqueInstance;
    	}
    	
    	public void fill() { //填满
    		if (isEmpty()) {
    			empty = false;
    			boiled = false;
    		}
    	}
    	
    	public void drain() { //倾倒
    		if (!isEmpty() && isBoiled())
    			empty = true;
    	}
    	
    	public void boil() { //煮
    		if (!isEmpty() && !isBoiled()) {
    			boiled = true;
    		}
    	}
    	
    	public boolean isEmpty() {
    		return empty;
    	}
    	
    	public boolean isBoiled() {
    		return boiled;
    	}
    
    }
    

    4. 枚举单件(enum singleton)模式, 也能够保证线程安全.

    代码:

    /**
     * @time 2014.6.5
     */
    package singleton;
    
    /**
     * @author C.L.Wang
     *
     */
    public class EnumSingleton {
    
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    
    		eSingleton d1 = eSingleton.INSTANCE;
    		d1.setName("Spike");
    		
    		eSingleton d2 = eSingleton.INSTANCE;
    		d2.setName("Caroline");
    		
    		System.out.println(d1);
    		System.out.println(d2);
    		
    		System.out.println(d1 == d2);
    	}
    
    }
    
    enum eSingleton {
    	
    	INSTANCE;
    	
    	private String name;
    	
    	public String getName() {
    		return name;
    	}
    	
    	public void setName(String name) {
    		this.name = name;
    	}
    	
    	@Override
    	public String toString() {
    		return "[" + name + "]";
    	}
    }

    输出:

    [Caroline]
    [Caroline]
    true
    










  • 相关阅读:
    硬盘
    [编译] 6、开源两个简单且有用的安卓APP命令行开发工具和nRF51822命令行开发工具
    [编译] 5、在Linux下搭建安卓APP的开发烧写环境(makefile版)—— 在Linux上用命令行+VIM开发安卓APP
    [Zephyr] 1、在linux上安装Zephyr-OS并跑DEMO
    [编译] 4、在Linux下搭建nRF51822的开发烧写环境(makefile版)
    [BlueZ] 2、使用bluetoothctl搜索、连接、配对、读写、使能notify蓝牙低功耗设备
    [BlueZ] 1、Download install and use the BlueZ and hcitool on PI 3B+
    [python] 3 、基于串口通信的嵌入式设备上位机自动测试程序框架(简陋框架)
    [ARCH] 1、virtualbox中安装archlinux+i3桌面,并做简单美化
    [编译] 3、在Linux下搭建51单片机的开发烧写环境(makefile版)
  • 原文地址:https://www.cnblogs.com/ldxsuanfa/p/10037636.html
Copyright © 2011-2022 走看看