简体   繁体   中英

class hierarchy design

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 eg 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

  1. Execute_Parser
  2. Fetchemails
  3. CreateMSGDigest
  4. Get_Foldername
  5. Scan_Table
  6. Store_Emailinfo

IMAP

  1. FetchEmails
  2. CreateMSGDigest
  3. Scan_Table
  4. 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 )?

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 (eg 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.

tl;dr Subclassing does not make sense because there is no IS-A relationship between POP3 and IMAP

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. The question in your context is if there is an IS-A relationship between IMAP and POP3 protocol. 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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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