JAVA_Basis -- 5 单例设计模式

1 java中的23中设计模式

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-1D75xhzI-1600384142056)(/img/bVbOXwE)]

java程序设计中的23中设计模式是java语言开发过程中开发者凭借多年的开发经验总结出来的,其本质是对面向对象设计原则的实际运用,是对java语言中对类的封装性,继承性,多态性,类的关联关系和组合关系的深入理解。

2 单例设计模式

单例模式,属于创建类型的一种常用的软件设计模式。通过单例模式的方法创建的类在当前进程中只有一个实例(类似于服务管理器),Spring默认创建的Bean就是单例模式。其特点是保证一个对象只有一个,具有节约系统空间,控制资源使用的优点。

2.1 案例分析

package com.mtingcat.javabasis.singletondesignpattern;

/**
 * Every Java application has a single instance of class
 * <code>Runtime</code> that allows the application to interface with
 * the environment in which the application is running. The current
 * runtime can be obtained from the <code>getRuntime</code> method.
 * <p>
 * An application cannot create its own instance of this class.
 *
 * @author  unascribed
 * @see     java.lang.Runtime#getRuntime()
 * @since   JDK1.0
 */
public class Runtime {
	/**
	 * 全局唯一对象
	 */
    private static Runtime currentRuntime = new Runtime();

    /**
     * Returns the runtime object associated with the current Java application.
     * Most of the methods of class <code>Runtime</code> are instance
     * methods and must be invoked with respect to the current runtime object.
     *
     * @return  the <code>Runtime</code> object associated with the current
     *          Java application.
     * 通过自定义的静态方法获取实例
     */
    public static Runtime getRuntime() {
        return currentRuntime;
    }

    /** Don't let anyone else instantiate this class 私有化*/
    private Runtime() {}
}


2.2 创建方式

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-HsEtFFBL-1600384142060)(/img/bVbOXRm)]

饿汉式

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ppJHvafw-1600384142063)(/img/bVbOXQ1)]

饿汉式的主要特点是类会提前把对象创建好,给你提供get()方法,返回已经创建好的对象。
结构控制:
① 私有化构造方法,使得外界无法创建对象,保证对象的唯一性。
② 类内部直接创建好对象,使用static关键字修饰,外界直接使用类名称拿到。
③ 提供get()方法返回创建好的对象,使用static关键字修饰(静态只能调用静态)。
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-TBEIABEr-1600384142065)(/img/bVbOXC0)]

package com.mtingcat.javabasis.singletondesignpattern;

public class SingleDemo01 {
	public static void main(String[] args) {
		Single a = Single.get();
		Single b = Single.get();
		
		
		System.out.println(a==b);
		System.out.println(a);
		System.out.println(b);
	}

}

class Single{
	/**
	 * 私有化构造方法,要想创建对象就只能通过Single类
	 */
	private Single(){}
	
	/**
	 * 在类内部直接创建好对象
	 */
	static private Single s = new Single();
	
	/**
	 * 对外只提供一个get方法,放回的是已经创建好的对象
	 * 用static修饰使得外界不通过对象访问而是直接通过类名访问
	 * @return
	 */
	static public Single get(){
		return s;
	}
}

懒汉式

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-M3DkKkpm-1600384142069)(/img/bVbOXRq)]

饿汉式的主要特点是类会提前声明要创建对象,但是还没有创建,给你提供get()方法,当调用get()方法的时候才会创建,并且只会创建一次(第一次)。
结构控制:
① 私有化构造方法,使得外界无法创建对象,保证对象的唯一性。
② 类内部直接创建好对象,使用static关键字修饰,外界直接使用类名称拿到。
③ 提供get()方法返回创建好的对象,使用static关键字修饰(静态只能调用静态)。
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-R2888z9O-1600384142072)(/img/bVbOXRs)]

package com.mtingcat.javabasis.singletondesignpattern;

public class SingleDemo02 {
	public static void main(String[] args) {
		Single02 a = Single02.get();
		Single02 b = Single02.get();
		
		
		System.out.println(a==b);
		System.out.println(a);
		System.out.println(b);
	}

}

class Single02{
	/**
	 * 私有化构造方法,要想创建对象就只能通过Single类
	 */
	private Single02(){}
	
	/**
	 * 在类内部直接创建好对象
	 */
	static private Single02 s = null;
	
	/**
	 * 对外只提供一个get方法,放回的是已经创建好的对象
	 * 用static修饰使得外界不通过对象访问而是直接通过类名访问
	 * @return
	 */
	synchronized static public Single02 get(){
		if(s==null){
			s=new Single02();
		}
		return s;
	}
}

猜你喜欢

转载自blog.csdn.net/weixin_43388956/article/details/108657162