0

I'm trying to build a customer email generator in java using the abstract factory pattern. I understand how to use the factory method pattern; however, I'm a bit confused about the abstract factory pattern. I'm trying to generate an email based on customer type. Could you look at my code below and tell me if I'm using the abstract method correctly? Thank you

public abstract class EmailTemplate {
   public abstract String getHeader();
   public abstract String getBody();
   public abstract String getFooter();

   public String generateEmail(){
      return getHeader()+"\n"+getBody()+"\n"+getFooter();
   }
}
public interface EmailFactory {
    EmailTemplate createEmail();
}
public class BusinessEmail extends EmailTemplate {
  @Override
  public String getHeader() {
    return "Dear [business customer],";
  }

  @Override
  public String getBody() {
    return "Thank you for being our valued customer. We are so grateful for the pleasure of serving you and hope we met your expectations.";
  }

  @Override
  public String getFooter() {
    return "Best Regards," +
            "[name]";
  }
}
public interface EmailGeneratorFactory {
    EmailTemplate createEmail();
}
public class BusinessFactory implements EmailGeneratorFactory {
    @Override
    public EmailTemplate createEmail() {
        return new BusinessEmail();
    }
}
public class EMailGenerationSystem {
    private static EMailGenerationSystem EMailGenerationSystem = new EMailGenerationSystem();
    private EMailGenerationSystem(){};
    public static EMailGenerationSystem getInstance(){
        return EMailGenerationSystem;
    }
    public EmailTemplate getEmail(EmailGeneratorFactory factory){
        return factory.createEmail();
    }
}
rafiaTech
  • 433
  • 1
  • 6
  • 15
  • 1
    Does your code produce correct customer emails? – Gilbert Le Blanc Apr 04 '20 at 22:36
  • Yes it does. I just want to find out if I'm using the abstract factory pattern correctly. – rafiaTech Apr 04 '20 at 23:23
  • So this code works correctly, you just want feedback on it? I think this question is off-topic on stackoverflow, there's a separate site for code feedback - https://codereview.stackexchange.com/ – Joni Apr 04 '20 at 23:58
  • This is not exactly the Abstract Factory pattern. I show examples of that, as well as Factory Method, [here](https://stackoverflow.com/a/50786084/1371329). – jaco0646 Apr 05 '20 at 15:06
  • Thank you for your feedback. I followed the example here https://www.journaldev.com/1418/abstract-factory-design-pattern-in-java – rafiaTech Apr 05 '20 at 18:27
  • That example looks like nonsense. There is a misleading but pervasive myth that an Abstract Factory is, "a factory of factories". Furthermore, I would avoid any tutorial that uses the phrase, "factory design pattern". There is no design pattern named _factory_. The term _factory_ describes a category of different design patterns, each with its own specific name. – jaco0646 Apr 06 '20 at 17:13

0 Answers0