本文主要讲述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);
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
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);
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
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 +
'}';
}
}
运行结果如下:
到此这篇关于一文详解Java的饿汉和懒汉设计模式的文章就介绍到这了,更多相关Java设计模式内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!