Skip to main content
added 318 characters in body
Source Link
Sleiman Jneidi
  • 3.2k
  • 11
  • 20

The best way to deal with compulsory and optional parameters is using the Builder pattern.The problem with overloaded functions is that they are confusing and user of the class or even you can make mistakes while calling this method.

Instead of that you can have builder that allows to construct Emails fluently.

class EmailSender{

private final String body;
private final String recipient;
private final List<String> to;
private final List<String> ccs;

public  EmailSender(EMailBuilder mailBuilder){
    this.body = mailBuilder.body;
    this.to = mailBuilder.to;
    this.recipient = mailBuilder.recipient;
    this.ccs = mailBuilder.ccs;

}
public void send(){
    //
}
public  static  class EMailBuilder{

    private final String body;
    private final String recipient;
    private final List<String> to;
    private final List<String> ccs;

    public  EMailBuilder(String body,String recipient,String to){
        this.recipient = recipient;
        this.body = body;
        this.to = new ArrayList<>();
        this.to.add(to);
        this.ccs = new ArrayList<>();

    }

    public EMailBuilder addTo(String to){
        this.to.add(to);
        return this;
    }
    public EMailBuilder addCc(String cc){
        this.ccs.add(cc);
        return this;
    }

    public EmailSender build(){
        EmailSender sender = new EmailSender(this);
        return sender;
    }

 }
}

Something like allows you build things easily, for example:

  EmailSender emailSender = new EmailSender.EMailBuilder("This is an email","me","you")
            .addTo("the Other Chap").addCc("the boss")
            .build();
  emailSender.send();

The problem with your code is it hard to get right and validate. Is it allowed to send an email with a to ? It shouldn't, and hence you need to length of the array.

  if(to.length==0){
   // invalid 
   }

The beauty about the builder pattern is that it tells what's compulsory and what's optional.

The best way to deal with compulsory and optional parameters is using the Builder pattern.The problem with overloaded functions is that they are confusing and user of the class or even you can make mistakes while calling this method.

Instead of that you can have builder that allows to construct Emails fluently.

class EmailSender{

private final String body;
private final String recipient;
private final List<String> to;
private final List<String> ccs;

public  EmailSender(EMailBuilder mailBuilder){
    this.body = mailBuilder.body;
    this.to = mailBuilder.to;
    this.recipient = mailBuilder.recipient;
    this.ccs = mailBuilder.ccs;

}
public void send(){
    //
}
public  static  class EMailBuilder{

    private final String body;
    private final String recipient;
    private final List<String> to;
    private final List<String> ccs;

    public  EMailBuilder(String body,String recipient,String to){
        this.recipient = recipient;
        this.body = body;
        this.to = new ArrayList<>();
        this.to.add(to);
        this.ccs = new ArrayList<>();

    }

    public EMailBuilder addTo(String to){
        this.to.add(to);
        return this;
    }
    public EMailBuilder addCc(String cc){
        this.ccs.add(cc);
        return this;
    }

    public EmailSender build(){
        EmailSender sender = new EmailSender(this);
        return sender;
    }

 }
}

Something like allows you build things easily, for example:

  EmailSender emailSender = new EmailSender.EMailBuilder("This is an email","me","you")
            .addTo("the Other Chap").addCc("the boss")
            .build();
  emailSender.send();

The best way to deal with compulsory and optional parameters is using the Builder pattern.The problem with overloaded functions is that they are confusing and user of the class or even you can make mistakes while calling this method.

Instead of that you can have builder that allows to construct Emails fluently.

class EmailSender{

private final String body;
private final String recipient;
private final List<String> to;
private final List<String> ccs;

public  EmailSender(EMailBuilder mailBuilder){
    this.body = mailBuilder.body;
    this.to = mailBuilder.to;
    this.recipient = mailBuilder.recipient;
    this.ccs = mailBuilder.ccs;

}
public void send(){
    //
}
public  static  class EMailBuilder{

    private final String body;
    private final String recipient;
    private final List<String> to;
    private final List<String> ccs;

    public  EMailBuilder(String body,String recipient,String to){
        this.recipient = recipient;
        this.body = body;
        this.to = new ArrayList<>();
        this.to.add(to);
        this.ccs = new ArrayList<>();

    }

    public EMailBuilder addTo(String to){
        this.to.add(to);
        return this;
    }
    public EMailBuilder addCc(String cc){
        this.ccs.add(cc);
        return this;
    }

    public EmailSender build(){
        EmailSender sender = new EmailSender(this);
        return sender;
    }

 }
}

Something like allows you build things easily, for example:

  EmailSender emailSender = new EmailSender.EMailBuilder("This is an email","me","you")
            .addTo("the Other Chap").addCc("the boss")
            .build();
  emailSender.send();

The problem with your code is it hard to get right and validate. Is it allowed to send an email with a to ? It shouldn't, and hence you need to length of the array.

  if(to.length==0){
   // invalid 
   }

The beauty about the builder pattern is that it tells what's compulsory and what's optional.

Source Link
Sleiman Jneidi
  • 3.2k
  • 11
  • 20

The best way to deal with compulsory and optional parameters is using the Builder pattern.The problem with overloaded functions is that they are confusing and user of the class or even you can make mistakes while calling this method.

Instead of that you can have builder that allows to construct Emails fluently.

class EmailSender{

private final String body;
private final String recipient;
private final List<String> to;
private final List<String> ccs;

public  EmailSender(EMailBuilder mailBuilder){
    this.body = mailBuilder.body;
    this.to = mailBuilder.to;
    this.recipient = mailBuilder.recipient;
    this.ccs = mailBuilder.ccs;

}
public void send(){
    //
}
public  static  class EMailBuilder{

    private final String body;
    private final String recipient;
    private final List<String> to;
    private final List<String> ccs;

    public  EMailBuilder(String body,String recipient,String to){
        this.recipient = recipient;
        this.body = body;
        this.to = new ArrayList<>();
        this.to.add(to);
        this.ccs = new ArrayList<>();

    }

    public EMailBuilder addTo(String to){
        this.to.add(to);
        return this;
    }
    public EMailBuilder addCc(String cc){
        this.ccs.add(cc);
        return this;
    }

    public EmailSender build(){
        EmailSender sender = new EmailSender(this);
        return sender;
    }

 }
}

Something like allows you build things easily, for example:

  EmailSender emailSender = new EmailSender.EMailBuilder("This is an email","me","you")
            .addTo("the Other Chap").addCc("the boss")
            .build();
  emailSender.send();