空对象设计模式问题

发布于 2024-07-10 10:49:42 字数 359 浏览 8 评论 0原文

我最近观看了关于空对象设计模式的这个 YouTube 教程。 尽管其中存在一些错误:例如不执行任何操作的 NullCar 会创建无限循环,但这个概念得到了很好的解释。 我的问题是,当可以为 null 的对象具有 getter 并在代码中使用时,您会怎么做? 您如何知道默认返回哪个值? 或者我应该在所有对象中实现这种模式? 如果我需要返回字符串或基元怎么办? 我是从Java的角度来谈的。

编辑:我不会将空对象测试换成默认值测试吗? 如果没有,为什么不呢?

I recently watched this youtube tutorial on the Null Object design pattern. Even though there were some errors in it: such as the NullCar that doesn't do anything creates an infinite loop, the concept was well explained. My question is, what do you do when the objects that can be null have getters, and are used in your code? How do you know which value to return by default? Or should I implement this pattern inside all the objects? What if I need to return strings or primitives? I'm talking from a Java perspective.

EDIT: won't I be trading null objects testing for default value testing ? If not , why not ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

往事风中埋 2024-07-17 10:49:42

空对象的目的是避免代码中出现空引用。 Null Object getter 返回的值取决于您的域。 零或空字符串通常是合适的。

如果我们将空对象模式移植到现实生活中,您所问的问题类似于问“没有人多大了?”。

也许你的设计可以改进,因为你似乎没有遵循告诉,不要问 原则。

编辑:当一个对象将行为委托给另一个对象(例如在策略或状态设计模式中); 正如 Tom Hawtin -tackline 所评论的,对于返回值的对象使用 特殊情况对象

The objective of a Null Object is to avoid having Null references in the code. The values returned by Null Object getters depend on your domain. Zero or empty string are usually appropriate.

If we transpose the Null Object pattern to real life, what you're asking is similar to ask "how old is nobody ?".

Perhaps your design can be improved as you seem not to follow the tell, don't ask principle.

EDIT: the Null Object design pattern is typically used when an object delegates behavior to another object (such as in Strategy or State Design Patterns) ; as Tom Hawtin - tackline commented, use Special Case Objects for objects returning values.

冷…雨湿花 2024-07-17 10:49:42

据我所知,这个想法是空对象的值尽可能接近“无”。 不幸的是,这意味着您必须自己定义它。 举个例子,当我无法传递空字符串时,我个人使用“”,空对象编号对我来说是-1(主要是因为默认情况下大多数数据库序列从1开始,我们将它们用于项目id:sa很多所以-1是死赠品,它是一个空对象),对于列表/映射/集,它是 Collections.EMPTY_SETEMPTY_MAPEMPTY_LIST 等等。 如果我有自定义类,我必须从中创建一个空对象,我会从中删除所有实际数据,看看它会将我带往何处,然后应用我刚才提到的内容,直到它为“空”。

所以你真的不“知道”默认返回哪个值,你只需要自己决定。

As far as I've understood it the idea is that the null object's value is as close to "nothing" as possible. That unfortunately means you have to define it yourself. As an example I personally use "" when I can't pass a null String, null object number for me is -1 (mostly because by default most database sequences start at 1 and we use those for item id:s a lot so -1 is dead giveaway it's a null object), with lists/maps/sets it's Collections.EMPTY_SET, EMPTY_MAP or EMPTY_LIST and so on and so forth. If I have custom class I have to create a null object from, I remove all actual data from it and see where that takes me and then apply what I just mentioned until it's "empty".

So you really don't "know" which value to return by default, you just have to decide it by yourself.

枯叶蝶 2024-07-17 10:49:42

当可以为 null 的对象具有 getter 并在代码中使用时,您会怎么做? 你怎么知道默认返回哪个值?

您如何知道要实现哪些类? 这是一个设计问题,取决于应用。

一般来说,NullObject 模式的目的是支持用多态性替换条件重构条件是与编程语言的空值进行比较的特殊情况。

视频中示例的正确实现需要将 driveCar 方法委托给 Car 类。 SlowCarFastCar 类可能会通过基类中的共享实现来执行循环,而 NullCar 将立即返回。

在 Java 上下文中,NullCar.speed 属性可能是一个未装箱的 int。 因此将其设置为 null 不是一个选项。 我可能会将属性隐藏在访问器后面,并让 NullCar.getSpeed 引发异常。 任何需要测试以避免此异常的客户端代码都将移至汽车类中。

委派所有直接依赖于可用速度值的操作是 Tell Don' 的应用t Ask philippe提到的面向对象设计原则

what do you do when the objects that can be null have getters , and are used in your code ? How do you know which value to return by default ?

How do you know which classes to implement? This is a design question, it depends on the application.

Generally speaking the purpose of the NullObject pattern is to support a Replace Conditional with Polymorphism refactoring in the special case where the the conditional is a comparison against the null value of the programming language.

A correct implementation of the example in the video would require delegating the driveCar method to the Car classes. The SlowCar and FastCar classes would perform the loop, presumably through a shared implementation in a base class, and the NullCar would just return immediately.

In a Java context, the NullCar.speed attribute would probably be an unboxed int. So setting it to null is not an option. I would probably hide the attribute behind accessor, and have NullCar.getSpeed raise an exception. Any client code that would need a test to avoid this exception would instead move into the car classes.

Delegating all operations that directly depend on a speed value being available is an application of the Tell Don't Ask principle of object-oriented design mentioned by philippe

木森分化 2024-07-17 10:49:42

Null设计模式在代码中的结合点应该是什么? 我认为 DAO 对象是此设计模式的第一级客户端,因为它们在数据库中查找实体并简单地返回它。

这些对象的可为空性检查会污染实际访问和使用它们的服务层或命令层中的代码。

请给出意见。

What should be the integration point of Null design pattern in code ? I think that DAO objects are the fisrt level client for this design pattern as they lookup an entity in database and return it simply.

The nullability check of these objects pollute the code in service layer or command layer where they are actually accessed and used.

Please comment.

人疚 2024-07-17 10:49:42

它应该返回您正在获取的类的空对象。 例如,如果您有一个类 A,其 getter 返回类 B 的对象,则相应的 NullA 的 getter 应该返回NullB

It should return the null object for the class you are getting. For example, if you have a class A with a getter that returns an object of class B, then the corresponding NullA's getter should return NullB.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文