Skip to content

Instantly share code, notes, and snippets.

@kayoubi
Last active August 29, 2015 14:17
Show Gist options
  • Save kayoubi/c02eacd9d29fad913e3a to your computer and use it in GitHub Desktop.
Save kayoubi/c02eacd9d29fad913e3a to your computer and use it in GitHub Desktop.
builder in java8
import java.util.function.Consumer;
/**
* @author Khaled Ayoubi
*/
public class BookBuilder {
private Book book;
private BookBuilder() {
this.book = new Book();
}
public BookBuilder name(String name) {
book.setName(name);
return this;
}
public BookBuilder author(String author) {
book.setAuthor(author);
return this;
}
public BookBuilder year(int year) {
book.setYear(year);
return this;
}
public static Book build(Consumer<BookBuilder> consumer) {
BookBuilder builder = new BookBuilder();
consumer.accept(builder);
return builder.book;
}
public static void main(String[] args) {
BookBuilder.build(b -> b.name("nice book").author("khaled").year(2222));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment