我怎样才能实现一个普通形式的空对象设计模式? [英] How can i implement the NULL Object Design Pattern in a generic form?

查看:116
本文介绍了我怎样才能实现一个普通形式的空对象设计模式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法来实现空对象设计模式,在通用的形式,这样我就不需要实现它的每一个楼内设有商务对象。

Is there a way to implement the null object design pattern in a generic form so that i don't need to implement it for every buisness object.

对于我来说,有你所需要的每一个企业级的两个高级别类。一个用于单个记录,另一个用于列表。所以,我认为应该有一种方法来实现在高水平的空对象设计模式,并没有实现它的每一个类。

For me, there are two high level classes you'll need for every business class. One for a single record and another for a list. So i think there should be a way to implement the NULL Object design pattern at a high level and not have to implement it for every class.

有没有一种方法,以及如何请?

Is there a way and how please?

推荐答案

在我的理解中,NULL-类不必须为每一个类,你必须执行,而是为每个接口必须。在这种情况下,你肯定可以写一些方法,生成一个空的标准实现使用反射任何给定的接口。然而,一个NULL-对象的期望的行为可能有特殊的情况下为某些接口,在这种情况下,一个通用的解决方案会失败。​​

In my understanding, the NULL-class does not have to be implemented for every class you have, but rather for every interface you have. In that case, you could surely write some method that generates an empty standard-implementation for any given interface using reflection. However, the desired behavior of a NULL-object may have special cases for certain interfaces, in which case a generic solution would fail.

例如,你可以有一个接口实现 IComparable的。在某些情况下,你可能要等于其他所有对象的空对象,而在另一些情况下,你希望它是比所有其他对象更小。

For instance, you could have an interface that implements IComparable. In some cases, you may want the NULL-object to be equal to all other objects, and in other cases, you want it to be smaller than all other objects.

编辑: IComparable的只是一个例子。问题是,我不认为这是明智的有一个通用的空类实现。您可以使用NULL级,所以你的程序,而无需办理 NULL 返回值的特殊情况下工作。如果你有一个默认的实现,那么你就肯定要再次检查特殊情况,以及该模式的整点会错过。

The IComparable was just an example. The point is, that I do not think it is smart to have a generic NULL-class implementation. You use the NULL-class, so your program can work without having to handle the special cases of NULL return values. If you have a default implementation, then you would most certainly have to check for special cases again, and the whole point of the pattern would be missed.

这篇关于我怎样才能实现一个普通形式的空对象设计模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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