空对象模式

空对象模式

前言

空对象模式并不是Gof23种设计模式之一,但是也是一种常见的编程模式
空对象模式通常用来简化程序返回Null时的处理逻辑,让“空对象”承担处理Null的责任。

  • 优点
    • 大量减少if null的判断
  • 缺点
    • 需要扩展额外的继承,部分场景还是需要进行判断

随着Java8 Option的应用,这种模式也逐步淘汰

UML

使用接口或抽象类替换原先需要调用的类,为其添加Null对象。使用空对象执行逻辑,避免Null判断的出现

AbastractClass Object doSth DefaultClass Object doSth NullClass Object doSth

使用实例

1. 空判断

例如,我们在某段查询代码中,返回一个User类,然后调用User类的sayHi()方法。为了避免NullPointException,需要做空判断,代码可能如下

public class Test {
    public static void main(String[] args) {
        // 模拟10次调用
        for (int i=0; i<10; i++) {
            User user = getUser();
            if (null != user) {
                user.sayHi();
            } else {
                System.out.printf("第%d次getUser返回空 \n", i+1);
            }
        }
    }

    public static User getUser() {
        // 模拟查询到了user或没有查询到user
        long r = Math.round(Math.random() * 10);
        if (r % 2 == 0) {
            return new User();
        }
        return null;
    }
}

public class User {
    public void sayHi() {
        System.out.println("hello world");
    }
}

运行结果:

Connected to the target VM, address: ‘127.0.0.1:63237’, transport: ‘socket’
hello world
hello world
hello world
第4次getUser返回空
第5次getUser返回空
第6次getUser返回空
hello world
hello world
hello world
hello world
Disconnected from the target VM, address: ‘127.0.0.1:63237’, transport: ‘socket’

2. 空对象模式

将User需改为一个接口,添加两个实现类,DefaultUser和NullUser。前者实现具体方法,后者实现方法体为空

public interface User {
    void sayHi();
}

public class DefaultUser implements User {
    @Override
    public void sayHi() {
        System.out.println("hello, world");
    }
}

public class NullUser implements User{
    @Override
    public void sayHi() {
        // do nothing
    }
}

查询时,不需要返回null,而是返回NullUser,调用者不需要再进行空判断

public class Test {

    public static void main(String[] args) {
        // 模拟10次调用
        for (int i=0; i<10; i++) {
            User user = getUser();
            // 此时不需要进行空判断也不会报错
            user.sayHi();
        }
    }

    public static User getUser() {
        // 模拟查询到了user或没有查询到user
        long r = Math.round(Math.random() * 10);
        if (r % 2 == 0) {
            return new DefaultUser();
        }
        return new NullUser();
    }
}

运行结果:

hello, world
hello, world
hello, world
hello, world
hello, world
hello, world
hello, world

Process finished with exit code 0

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值