类层次结构设计(class hierarchy design)

编程入门 行业动态 更新时间:2024-10-26 20:34:05
类层次结构设计(class hierarchy design)

我想为我的Java项目定义一个类层次结构。 我的意图基本上是从电子邮件帐户获取电子邮件,并使用IMAP或POP3将其存储到数据库,具体取决于网站(例如gmx支持IMAP和yahoo支持POP3 )。 我正在使用javamail API。 假设我有2个名为IMAP和POP3子类。 他们的相应方法如下:

POP3

Execute_Parser Fetchemails CreateMSGDigest Get_Foldername Scan_Table Store_Emailinfo

IMAP

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_Emailinfo

IMAP

FetchEmails CreateMSGDigest Scan_Table Store_Emailinfo

As 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.

更多推荐

本文发布于:2023-08-07 17:44:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1465502.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:结构设计   层次   class   hierarchy   design

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!