简体   繁体   English

类层次结构设计

[英]class hierarchy design

I want to define a class hierarchy for my Java Project. 我想为我的Java项目定义一个类层次结构。 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 eg gmx supports IMAP and yahoo supports POP3 ). 我的目的基本上是从电子邮件帐户中获取电子邮件,并根据网站的不同,使用IMAPPOP3将它们存储到数据库中(例如gmx支持IMAP而yahoo支持POP3 )。 I am using javamail API for this. 我为此使用javamail API。 Suppose I have 2 sub classes named IMAP and POP3 . 假设我有两个名为IMAPPOP3子类。 Their corresponding methods are as below: 对应的方法如下:

POP3 POP3

  1. Execute_Parser Execute_Parser
  2. Fetchemails 提取电子邮件
  3. CreateMSGDigest 创建MSG摘要
  4. Get_Foldername Get_Foldername
  5. Scan_Table 扫描表
  6. Store_Emailinfo Store_Emailinfo

IMAP IMAP

  1. FetchEmails 提取电子邮件
  2. CreateMSGDigest 创建MSG摘要
  3. Scan_Table 扫描表
  4. Store_Emailinfo Store_Emailinfo

As you can see POP3 needs to implement 2 extra methods which are not needed by IMAP . 如您所见, POP3需要实现IMAP不需要的2种额外方法。 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. 我猜这两个类都通用的所有IMAP方法。 but then what about other two methods of POP3 (Execute_Parser and Get_Foldername )? 但是POP3其他两种方法(Execute_Parser和Get_Foldername)又如何呢?

You might want to add all 6 methods in your base class, and provide a default empty implementation. 您可能要在基类中添加所有6个方法,并提供默认的空实现。 In your Pop3 implementation, you override all 6 methods, and in the Imap class, only the 4 you need. Pop3实现中,您将覆盖所有6种方法,而在Imap类中,仅需要4种方法。

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 (eg boolean isUseParserRequired () ). 现在,根据您的需要,您可能想添加其他方法来查询对象,是否需要调用Get_FoldernameExecute_Parser方法(例如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. 如果我们假设您创建了一个EmailProtocol抽象基类,并想以一种通用的方式使用它,那么您可能需要添加这样的辅助方法。 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. 它将允许您在处理EmailProtocol实例时使用通用接口,而不必依靠if/else确定您拥有哪种实例,然后调用适当的方法。

And as JB Nizet mentions, you should definitely stick to Java naming conventions. 正如JB Nizet所提到的,您绝对应该坚持Java命名约定。

tl;dr Subclassing does not make sense because there is no IS-A relationship between POP3 and IMAP tl; dr子类化没有意义,因为POP3和IMAP之间没有IS-A关系

When using subclasses there are quite different semantics/motivations involved. 使用子类时,涉及的语义/动机非常不同。 One is that you try to model some natural IS-A relationship between two entities in your context. 一种是您尝试为上下文中的两个实体之间的某些自然IS-A关系建模。 The question in your context is if there is an IS-A relationship between IMAP and POP3 protocol. 您所处的问题是IMAP和POP3协议之间是否存在IS-A关系。 The answer here is no! 答案是否定的! The IMAP protocol does not derive from POP3, it's not compatible etc. Another semantic of class hierarchies concentrates on conceptual interfaces which means that you model a class hierarchy to actually use instances of classes in a uniform and polymorphic way. IMAP协议不是从POP3派生的,它不兼容等。类层次结构的另一种语义集中在概念性接口上,这意味着您可以对类层次结构进行建模以实际以统一和多态的方式使用类的实例。 An alternative to this approach is do not use subclassing which leads to a tight coupling of the classes but to use interfaces and share common code via composition patterns. 此方法的替代方法是不使用子类,而子类会导致类的紧密耦合,而是使用接口并通过组合模式共享公共代码。

I'd go for using interfaces and share common code via composition because with this approach you avoid a tight binding of classes that don't have an IS-A relationship. 我会选择使用接口并通过组合来共享通用代码,因为使用这种方法,可以避免没有IS-A关系的类的紧密绑定。

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

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