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

查看:83
本文介绍了为每个类创建一个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.

Wikipedia文章给出的唯一示例是一个空集合的示例,而不是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天全站免登陆