将一个请求对象作为Method Signature参数,它构成所有必需参数 [英] Having one request object as a Method Signature parameter, which constitute all the required parameters

查看:1049
本文介绍了将一个请求对象作为Method Signature参数,它构成所有必需参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

方法签名是方法声明的一部分。它是方法名称和参数列表的组合。

A method signature is part of the method declaration. It is the combination of the method name and the parameter list.

因此,我只想传递构成所有参数的请求对象,而不是指定参数列表。对于所有方法可能都不是这样,但是想要尽可能地尝试。

So instead of specifying a list of parameters, I just want to pass a request object which constitute all the parameters. It might not be true for all the methods, but want to try wherever it is possible.

比如说

public void setMapReference(int xCoordinate, int yCoordinate)
{
  //method code
}

可以也写成

public void setMapReference(Point point)
{
  //method code
}







class Point {
  int xCoordinate;
  int yCoordinate;
  boolean isValidPoint();
}






但来电者可能会混淆因为他不知道参数.. !!


But the caller may confuse as he is not aware of the parameters..!!

这是一个好习惯吗?

推荐答案

我不会在任何可能的地方这样做 - 但这通常是个好主意,是的。基本上,问问自己参数本身是否构成一个连贯的单一实体:将它们混为一谈并将它们视为单一的事物是否有意义?如果是这样,封装它们听起来是个好主意。如果存在明显的行为,事物可以承担责任,以避免代码生活在已经承担其他责任的类中,那就更好了。

I wouldn't do it "wherever it is possible" - but it's often a good idea, yes. Basically, ask yourself whether the parameters themselves constitute a coherent single entity: does it make sense to lump them together and think of them as a single "thing"? If so, encapsulating them sounds like a good idea. It's even better if there's obvious behaviour which that "thing" could take responsibility for, to avoid that code living in a class which already has other responsibilities.

编辑:注意我不会 Point 类型具有您显示的包访问字段:我将它们作为包含属性的私有字段,如正常。如果可能的话,我会尽量让它变得不可变。

Note that I wouldn't let the Point type have package-access fields as you've shown: I'd make them private fields with properties, as normal. I'd try to make it immutable if possible.

这篇关于将一个请求对象作为Method Signature参数,它构成所有必需参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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