Last active
August 29, 2015 14:17
-
-
Save kayoubi/c02eacd9d29fad913e3a to your computer and use it in GitHub Desktop.
builder in java8
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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