简体   繁体   English

(Java)重载:多少是太多?

[英](Java) Overloading: How much is too much?

I'm using Java to generate JCL. 我正在使用Java生成JCL。 I have four methods for adding a data definition statement. 我有四种添加数据定义语句的方法。 One accepts a char, one a string, one an array (of Strings), and the other has nothing for the second parameter. 一个接受一个字符,一个接受字符串,一个接受(字符串的)数组,另一个接受第二个参数。

public void addDD (String label, char classChar) //Generates: SYSIN DD SYSOUT=[classChar]
public void addDD (String label, String dataset) //Generates: SYSIN DD DSN=[dataset]
public void addDD (String label) //Generates: SYSIN DD DUMMY
public void addDD (String label, String datasets[]) //Generates: SYSIN DD *
                                                    // DSN=[datasets[0]],
                                                    // DSN=[datasets[1]]

The concern I have is that these methods don't just accept different parameter types; 我担心的是,这些方法不仅接受不同的参数类型,还接受其他参数类型。 the entire method changes based upon the type. 整个方法根据类型而变化。 If it's a string, you need "DSN=". 如果是字符串,则需要“ DSN =“。 If it's a char, you need "SYSOUT=". 如果是字符,则需要“ SYSOUT =“。 At the same time, I don't want the client to worry about using a different method name for each of these scenarios. 同时,我不希望客户担心每种情况下使用不同的方法名称。

Is my current design considered bad practice or good? 我当前的设计被认为是不好的做法还是好的?

These decisions are always partly a matter of taste. 这些决定总是部分取决于品味。

In this particular case, I would rather change the method names as they really do something different. 在这种情况下,我宁愿更改方法名称,因为它们确实做了一些不同的事情。 Most of the time overloading methods like this is just convenience: having a char or String variant mostly means that the char will be converted to a string and the effect will be the same, which isn't the case in your example. 像这样的大多数时间重载方法只是为了方便:使用char或String变体通常意味着char将被转换为字符串,并且效果将相同,在您的示例中情况并非如此。

There might be other reasons for doing this like you did (using reflection to find the correct method), but in that case I would recommended writing clear Javadoc describing that the methods actually do to avoid confusion. 这样做的原因可能还有其他原因(使用反射来找到正确的方法),但是在那种情况下,我建议编写清晰的Javadoc来描述该方法实际上是在避免混淆。

What you're doing is quite dangerous - you counting on the caller of addDD(String, char) to read the javadocs and figure out he shouldn't be using addDD(String, String) instead. 您正在做的事情非常危险-您指望addDD(String, char)的调用者读取javadocs,并弄清楚他不应该使用addDD(String, String)代替。

Option 1 : rename the methods: addDDSysout() , addDDDsn() etc. 选项1 :重命名方法: addDDSysout()addDDDsn()等。

Option2 : implement all these with just a single method: Option2 :仅使用一个方法即可实现所有这些功能:

addDD(String label, String... args)

and make very sure you can handle anything thrown at it - no strings in args, a single string, multiple strings, a single character etc. 确保您可以处理抛出的所有内容-args中没有字符串,单个字符串,多个字符串,单个字符等。

I have to agree that these things are often a matter of taste. 我必须同意,这些东西通常只是一个品味问题。 But I'm not sure the other responders understood the significance of a JCL DD statement. 但是我不确定其他响应者是否理解JCL DD语句的重要性。 Because I disagree with them based on the domain of your problem. 因为根据您所遇到的问题,我不同意它们。

I like your design. 我喜欢你的设计。 I would discourage different method names. 我不鼓励使用不同的方法名称。 That is basically what TSO did with FILE(xxx) vs DSN(xxx) and I always hated it. 基本上,这就是TSO对FILE(xxx)与DSN(xxx)所做的事情,我一直讨厌它。 While you're methods produce different "results" in the input reader stream, these differences are forced by the syntax of JCL. 当您的方法在输入阅读器流中产生不同的“结果”时,这些差异是由JCL的语法造成的。 To be more specific they don't do different things . 更具体地说,他们不会做不同的事情 Fundamentally you are declaring a data source/sink to a batch job. 从根本上来说,您是在声明一个批处理作业的数据源/接收器。 Don't make your user learn 4 different methods to do that. 不要让用户学习4种不同的方法来做到这一点。

The number of method signatures needed to cover all DD statement constructs could become quite messy. 覆盖所有DD语句构造所需的方法签名的数量可能会变得非常混乱。

For example, I'm not too sure that method: 例如,我不太确定该方法:

public void addDD (String label, String datasets[]) 

which generates: 产生:

//SYSIN DD *
// DSN=[datasets[0]],
// DSN=[datasets[1]]...

is going to work very well, the DD * indicates inline data to follow, and that needs to be terminated with a /* before concatenating additional datasets (if any). 可以很好地工作, DD *表示要跟随的内联数据,并且在连接其他数据集(如果有)之前需要用/*终止。 The above method should probably generate a simple series of DD statements as follows: 上面的方法可能会生成一系列简单的DD语句,如下所示:

//SYSIN DD
// DSN=[datasets[0]],
// DSN=[datasets[1]]...

What sort of signature would you use to generate an inline dataset? 您将使用哪种签名来生成内联数据集? Probably the same thing: 大概是一样的东西:

public void addDD (String label, String inlinedata[])

to generate: 产生:

//SYSIN DD *
inlinedata[0]
inlinedata[1]...
/*

But both methods have the same signature - that doesn't work. 但是这两种方法具有相同的签名-不起作用。 You might be better off using multiple methods to avoid signature collisions. 您最好使用多种方法来避免签名冲突。 Or if you continue with the overloaded single method, then be sure you have all the possible DD name constructs covered before getting too deep into the swamp. 或者,如果继续使用重载的单个方法,请确保在陷入沼泽之前,已覆盖所有可能的DD名称构造。

Either way, this is going to require a significant amount of documentation for end-users to make sense of (JCL can be a real dog of a scripting language to follow). 无论哪种方式,这都将需要大量文档供最终用户理解(JCL可能是遵循脚本语言的真正狗)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM