samedi 4 avril 2020

Java abstract factory pattern

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();
    }
}

Aucun commentaire:

Enregistrer un commentaire