builder design pattern java

Solutions on MaxInterview for builder design pattern java by the best coders in the world

showing results for - "builder design pattern java"
Vanessa
05 Jan 2021
1public class BankAccount {
2    public static class Builder {
3        private long accountNumber; //This is important, so we'll pass it to the constructor.
4        private String owner;
5        private String branch;
6        private double balance;
7        private double interestRate;
8        public Builder(long accountNumber) {
9            this.accountNumber = accountNumber;
10        }
11        public Builder withOwner(String owner){
12            this.owner = owner;
13            return this;  //By returning the builder each time, we can create a fluent interface.
14        }
15        public Builder atBranch(String branch){
16            this.branch = branch;
17            return this;
18        }
19        public Builder openingBalance(double balance){
20            this.balance = balance;
21            return this;
22        }
23        public Builder atRate(double interestRate){
24            this.interestRate = interestRate;
25            return this;
26        }
27        public BankAccount build(){
28            //Here we create the actual bank account object, which is always in a fully initialised state when it's returned.
29            BankAccount account = new BankAccount();  //Since the builder is in the BankAccount class, we can invoke its private constructor.
30            account.accountNumber = this.accountNumber;
31            account.owner = this.owner;
32            account.branch = this.branch;
33            account.balance = this.balance;
34            account.interestRate = this.interestRate;
35            return account;
36        }
37    }
38    //Fields omitted for brevity.
39    private BankAccount() {
40        //Constructor is now private.
41    }
42    //Getters and setters omitted for brevity.
43}
44
45BankAccount account = new BankAccount.Builder(1234L)
46            .withOwner("Marge")
47            .atBranch("Springfield")
48            .openingBalance(100)
49            .atRate(2.5)
50            .build();
51BankAccount anotherAccount = new BankAccount.Builder(4567L)
52            .withOwner("Homer")
53            .atBranch("Springfield")
54            .openingBalance(100)
55            .atRate(2.5)
56            .build();
Daniela
04 Oct 2016
1public class User 
2{
3    //All final attributes
4    private final String firstName; // required
5    private final String lastName; // required
6    private final int age; // optional
7    private final String phone; // optional
8    private final String address; // optional
9 
10    private User(UserBuilder builder) {
11        this.firstName = builder.firstName;
12        this.lastName = builder.lastName;
13        this.age = builder.age;
14        this.phone = builder.phone;
15        this.address = builder.address;
16    }
17 
18    //All getter, and NO setter to provde immutability
19    public String getFirstName() {
20        return firstName;
21    }
22    public String getLastName() {
23        return lastName;
24    }
25    public int getAge() {
26        return age;
27    }
28    public String getPhone() {
29        return phone;
30    }
31    public String getAddress() {
32        return address;
33    }
34 
35    @Override
36    public String toString() {
37        return "User: "+this.firstName+", "+this.lastName+", "+this.age+", "+this.phone+", "+this.address;
38    }
39 
40    public static class UserBuilder 
41    {
42        private final String firstName;
43        private final String lastName;
44        private int age;
45        private String phone;
46        private String address;
47 
48        public UserBuilder(String firstName, String lastName) {
49            this.firstName = firstName;
50            this.lastName = lastName;
51        }
52        public UserBuilder age(int age) {
53            this.age = age;
54            return this;
55        }
56        public UserBuilder phone(String phone) {
57            this.phone = phone;
58            return this;
59        }
60        public UserBuilder address(String address) {
61            this.address = address;
62            return this;
63        }
64        //Return the finally consrcuted User object
65        public User build() {
66            User user =  new User(this);
67            validateUserObject(user);
68            return user;
69        }
70        private void validateUserObject(User user) {
71            //Do some basic validations to check 
72            //if user object does not break any assumption of system
73        }
74    }
75}
76
queries leading to this page
design pattern in javabuilder design pattern java exampleeasiest builder pattern javacreate a builder javabuilder pattern in javavjava method builder patternjava constructor pattern find programjava class builder pattern examplehow to implement builder pattern in javabuilder pattern example in javaexample of builder pattern in javawhy we need builder pattern in javajava builder methodwhen we use the builder design pattern in javadesign pattern builder javahow builder pattern works in javabuilder design pattern in java examplejava builder how to make ofbuilder desing pattern in javabuilder java patternjava builder pattern do workbuilder pattern in java examplebuilder class java examplejava builder classjava builder patternbuilder design patterns in javajava builder design pattern tutorialbuilder design pattern in javabuilder pattern example javatest builder pattern javawhat is builder design pattern in javabuilder class javabuilder patter javabuilder java examplejava pattern builderjava builder design pattern examplewhat is builder patterns in javabuilder pattern java exampleinbuilt class in java builder 28 29 buildjava 8 builder method patternthe builder pattern javabuilder pattern java 5 examplejava builder pattern with genericsfactory design pattern javahow to do builder pattern programs in javabuilder design pattern in java simple examplebuilder java design patternbuilder meaning in javabuilder design pattern java example mkyongjava builder pattern with decoratorjava builder design patternbuilder pattern in java builder 28 29 javajava builder pattern examplehow to create builder pattern in javabuilder design pattern implementation in javabuilder pattern javabuilder pattern jabvajava custom builderjava string builder patternpattern builder javadesign pattern javadesign pattern builder java examplebulder javabuilder design patterns javahow to use builder javabuilder design pattern example in javawhat is builder pattern in javaclass builder javawhy i should use builder pattern javathe builder design pattern in java gurubuilder design pattern javawhat is a builder pattern in javadesign patterns java builderwhat design pattern is stringbuilder in javaimplement builder pattern javabuilder patten javabuilder pattern java abstract classjava singleton design patternbuilder design pattern used in javabuilder pattern example java 8builder patternbuilder pattern for javabuilder pattern java implementationbuilder build javajava pattern 40java builderbuilder design pattern java