0

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()
JavaSheriff
  • 7,074
  • 20
  • 89
  • 159
  • 1
    I don't follow what exactly you're asking. You seem to have a devised an architecture that could work. Although I would recommend that your `EmailParser` be an interface rather than a class, there's no reason why you could not have an abstract base class that all your current `EmailParser` implementations extend. Do you have a specific concern? A request for an overall design review would be too broad for SO. – John Bollinger Nov 06 '17 at 22:12
  • 1
    @user648026 - Implementation is near to static factory, except `EmailParserFactory.getAppropriateEmailParser(email)` static call. But if all parsed emails are of same _type_ and can be processed in common, it is a best use case for Factory method with factory method`getEmailParser()`, creator as Abstract EmailProcessor (contains common processing code), concrete creator as SpecificEmailParser. Please refer to my earlier answer to similar question - https://stackoverflow.com/questions/46384358/what-are-the-real-benefits-of-using-the-abstract-factory-in-the-following-exampl/46407888#46407888 – Kedar Tokekar Nov 08 '17 at 00:31

1 Answers1

2

You can use composition instead of inheritance. In fact you have method getBodyText() that no relevant to Parser. So you can extract it to BodyTextExtractor and inject that extractor to all of your Parsers. After that refactoring EmailParser transforms to interface with one method (SRP).