Skip to content

Instantly share code, notes, and snippets.

@mcalavera81
Last active October 29, 2015 14:29
Show Gist options
  • Save mcalavera81/9588ad697570fdc76cc1 to your computer and use it in GitHub Desktop.
Save mcalavera81/9588ad697570fdc76cc1 to your computer and use it in GitHub Desktop.
Fluent API and Loan Pattern/Execute around method Pattern
package com.example;
import java.io.IOException;
import java.util.function.Function;
public class MessingAround {
public static void main(String[] args) throws IOException, InterruptedException {
FluentMailer.send(mailer->mailer.to("[email protected]").from("[email protected]")
.subject("Hello").body("Body!"));
}
public static class FluentMailer {
private String addressTo,addressFrom,subject,message ;
private FluentMailer() {
}
public static void send(final Function<FluentMailer,FluentMailer> block) {
final FluentMailer mailer = new FluentMailer();
FluentMailer result = block.apply(mailer);
System.out.println(String.format("Sending email with: %s", result));
}
public FluentMailer from(final String address) {
this.addressFrom = address;
return this;
}
public FluentMailer to(final String address) {
this.addressTo = address;
return this;
}
public FluentMailer subject(final String subject) {
this.subject = subject;
return this;
}
public FluentMailer body(final String message) {
this.message = message;
return this;
}
@Override
public String toString() {
return "FluentMailer{" +
"addressTo='" + addressTo + '\'' +
", addressFrom='" + addressFrom + '\'' +
", subject='" + subject + '\'' +
", message='" + message + '\'' +
'}';
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment