Last active
July 17, 2018 10:57
-
-
Save mahbodkh/acbd4ef6c49cb6be8a36bb3111dbcad3 to your computer and use it in GitHub Desktop.
Builder Design Pattern
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
| public class Animal { | |
| private int code; | |
| private String name; | |
| private int age; | |
| private Animal(AnimalBuilder builder) { | |
| this.code = builder.code; | |
| this.name = builder.name; | |
| this.age = builder.age; | |
| } | |
| public int getCode() { | |
| return code; | |
| } | |
| public void setCode(int code) { | |
| this.code = code; | |
| } | |
| public String getName() { | |
| return name; | |
| } | |
| public void setName(String name) { | |
| this.name = name; | |
| } | |
| public int getAge() { | |
| return age; | |
| } | |
| public void setAge(int age) { | |
| this.age = age; | |
| } | |
| public static class AnimalBuilder { | |
| private int code; | |
| private String name; | |
| private int age; | |
| public AnimalBuilder(int code, String name) { | |
| this.code = code; | |
| this.name = name; | |
| } | |
| public AnimalBuilder setAge(int age) { | |
| this.age = age; | |
| return this; | |
| } | |
| public Animal build() { | |
| return new Animal(this); | |
| } | |
| } | |
| public static void main(String[] args) { | |
| Animal animal = new Animal.AnimalBuilder(7, "lion").setAge(12).build(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment