没有行为的对象 [英] Objects Without Behaviour

查看:82
本文介绍了没有行为的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

除了通用语言外,我还有一个与一般OOP相关的问题. 我正在尝试一个简单的应用程序(在Java中),并且试图像在现实世界中一样对它进行建模. 在进行重构时,我意识到我想到了一个简单的对象,它只有一个成员,并且覆盖了等号和哈希码.

I have a question related to general OOP than specific to a language. I was trying out a simple application (in java) and I was trying to model it like a real world scenario. While re-factoring I realized that I came up with a simple object that just has one member and an overridden equals and hashcode.

我的问题是....拥有这样的物体是一种不好的做法吗? (欢迎引用博客等)

My question is.... is it a bad oo practice to have such objects (references to blogs etc would be welcome)

推荐答案

简短回答:

拥有这样的物体是一种不好的做法吗

is it a bad oo practice to have such objects

不一定,但这取决于上下文.

Not necessarily, but it depends on the context.

更长的答案:

除了通用语言外,我还有一个与一般OOP相关的问题.我正在尝试一个简单的应用程序(在Java中),并且试图像在现实世界中一样对它进行建模.

I have a question related to general OOP than specific to a language. I was trying out a simple application (in java) and I was trying to model it like a real world scenario.

确实没有任何规则说明您应该这样做.实际上,我知道有很多人对此声明不屑一顾,鲍勃·马丁叔叔一.与其说是对真实场景"进行建模,不如说是对业务流程进行建模.我过去曾尝试过这种方法,但发现严格地对现实世界中的所有事物建模都没有或几乎没有收益.如果有的话,我认为这会使您的应用程序变得更加复杂,并且软件变得越复杂,维护起来就越困难.

There really isn't any rule stating that you should. In fact, I know of quite a few people who frown upon that statement, Uncle Bob Martin for one. It's more about modelling business processes than it is to model "real world scenarios". I've tried that in the past, and found there's no - or almost no - benefit to get from rigidly trying to model everything as it is in the real world. If anything, I think it makes your application more complex, and the more complex software becomes, the harder it becomes to maintain.

在进行重构时,我意识到我想到了一个简单的对象,该对象只有一个成员,并且覆盖了等号和哈希码.

While re-factoring I realized that I came up with a simple object that just has one member and an overridden equals and hashcode.

可能还可以,就像@Arseny已经说过的那样,ValueObject是一种众所周知的工作方式,尽管在编写代码时通常不会得到很多.如果您的多个对象没有任何行为,则可能表明存在所谓的贫血域模型,您必须小心(更多复杂性没有明显好处).

Might be okay, as @Arseny already said, the ValueObject is a well-known way of working, although I usually don't end up with a lot of them when I write code. If more than a few of your objects doesn't have any behaviour, this might be an indication of a so-called Anemic Domain Model, which you have to be careful for (more complexity at no apparent benefit).

您可以找出自己是否做错了"(当然,变量值是错误"):只需查看协作者对ValueObject所做的工作,并查看是否存在类似于计算的任何内容实际上属于对象本身.

You can find out if you're "doing it wrong" (with variable values of "wrong", of course): just see what the collaborators are doing with your ValueObject, and see if there's anything there that resembles a calculation which actually belongs to the object itself.

但是,如果这是不包含任何行为的少数几个对象之一:是的,发生了,您可能不必担心它.但是,我们必须看到一些代码才能定论.

However, if this is one of the few objects that doesn't contain any behaviour: well, yeah, that happens and you probably don't have to worry about it. We'd have to see some code to be conclusive in our anwers though.

这篇关于没有行为的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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