java builder pattern example

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

showing results for - "java builder pattern example"
Gael
20 Nov 2016
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();
queries leading to this page
design pattern javabuilder pattern java examplebuilder design pattern in java examplejava class builder pattern examplebuilder java patternhow to do builder pattern programs in javabuilder design pattern java example mkyongjava builderjava pattern builderbuilder build javabuilder design patterns javaclass builder javadesign patterns java builderbuilder pattern javabuilder design pattern in java simple examplebuilder design pattern used in javawhat is builder patterns in javabuilder pattern for javabuilder pattern example java 8builder pattern java abstract classbuilder java design patternbuilder class javawhy i should use builder pattern javainbuilt class in java builder 28 29 buildjava constructor pattern find programbuilder design pattern java examplebuilder pattern example javajava builder methodbuilder pattern in javawhen we use the builder design pattern in javaimplement builder pattern javawhat design pattern is stringbuilder in javabuilder design pattern implementation in javajava builder pattern with genericspattern builder javajava builder pattern with decoratorbuilder design pattern example in javabuilder pattern in java exampledesign pattern in javacreate a builder javajava builder classbuilder pattern jabvawhat is builder pattern in javabulder javajava pattern 40easiest builder pattern javajava string builder patternwhat is builder design pattern in javadesign pattern builder java exampleexample of builder pattern in javajava builder design patternbuilder design pattern in javawhy we need builder pattern in javahow to use builder java builder 28 29 javajava singleton design patternbuilder pattern java implementationbuilder class java examplejava builder design pattern examplebuilder patternhow builder pattern works in javabuilder java examplejava builder design pattern tutorialjava method builder patternhow to create builder pattern in javabuilder pattern in javavtest builder pattern javawhat is a builder pattern in javajava builder how to make ofbuilder design patterns in javabuilder meaning in javajava 8 builder method patternbuilder pattern example in javajava builder patternbuilder design pattern javabuilder pattern java 5 examplejava custom builderjava builder pattern do workhow to implement builder pattern in javafactory design pattern javajava builder pattern examplejava builder pattern example