本文主要讲述java的单例设计模式,包括 :饿汉和懒汉设计模式
单例设计模式的目的:使得该类的对象,只能有一个,不允许其他类,创建该类的对象。
- 饿汉设计模式
示例代码如下:
1 public class HungerModel { 2 public static void main(String[] args) { 3 // GirlFriend girlFriend1 = new GirlFriend("white", 34); 4 // GirlFriend girlFriend2 = new GirlFriend("jack", 20); 5 // System.out.println(girlFriend1); 6 // System.out.println(girlFriend2); 7 GirlFriend girlFriend1 = GirlFriend.getInstance(); 8 System.out.println(girlFriend1); 9 10 GirlFriend girlFriend2 = GirlFriend.getInstance(); 11 System.out.println(girlFriend2); 12 13 System.out.println(girlFriend1 == girlFriend2); 14 15 } 16 17 } 18 19 // 只能有一个女朋友,即只能创建一个girlfriend对象? 20 21 class GirlFriend { 22 private String name; 23 private int age; 24 private static GirlFriend girl = new GirlFriend("布尔玛",20); 25 26 /** 27 * (1) 将构造器私有化,不允许其他类调用该构造器 28 * (2) 类中创建该对象实例,私有化,设置为静态,使用无需创建对象,直接用类.静态变量访问. 29 * (3) 提供static公共方法,返回实例对象 30 * @param name 31 * @param age 32 */ 33 34 public static GirlFriend getInstance(){ 35 return girl; 36 } 37 38 private GirlFriend(String name, int age) { 39 this.name = name; 40 this.age = age; 41 } 42 43 @Override 44 public String toString() { 45 return "GirlFriend{" + 46 "name='" + name + '\'' + 47 ", age=" + age + 48 '}'; 49 } 50 }
运行结果如下:
- 懒汉设计模式
示例代码如下:
public class LazyModel { public static void main(String[] args) { GirlFriend_ girlFriend1 = GirlFriend_.getInstance(); System.out.println(girlFriend1); GirlFriend_ girlFriend2 = GirlFriend_.getInstance(); System.out.println(girlFriend2); System.out.println(girlFriend1 == girlFriend2); } } class GirlFriend_ { private String name; private int age; private static GirlFriend_ girlFriend ; // 默认初始值为null /** * (1) 构造器私有化 * (2) 类成员中添加对象引用,static ,private,不要直接new, * (3) 提供公共方法,返回对象实例 ,public,判断当前对象是否为空, * 若为空,则创建,若不为空,直接返回该对象。 * @param name * @param age */ private GirlFriend_(String name, int age) { this.name = name; this.age = age; } public static GirlFriend_ getInstance(){ if(girlFriend == null){ girlFriend = new GirlFriend_("布尔玛",20); } return girlFriend; } @Override public String toString() { return "GirlFriend_{" + "name='" + name + '\'' + ", age=" + age + '}'; } }
运行结果如下:
饿汉和懒汉模式的区别:
饿汉模式,由于直接在类静态成员变量初始化时,创建该类的对象,导致该类对象随着类的存在,一直存在,有可能该对象一直得不到使用,因此饥饿,造成资源浪费;
示例代码如下:
public class HungerModel { public static void main(String[] args) { System.out.println(GirlFriend.id); } } // 只能有一个女朋友,即只能创建一个girlfriend对象? class GirlFriend { private String name; private int age; public static int id = 1001; private static GirlFriend girl = new GirlFriend("布尔玛",20); /** * (1) 将构造器私有化,不允许其他类调用该构造器 * (2) 类中创建该对象实例,私有化,设置为静态,使用无需创建对象,直接用类.静态变量访问. * (3) 提供static公共方法,返回实例对象 * @param name * @param age */ public static GirlFriend getInstance(){ return girl; } private GirlFriend(String name, int age) { this.name = name; this.age = age; System.out.println("GirlFriend类的private GirlFriend(String name, int age)构造器被调用"); } @Override public String toString() { return "GirlFriend{" + "name='" + name + '\'' + ", age=" + age + '}'; } }
运行结果如下:
由此看出,没有使用该对象,但是该对象仍然被创建,浪费资源。
懒汉模式,先声明静态变量【对象的引用】,在公共方法【返回对象】时,判断当前对象是否为空,若为空,则创建该对象,返回对象实例,这样,当调用该对象时,才会创建对象,不会造成资源浪费。
示例代码如下:
1 public class LazyModel { 2 public static void main(String[] args) { 3 System.out.println(GirlFriend_.id); 4 System.out.println(GirlFriend_.getInstance()); 5 } 6 } 7 class GirlFriend_ { 8 private String name; 9 private int age; 10 public static int id = 1002; 11 private static GirlFriend_ girlFriend ; // 默认初始值为null 12 13 /** 14 * (1) 构造器私有化 15 * (2) 类成员中添加对象引用,static ,private,不要直接new, 16 * (3) 提供公共方法,返回对象实例 ,public,判断当前对象是否为空, 17 * 若为空,则创建,若不为空,直接返回该对象。 18 * @param name 19 * @param age 20 */ 21 22 private GirlFriend_(String name, int age) { 23 this.name = name; 24 this.age = age; 25 System.out.println("GirlFriend_类的private GirlFriend_(String name, int age)构造器被调用"); 26 } 27 28 public static GirlFriend_ getInstance(){ 29 if(girlFriend == null){ 30 girlFriend = new GirlFriend_("布尔玛",20); 31 } 32 return girlFriend; 33 } 34 35 @Override 36 public String toString() { 37 return "GirlFriend_{" + 38 "name='" + name + '\'' + 39 ", age=" + age + 40 '}'; 41 } 42 }
运行结果如下:
老韩补充知识点: