一种方法有多个参数还是多种方法有一个参数?
想象一下用于跟踪对手之间游戏的“游戏”类。使用 1 种基于用户输入参数检索游戏的方法是 OOP 更好,还是使用特定于检索目标的多种方法更好?
class Games {
function get_games($game_id = NULL, $stadium_id = NULL, $start_date = NULL,
$end_date = NULL, $count = 999); {}
}
VS
class Games {
function get_all_games($count = 999); {}
function get_game_by_id($game_id = 1); {}
function get_games_by_stadium($stadium_id = 1); {}
function get_games_by_dates($start_date = NULL; $end_date = NULL) {}
}
好处的解释和任何编码/snytax 技巧将不胜感激。谢谢。
Imagine a "Games" class used to track games between opponents. Is it better OOP to have 1 method to retrieve games based on user input parameters or is it better to have multiple methods specific to the retrieval goals?
class Games {
function get_games($game_id = NULL, $stadium_id = NULL, $start_date = NULL,
$end_date = NULL, $count = 999); {}
}
VS
class Games {
function get_all_games($count = 999); {}
function get_game_by_id($game_id = 1); {}
function get_games_by_stadium($stadium_id = 1); {}
function get_games_by_dates($start_date = NULL; $end_date = NULL) {}
}
Explanation of benefits and any coding / snytax tips would be appreciated. Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我练习 OOP 的次数越多,我就越发现自己遵循将参数传递给方法的规则。有点像有很多级别的嵌套 if 语句,我发现如果我有两个以上,我可能会做错什么。
保持代码简单。您正在编写一个执行某些操作的方法,而不是执行所有操作的过程代码块。如果你想玩游戏,那就玩游戏吧。如果您想获取某个日期范围的列表,那么就这样做。
不过我想指出的是,您实际上并不需要 get_all_games() - 您可以只允许 get_games_by_dates() 不带参数地传递。如果它没有得到任何,那么它会得到从永远开始的每个日期的游戏(所有游戏)
The more I practice OOP the more I find myself following a rule about passing parameters to methods. Kind of like having many levels of nested if statements, I find that if I have more than two I might be doing something wrong.
Keep your code simple. You're writing a method that does something, not a block of procedural code that does everything. If you want to get a game, then get a game. If you want to get a list for a date range, then do that.
However I would point out that you don't really need get_all_games() - You can just allow for get_games_by_dates() to be passed with no parameters. If it doesn't get any then it would get the games for every date since forever (all the games)
我总是倾向于 OOP 代码。原因是它使您的代码更易于维护和阅读。拥有的功能越多,以后执行代码就越容易
I would always err on the side of OOP code. the reason being is that it makes you code much easier to maintain and read. The more functions you have the easier it is to follow code later on down the road
“特定于检索目标的多种方法”的好处是您可以添加/删除目标。使用带有一堆参数的单一函数的问题在于,如果您决定添加/删除一种获取游戏的方式,则必须更改界面。这会破坏任何使用它的代码。
每种方法应尽可能简洁,仅执行一项功能。
The benefit of "multiple methods specific to the retrieval goals" is that you can add/remove goals. The problem with using one monolithic function with a bunch of parameters is that, should you decide to add/remove a way to get games, you'd have to change the interface. Which would break any code that uses it.
Each method should be as concise as possible, performing only one function.
我会选择单独的方法,因为您使用了大量具有默认值的参数。
如果你想获得所有游戏,你必须这样做:
I would go for separate methods since you are using lots of paramaters with default values.
If you want to get all games you would have to do:
假设您的
get_....()
函数返回所有游戏数据,我会根据传入的 id 编写一个函数来返回此数据,并编写一系列find_...()
函数返回找到的 id 数组。这将带来额外的好处,即可以更轻松地重写后代类中的数据检索代码。然后您可以致电:
Assuming that your
get_....()
functions are returning all game data, I would write a single function to return this data, based on an id passed in, and write a series offind_...()
functions to return an array of found ids. This will have the added benefit of making it easier to override the data retrieval code in decendant classes.You can then call: