返回一个新对象与修改作为参数传入的对象

发布于 2024-09-13 19:00:13 字数 497 浏览 1 评论 0原文

我在代码审查期间遇到了以下代码片段。

我的直觉告诉我,这不符合正确的 OOP。

我认为 LoadObject 方法应该返回一个新的 SomeObject 对象,而不是修改传递给它的对象。尽管我无法真正找到为什么这样做更好的正确解释。

我的解决方案更好吗?如果是的话为什么?具体来说,给定的代码示例中违反了哪些 OOP 原则或标准(如果有)?

   public void someMethod()
    {
        ...
        var someObject = new SomeObject();
        LoadSomeObject(reader,someObject);
    }

    private void LoadSomeObject(SqlDataReader reader, SomeObject someObject)
    {
       someObject.Id = reader.GetGuid(0);
    }

I came across the following piece of code during a code review.

My intuition is telling me that this isn't following proper OOP.

I'm thinking that instead the LoadObject method should return a new SomeObject object, instead of modifying the one passed into it. Though I can't really find a proper explanation of why this is better.

Is my solution better? and if so why? specifically what OOP principles or standards are broken in the given code example (if any) ?

   public void someMethod()
    {
        ...
        var someObject = new SomeObject();
        LoadSomeObject(reader,someObject);
    }

    private void LoadSomeObject(SqlDataReader reader, SomeObject someObject)
    {
       someObject.Id = reader.GetGuid(0);
    }

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

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

发布评论

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

评论(6

浅唱々樱花落 2024-09-20 19:00:13

代码的编写方式没有任何问题,因为您只是修改了 someObject 的属性。

然而,在 LoadSomeObject 中创建 someObject 并返回它也是正确的。

在这一点上,两种选择都是正确的。

There's nothing wrong with the way the code is written since you are only modifying a property on someObject.

However, it would be just as correct to create someObject within LoadSomeObject and return it.

At this point, both choices are just as correct.

凯凯我们等你回来 2024-09-20 19:00:13

我不是 OO 大师,所以对这些持保留态度。

  1. 对象应尽可能自行管理/定义其行为。如果您曾经了解过松散耦合,那么这背后的原理是显而易见的。将 LoadSomeObject 的细节转移到 SomeObject 的实现中可能是更好的设计决策,但是对于这样一个通用示例来说,这很难讨论。

  2. 可变状态在任何命令式代码(包括面向对象代码)中都是完美的,它是这些范例的核心“功能”。 OTOH,不可变状态具有不可描述的优势(我想我们对这个主题有几个问题,否则就问任何 FP 拥护者),并且拥有一些不可变对象并不是特别非 OO。

编辑:您也可以将 reader 传递给 SomeObject 的构造函数。

I'm not an OO guru, so take these with a grain of salt.

  1. objects should manage themself/define their behaviour themselves as far as possible. The rationale behind this is obvious if you ever head of loose coupling. I may very well be the better design decision to move the details of LoadSomeObject to the implementation of SomeObject, but that's hard to discuss for such a general example.

  2. Mutable state is perfectly fine in any imperative code (including OO code), it's a core "feature" of these paradigms. OTOH, immutable state has undeinable advantages (I guess we have a few questions on that topic here, otherwise ask any FP advocate), and having some immutable objects is not especially non-OO.

Edit: You may as well pass reader to SomeObject's constructor.

面犯桃花 2024-09-20 19:00:13

不存在通用的面向对象概念,这与类似的代码相冲突。
但稍后您会发现,如果您不遵循一些设计原则,那么很难收集和理解操作 SomeObject 实例的方法。
对于初学者来说,最简单的方法可能是分离两种主要类型的过程:

  • 功能性 - 旨在创建新实例而不是改变其他对象。
  • Methodic - 旨在更改其主机实例的状态。

这里的好主意是,如果您想分离 SomeObject 操作逻辑,可以使用

 public void loadFromReader( SqlDataReader reader )

方法和方法

创建 SomeObjectBuilder 类型

公共 SomeObject getValue()

属性

There isn't universal OO concept, that is conflicting with code like that.
But later you'll find that is hard to collect and understand way to manipulate of SomeObject instances if you will not follow some design principles.
Maybe easiest way for starter is to separate two main kind of procedures:

  • Functional - that is designed to create new instances and not to mutate other objects.
  • Methodic - that is designed to change state of it's host instance.

So good idea here, if you want to separate SomeObject manipulate logic is to create SomeObjectBuilder type with

 public void loadFromReader( SqlDataReader reader )

method and

public SomeObject getValue()

property

っ〆星空下的拥抱 2024-09-20 19:00:13

无论哪种方式都是完全可以接受的,但是在决定哪种方式适合您的特定情况时,需要考虑您希望如何处理返回的对象。不变性,即在每个操作中创建一个新实例是 .NET 中字符串的处理方式。

复制方法显然需要返回一个副本。作为作用于单例对象的方法,例如由全局引用保存的方法,不适合返回新对象,因为更改可能会丢失。

Either way is perfectly acceptable but considerations about what you wish to do with the returned object come in to play when deciding which is right for your particular situation. Immutability i.e creating a new instance in every operation is how strings in .NET are handled.

A copy method would obviously need to return a copy. Where as a method that works on a singleton object for example that is held by a global reference would not suit returning a new object as the changes may be lost.

毁梦 2024-09-20 19:00:13

如果 LoadSomeObject 要返回新的 SomeObject,您可能需要更改方法名称以避免混淆。也许到NewAndLoadSomeObject

If LoadSomeObject is going to return a new SomeObject, you might want to change the method name to avoid confusion. Maybe to NewAndLoadSomeObject?

林空鹿饮溪 2024-09-20 19:00:13

我确实同意你的直觉,在大多数情况下感觉有点不对劲。我同意返回一些东西更好,因为这样可以更清楚地表明某些东西正在被修改。看到正在使用的返回值可以立即清楚发生了什么。

不过,我认为这感觉还是好一点(无论如何在大多数情况下):

public void someMethod()
{
    ...
    var someObject = new SomeObject();
    someObject.Load(reader);
}

然后显然在 SomeObject 类中,您会发现

public void Load(SqlDataReader reader)
{
   this.Id = reader.GetGuid(0);
}

这个想法是支持实例方法而不是静态方法。当您可以让对象对其自己的数据进行操作时,为什么还要创建一个对象并传递其数据呢?

I do agree with your intuition, that it feels a bit off for most cases. I would agree that returning something is better, as just makes it more clear that something is being modified. Seeing a returned value being used makes it immediately clear what is going on.

I think this feels a little better still, though (in most cases anyway):

public void someMethod()
{
    ...
    var someObject = new SomeObject();
    someObject.Load(reader);
}

And then obviously in the SomeObject class, you would have

public void Load(SqlDataReader reader)
{
   this.Id = reader.GetGuid(0);
}

The idea is to favour instance methods over static ones. Why bother creating an object and passing its data around when you can just have the object operate on its own data?

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