开发者

OOP: Passing an object vs. passing attributes into a method

In terms of a clean design, which is preferable?

1) Passing an object, say yourself, to another class method which will manipulate the attributes of the passed object directly.

Inside Class A...

B.doStuff(this);

2) Pass the object's attributes instead, and either assign the return back to the attributes, or pass by reference.

Inside Class A...

this.var1 = B.doStuff(this.var1);

The downside to the first method is ambiguity, it isn't explicit what B is altering in A. The downside to the second method is that unless attributes are passed by reference/pointers, then an array would need to be returned, and would also constitute a longer function call if you end up passing a lot of attributes. I'm guessing the correct choice is dependant on the situation, but are there other advantages/disadvantages anyone else can come up with before I make a decision?

EDIT: Due to the fact that t开发者_如何学编程here will be a large amount of attributes passed to the second class (I am delegating certain tasks to it), I think it warrants the use of the first method in this case. Another downside I came up with was that Class B can only access public attributes and methods in Class A, whereas the second method allowed Class B to manipulate private attributes that Class A may not wish to reveal to the public. But once again, using the second method is more common and certainly more preferable in cases where only a few variables will be accessed.


You should opt for that design which provides least amount of information to the function but optimum enough for the working of the function. This ensures least amount of side effects to expect from the processing.

I think this question comes down to whether to pass value by reference or by value.

Mostly the functions are supposed to least to no side effects but if your Intention of function is to process the Data being passed in the arguments then passing the whole class reference is more better.

For Example when you take a reference to a Graphic objects in a Forms paint event (in C#), it is known that the manipulation will be done on the Object reference passed(But surely it depends upon your requirement and Implementation).

If your function can work properly without requirement of whole Class reference being passed then you should again opt for that. But remember passing lots of parameters by value are more resource intensive as a new copy is to be created every time and so new Memory is to be allocated, where as in by reference you are just passing a reference so no New memory is being allocated.

You second design can be used if you want your method to have no side effect on the data passed instead it works on the data provided and creates a new Object that contains the result, which you in turn assign to required attributes.

Usually the perception of the readers of your code is that, if there is some function being called on the Right side of the assignment operator then he know that something is done with data but a new Data is being returned and hence he generally doesn't expect it to do manipulation to the data provided as Parameter. If reader only see a function call without any assignment operator then he expect that something is done to the data being provided as Parameter.


You are absolutely right. The choice really depends on the situation here. If you are using ORM like Hibernate, it is common for a parent domain class to write something like this to configure a bidirectional dependency with the children domain class:-

public void addChild(Child child) {
    children.add(child);
    child.setParent(this);
}

For most of other cases, it is best to make your method parameter explicit... in another word, pass only the field(s) you need into the method to perform the task. It makes the class diagram easier to comprehend, and you know what exactly the API is trying to accomplish because you know the actual input and the generated output.


I would prefer the second approach, then class B wouldn't have any dependency of class A.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜