为每个类创建一个 NullObject 是否可行?(当然有工具) [英] Is it feasible to create a NullObject for every class? ( with a tool of course )

查看:25
本文介绍了为每个类创建一个 NullObject 是否可行?(当然有工具)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

NullObjectPattern 旨在成为安全"(中性)行为.

The NullObjectPattern is intended to be a "safe" ( neutral ) behavior.

这个想法是创建一个不做任何事情的对象(但也不抛出 NullPointerException )

The idea is create an object that don't do anything ( but doesn't throw NullPointerException either )

例如定义为的类:

class Employee {
    private String name;
    private int age;
    public String getName(){ return name; }
    public int getAge()    { return age;  }
}  

会在此代码中导致 NullPointerException:

Would cause a NullPointerException in this code:

 class Other {
      Employee oscar;

      String theName = oscar.getName(); // NPE

 }

NOP 所说的是,您可以拥有这样的对象:

What the NOP says, is you can have an object like this:

 class NullEmployee extends Employee {
      public static final  Employee instance = new NullEmployee(); 
      public String getName(){ return ""; }
      public int getAge()    { return 0;  }
 }

然后将其用作默认值.

 Employee oscar = NullEmployee.instance;

问题来了,当你需要为你创建的每个类重复代码时,一个解决方案就是使用一个工具来创建它.

The problem comes, when you need to repeat the code for every class you create, then a solution would be to have a tool to created it.

创建或使用这样的工具(如果存在)是否可行/合理/有用?

Would it be feasible/reasonable/useful to create such a tool or to use it ( if existed )?

也许使用 AOP 或 DI 可以自动使用默认值.

Perhaps using AOP or DI the default value could be used automagically.

推荐答案

对我来说,Null Object 模式感觉就像安慰剂.真实对象和空对象可能具有完全不同的含义,但行为非常相似.就像安慰剂一样,空对象会诱使您相信没有任何问题,但某些可能非常错误.

To me, the Null Object pattern feels like a placebo. A real object and a null object may have completely different meanings, but act very similar. Just like a placebo, the null object will trick you into believing there's nothing wrong, but something could be very wrong.

我认为尽早失败并经常失败是一种很好的做法.最后,您需要在某处区分真实对象和空对象,此时它与检查 null 指针没有什么不同.

I think it's a good practice to fail early and fail often. In the end, you'll want to distinguish between a real object and a null object somewhere, and at that point it would be no different from checking against a null pointer.

来自维基百科文章:

这种方法相对于可工作的默认实现的优势在于 Null 对象是非常可预测的并且没有副作用:它什么都不做.

The advantage of this approach over a working default implementation is that a Null Object is very predictable and has no side effects: it does nothing.

它也不会指出任何问题.想一想当一个空对象在您的应用程序中一路传播时会发生什么.然后,在某些时候,您的应用程序期望来自对象的某些行为,而空对象实现无法提供这些行为.那时您的应用程序可能会崩溃或进入无效状态.您将很难追踪空对象的来源.null 指针会在开始时抛出异常,直接将您的注意力引向问题的根源.

It won't point out any problems either. Think of what will happen when a null object travels all the way through your application. Then, at some point, your application expects certain behavior from the object, which the null object implementation fails to deliver. At that point your application may crash or enter an invalid state. You'll have a very hard time tracing the origin of the null object. A null pointer would have thrown an exception right at the beginning, drawing your attention directly to the source of the problem.

维基百科文章给出的唯一示例是一个空集合而不是 null.这是一个非常好的做法,但它是空对象模式的一个糟糕示例,因为它处理的是一组对象,而不是单个实例.

The only example the Wikipedia article gives, is that of an empty collection instead of null. This is a very good practice, but a lousy example of the null object pattern, because it's dealing with a collection of objects, instead of a single instance.

简而言之,我确信为所有类创建空对象实现是可行的,但我强烈建议不要这样做.

In short, I'm sure it's feasible to create null object implementations for all your classes, but I strongly recommend against it.

这篇关于为每个类创建一个 NullObject 是否可行?(当然有工具)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆