Passing parameters is common in daily programming, but should we pass parameters as object or values?
(A)
public boolean isGreaterThanZero(Payment object) {
if (object.getAmount() > 0) {
return true;
}
return false;
}
(B)
public boolean isGreaterThanZero(int amount) {
if (amount > 0) {
return true;
}
开发者_StackOverflow return false;
}
Neither of those.
With proper OOP, you would have isGreaterThanZero() on the Payment object, ie:
class Payment {
int amount
public boolean isGreaterThanZero() {
return amount > 0
}
}
and then:
Payment payment
boolean flag = payment.isGreaterThanZero()
Assuming that you are asking about parameter passing, and not about object design...
You need to consider a number of aspects when designing a method.
- Can the method be used in other places? In your example, the answer could be yes - it can tell if any int is greater than zero - in which case (B) is more useful.
- If the method needs more than one parameter and is specific to a certain object in your code, then pass the object.
Where it makes sense, try to reduce dependencies between modules in your code. In your example, (A) is introducing an unnecessary dependency on the Payment object. But if the method needed a couple of member variables of the Payment object (because it was specific to a payment) then by all means, pass a Payment object as the parameter.
This is a contrived example, but if this is the only interaction the receiving class has with Payment, I would use the value in the interest of decoupling. No point creating superfluous class dependencies.
EDIT: Marcel and I were typing the same thing at the same time. :) This.
The answer to this question is going to be specific to a given situation so there is no way to give a definitive answer, however the question of when to use which approach is still valid.
I would pass an object when:
- The method needs to deal with a large amount of data passed in from outside. It is desirable to wrap these parameters in an object because doing so means that you can have the method take one parameter and not 1 per data value. You can also re factor the parameter validation logic into the wrapper class for better locality.1.
- When the same combination of parameters is used frequently throughout an API
I would pass a value when there are few parameters, and all of them are simple value types.
As per OOP concepts, we should use Objects
. But, don't overuse it. For example, in your example, I would have used an int
. But in a method where I am using multiple values in many places, I would have used an object.
I wouldn't create a Payment
class just with one data member, "amount
".
My goal is to keep things decoupled. If all isGreaterThanZero() really needs is the amount, then the int version is better, because it doesn't couple the implementation to the Payment class.
I think we should use primitive types whenever possible (int, double, boolean, char) as parameters because it's not an accident that there are primitive types. I prefer the second option, because it's more general (it's not forcing you to provide a Payment object, any int value will be good enoug). But when you need the state of the object (at least two values of the object's data), the situation might be changed, you can either use values as parameters for a more general usage possibility or objects as parameters for simplicity.
Yes,we can pass object as parameter in the Java Program .
First Way
class demo
{private int length=1;
private int breadth=1;
private int area;
void input(int length,int breadth)
{
this.length=length;
this.breadth=breadth;
}
void add(demo d1,demo d2)
{
length=d1.length+d2.length;
breadth=d1.breadth+d2.breadth;
}
void output()
{
System.out.println("\nLength="+length+"\nBreadth="+breadth);
}
public static void main(String args[])
{
demo d1=new demo();
demo d2=new demo();
d1.input(1, 1);
d1.output();
d2.input(2, 2);
d2.output();
demo d3=new demo();
d3.add(d1, d2);
d3.output();
}
}
Second Way
class demo
{private int length;
private int breadth;
void input(int length,int breadth)
{
this.length=length;
this.breadth=breadth;
}
demo add(demo d2)
{ demo obj=new demo();//Neccesary as we want to return complete
object irrespective of its number of data fields
obj.length=length+d2.length;//Storing the length of two objects
in the obj
obj.breadth=breadth+d2.breadth;
return obj;
}
void output()
{
System.out.println("\nLength="+length+"\nBreadth="+breadth);
}
public static void main(String args[])
{
demo d1=new demo();
demo d2=new demo();
d1.input(1, 1);
d2.input(2, 2);
demo d3=d1.add(d2);//Here we created new object d3 called
add() by d1 and passed d2 object through it
d3.output();
}
}
Sometimes, Passing the object address(or a pointer, a handle, whatever you call it) as parameter and then to cast it in the method (as an instance of the recquired object) is faster then passing the object as parameter. It just a way to constrain the way it will be compiled. If sometimes it doesn't change anything, in other cases passing the adress will widely make the program faster. It's a low level compiler stuff but IT really CAN make the code faster.
Addenum for my previous answer. It also depends on compiler optimisations and the method call context. If the object is already defined before the call to the method or if it's a method that could benefits from inlining and optimisations it's not a good idea to use the adress. If you are in a loop, then it's a good idea to use the adress. You have to think a bit about how the method will be used in the specific program ( = anticipate how it will be compiled).
精彩评论