Can I code a factory pattern By using an abstract class?
for example I am parsing emails, there are multiple type of emails, each email type has its own parser class, all parser classes extend main abstract parser class that has some common code.
For each Emails I read from the server I call the factory (EmailParserFactory) to get the appropriate Email Parser
The thing is that with factory pattern the factory relies on a common interface (in the classic Java factory pattern the "Shape" is the interface )
Factory UML
In my example I am maintaining some common logic in my main parser so I need it abstract class.
I am not sure factory pattern is the right way to go here.
The current design I came up with as follow:
EmailParser parser EmailParserFactory.getAppropriateEmailParser(email) ;
ParsedEmail = parser.getParsedEmail(email) -- parser is already the appropriate parser, sending it the email message will get back a parsed object.
Objects:
EmailParserFactory
private getEmailType(emailMessage) -- will check email type
- if email subject contains feedback its a feedback email
- if the email contains GUID then its a reply email etc
EmailParser - abstract class
getBodyText() - common for all
ComplaintEmailParser - specific parser for Complaint Email
parseEmail()
FeedbackEmailParser - specific parser for Feedback Email
parseEmail()
ReplyEmailParser - specific parser for Reply Email
parseEmail()