我想为我的Java项目定义一个类层次结构。 我的意图基本上是从电子邮件帐户获取电子邮件,并使用IMAP或POP3将其存储到数据库,具体取决于网站(例如gmx支持IMAP和yahoo支持POP3 )。 我正在使用javamail API。 假设我有2个名为IMAP和POP3子类。 他们的相应方法如下:
POP3
Execute_Parser Fetchemails CreateMSGDigest Get_Foldername Scan_Table Store_EmailinfoIMAP
FetchEmails CreateMSGDigest Scan_Table Store_Emailinfo正如您所看到的, POP3需要实现IMAP不需要的2个额外方法。 对于这两个类,通用方法的实现都是相同的。 任何人都可以在这里建议我应该在基类中放入哪些方法? 我想IMAP所有方法对于这两个类都是常见的。 但那么POP3其他两种方法(Execute_Parser和Get_Foldername)呢?
I want to define a class hierarchy for my Java Project. My intention is basically to get emails from email account and store them to database either with IMAP or with POP3 depending on the website(for e.g gmx supports IMAP and yahoo supports POP3). I am using javamail API for this. Suppose I have 2 sub classes named IMAP and POP3. Their corresponding methods are as below:
POP3
Execute_Parser Fetchemails CreateMSGDigest Get_Foldername Scan_Table Store_EmailinfoIMAP
FetchEmails CreateMSGDigest Scan_Table Store_EmailinfoAs you can see POP3 needs to implement 2 extra methods which are not needed by IMAP. Implementation of common methods will be same for both the classes. Can anyone please suggest here which methods should I put in base class? I guess all methods of IMAP which are common for both the classes. but then what about other two methods of POP3(Execute_Parser and Get_Foldername )?
最满意答案
您可能希望在基类中添加所有6个方法,并提供默认的空实现。 在Pop3实现中,您覆盖所有6个方法,而在Imap类中,只覆盖您需要的4个方法。
现在,根据您的需要,您可能需要添加其他方法来查询对象是否需要调用Get_Foldername和Execute_Parser方法(例如boolean isUseParserRequired () )。 如果我们假设您创建了EmailProtocol抽象基类,并希望以通用方式使用它,则可能需要添加此类帮助程序方法。 它允许您在操作EmailProtocol实例时使用通用接口,而不必依赖if/else来确定您拥有的实例类型,然后调用适当的方法。
正如JB Nizet所提到的,你应该坚持使用Java命名约定。
You might want to add all 6 methods in your base class, and provide a default empty implementation. In your Pop3 implementation, you override all 6 methods, and in the Imap class, only the 4 you need.
Now, depending on your needs, you might want to add other methods to query your object whether you need to call the Get_Foldername and Execute_Parser methods (e.g. boolean isUseParserRequired ()). If we suppose you create a EmailProtocol abstract base class, and want to use it in a generic way, you might need to add a such helper methods. It will allow you to use the generic interface when manipulating EmailProtocol instances rather than having to rely on if/else to determine what kind of instance you have, then call the appropriate methods.
And as JB Nizet mentions, you should definitely stick to Java naming conventions.
更多推荐
发布评论