Java方法复杂性

huangapple go评论79阅读模式
英文:

Java method complexity

问题

最近我开始编写一个Java库,用于访问Allegro(电子商务店铺)的REST API,以便在将来的项目中使用。

我遇到了一些问题,比如从复杂性的角度来看,通过参数获取报价对我来说非常困难。

基本上有多个查询参数,人们可以使用这些参数在该服务中查找报价。

是否有可能创建一个通用方法,可以接受x个参数(我不是在说String等),并且在不指定参数对应关系的情况下处理它们?

示例:

卖家ID(作为唯一参数),或者卖家ID和限制参数,这会破坏参数顺序。

我想提到,我考虑过创建多个方法,但使用另外的参数,但是使用12个不同的参数及其组合会变得非常混乱。

另外,如果术语有任何不正确之处(外语),我想道歉。

英文:

recently I've started coding a java library of rest-api of Allegro (e-commerce shop), to use it in further projects.

I've encountered that some things like getting offers by parameters is pretty difficult to me from the aspect of complexity.

Basically there are multiple query parameters that people can use to find offers in that service.

Is there any possibility to make an universal method that can take x number of parameters (I'm not talking about String... etc.), and process them without specifying which argument is to which parameter?

Example:

Seller.Id (as only argument), or Seller.ID and limit which is gonna mess the argument order.

I want to mention that I was considering making multiple method but with another arguments that the method takes in, but taking 12 different arguments and combinations of them would make a lot of mess.

Also, I want to apologize if any of the terminology is incorrect (foreign language).

答案1

得分: 0

尝试创建一个类(DTO),将这些参数作为属性。只将此类作为参数传递给您的方法(POST或PUT)。对于GET或DELETE,仅使用一个ID编号...

英文:

Try to create a class (DTO) with this arguments as attributes. Put only this class as argument to you method (POST or PUT). For GET or DELETE use only an ID number...

答案2

得分: 0

你可以通过将参数分组到单独的对象中来降低这种复杂性。例如,你可以将以下参数分组:

  1. 将 seller.id、seller.login 分组到 SellerInfo 对象中
  2. 将 category、phrase、searchmode、offset 和 limit 分组到 SearchInfo 对象中

使用对象作为参数,即使在将来需要添加/删除参数的情况下,也无需更改方法定义,而只需更改对象内的属性。

使用哈希映射(Hashmap)是另一种选择。然而,这会增加管理键/值对的开销。如果你不想创建第一个选项中建议的额外自定义对象,那么这是一个不错的选择。

英文:

You can reduce this complexity by grouping the parameters into separate objects. For instance, you could group

  1. seller.id, seller.login into SellerInfo object
  2. category, phrase, searchmode, offset and limit into SearchInfo object

Using objects as parameters would help even in future, if in case you need to add/remove parameters, you would not need to change the method definition, rather the attributes inside the objects.

Using a Hashmap is another option. However, that will increase the overhead of managing key/value pairs. This is a good option if you do not want to create additional custom objects suggested in the first option.

huangapple
  • 本文由 发表于 2020年9月5日 05:34:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/63748270.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定