如何实现设计模式中的单例模式?
- 内容介绍
- 文章标签
- 相关推荐
本文共计522个文字,预计阅读时间需要3分钟。
javapackage model;
/** * 单例模式 * @author cocoRen * 有一些...保证应用程序的某个对象只有一个实例 * 类型:确保应用程序的对象只有一个类型: * 饭模式,懒汉模式 */public class Singleton { // 1. 创建类的唯一实例,设置为private static private static Singleton instance;
// 防止外部通过new创建实例 private Singleton() {}
// 提供公共的静态方法,用于获取实例 public static Singleton getInstance() { if (instance==null) { instance=new Singleton(); } return instance; }}
饿汉模式package model; /** * 单例模式 * @author cocoRen * 有些 *保证应用程序的对象只有一个 *类型:饿汉模式,懒汉模式 */ public class Singleton { //1.创建类的唯一实例,修饰为private static /** * 加载时间,因为修饰为static所以再类加载时就会实例化对象,此为饿汉模式 */ private static Singleton instance =new Singleton(); //2.将构造函数私有化,不允许外部直接创建对象 private Singleton(){ } //提供一个获取实例的方法 //类方法 public static Singleton getInstance(){ return instance; } } 懒汉模式
package model; public class Singleton2 { //1.将构造函数私有化 private Singleton2(){ } //2.创建类的唯一实例 private static Singleton2 instance; //3.提供获取实例的方法 public static Singleton2 getInstence(){ //再返回实例时判断是否创建了实例,若没有,先创建实例再返回,此为懒汉模式 if(instance==null){ instance=new Singleton2(); } return instance; } } 测试代码
package model; public class Test { public static void main(String[] args) { /** * 饿汉模式 * */ Singleton s1=Singleton.getInstance(); Singleton s2=Singleton.getInstance(); if(s1==s2){ System.out.println("s1与s2为同一实例"); }else{ System.out.println("s1不是s2为同一实例"); } /** * 懒汉模式 */ Singleton2 s3=Singleton2.getInstence(); Singleton2 s4=Singleton2.getInstence(); if(s3==s4){ System.out.println("s3与s4为同一实例"); }else{ System.out.println("s3不是s4为同一实例"); } } /** * 懒汉与饿汉的区别,当类加载时懒汉不创建实例,而饿汉是在类一加载就创建了实例,懒汉是在获取时再做判断, * 是否创建了实例。 * * 饿汉模式类加载比较慢,但运行时获取对象的速度比较快 ,线程安全 * 懒汉模式类类加载比较快,但运行时获取对象的速度比较慢,线程不安全 * */ }
本文共计522个文字,预计阅读时间需要3分钟。
javapackage model;
/** * 单例模式 * @author cocoRen * 有一些...保证应用程序的某个对象只有一个实例 * 类型:确保应用程序的对象只有一个类型: * 饭模式,懒汉模式 */public class Singleton { // 1. 创建类的唯一实例,设置为private static private static Singleton instance;
// 防止外部通过new创建实例 private Singleton() {}
// 提供公共的静态方法,用于获取实例 public static Singleton getInstance() { if (instance==null) { instance=new Singleton(); } return instance; }}
饿汉模式package model; /** * 单例模式 * @author cocoRen * 有些 *保证应用程序的对象只有一个 *类型:饿汉模式,懒汉模式 */ public class Singleton { //1.创建类的唯一实例,修饰为private static /** * 加载时间,因为修饰为static所以再类加载时就会实例化对象,此为饿汉模式 */ private static Singleton instance =new Singleton(); //2.将构造函数私有化,不允许外部直接创建对象 private Singleton(){ } //提供一个获取实例的方法 //类方法 public static Singleton getInstance(){ return instance; } } 懒汉模式
package model; public class Singleton2 { //1.将构造函数私有化 private Singleton2(){ } //2.创建类的唯一实例 private static Singleton2 instance; //3.提供获取实例的方法 public static Singleton2 getInstence(){ //再返回实例时判断是否创建了实例,若没有,先创建实例再返回,此为懒汉模式 if(instance==null){ instance=new Singleton2(); } return instance; } } 测试代码
package model; public class Test { public static void main(String[] args) { /** * 饿汉模式 * */ Singleton s1=Singleton.getInstance(); Singleton s2=Singleton.getInstance(); if(s1==s2){ System.out.println("s1与s2为同一实例"); }else{ System.out.println("s1不是s2为同一实例"); } /** * 懒汉模式 */ Singleton2 s3=Singleton2.getInstence(); Singleton2 s4=Singleton2.getInstence(); if(s3==s4){ System.out.println("s3与s4为同一实例"); }else{ System.out.println("s3不是s4为同一实例"); } } /** * 懒汉与饿汉的区别,当类加载时懒汉不创建实例,而饿汉是在类一加载就创建了实例,懒汉是在获取时再做判断, * 是否创建了实例。 * * 饿汉模式类加载比较慢,但运行时获取对象的速度比较快 ,线程安全 * 懒汉模式类类加载比较快,但运行时获取对象的速度比较慢,线程不安全 * */ }

