一文详解Java的饿汉和懒汉设计模式
zwz777 人气:0本文主要讲述java的饿汉和懒汉设计模式
饿汉和懒汉设计模式的目的:使得该类的对象,只能有一个,不允许其他类,创建该类的对象。
饿汉设计模式
示例代码如下:
public class HungerModel { public static void main(String[] args) { // GirlFriend girlFriend1 = new GirlFriend("white", 34); // GirlFriend girlFriend2 = new GirlFriend("jack", 20); // System.out.println(girlFriend1); // System.out.println(girlFriend2); GirlFriend girlFriend1 = GirlFriend.getInstance(); System.out.println(girlFriend1); GirlFriend girlFriend2 = GirlFriend.getInstance(); System.out.println(girlFriend2); System.out.println(girlFriend1 == girlFriend2); } } // 只能有一个女朋友,即只能创建一个girlfriend对象? class GirlFriend { private String name; private int age; 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; } @Override public String toString() { return "GirlFriend{" + "name='" + name + '\'' + ", age=" + age + '}'; } }
运行结果如下:
懒汉设计模式
示例代码如下:
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 + '}'; } }
运行结果如下:
由此看出,没有使用该对象,但是该对象仍然被创建,浪费资源。
懒汉模式,先声明静态变量【对象的引用】,在公共方法【返回对象】时,判断当前对象是否为空,若为空,则创建该对象,返回对象实例,这样,当调用该对象时,才会创建对象,不会造成资源浪费。
示例代码如下:
public class LazyModel { public static void main(String[] args) { System.out.println(GirlFriend_.id); System.out.println(GirlFriend_.getInstance()); } } class GirlFriend_ { private String name; private int age; public static int id = 1002; 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; System.out.println("GirlFriend_类的private GirlFriend_(String name, int age)构造器被调用"); } public static GirlFriend_ getInstance(){ if(girlFriend == null){ girlFriend = new GirlFriend_("布尔玛",20); } return girlFriend; } @Override public String toString() { return "GirlFriend_{" + "name='" + name + '\'' + ", age=" + age + '}'; } }
运行结果如下:
加载全部内容