Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class StudentBuilder
- {
- private String _name;
- private int _age = 14; // this has a default
- private String _motto = ""; // most students don't have one
- public StudentBuilder() { }
- public Student buildStudent()
- {
- return new Student(_name, _age, _motto);
- }
- public StudentBuilder name(String _name)
- {
- this._name = _name;
- return this;
- }
- public StudentBuilder age(int _age)
- {
- this._age = age;
- return this;
- }
- public StudentBuilder motto(String _motto)
- {
- this._motto = _motto;
- return this;
- }
- }
- Student s1 = new StudentBuilder().name("Eli").buildStudent();
- Student s2 = new StudentBuilder()
- .name("Spicoli")
- .age(16)
- .motto("Aloha, Mr Hand")
- .buildStudent();
- MyClass(String house, String street, String town, String postcode, String country, int foo, double bar) {
- super(String house, String street, String town, String postcode, String country);
- this.foo = foo;
- this.bar = bar;
- MyClass(Address homeAddress, int foo, double bar) {
- super(homeAddress);
- this.foo = foo;
- this.bar = bar;
- }
- MyObject obj = new MyObjectBuilder().setXxx(myXxx)
- .setYyy(myYyy)
- .setZzz(myZzz)
- // ... etc.
- .build();
Add Comment
Please, Sign In to add comment