java builder pattern example
Java
public class BankAccount {
public static class Builder {
private long accountNumber; //This is important, so we'll pass it to the constructor.
private String owner;
private String branch;
private double balance;
private double interestRate;
public Builder(long accountNumber) {
this.accountNumber = accountNumber;
}
public Builder withOwner(String owner){
this.owner = owner;
return this; //By returning the builder each time, we can create a fluent interface.
}
public Builder atBranch(String branch){
this.branch = branch;
return this;
}
public Builder openingBalance(double balance){
this.balance = balance;
return this;
}
public Builder atRate(double interestRate){
this.interestRate = interestRate;
return this;
}
public BankAccount build(){
//Here we create the actual bank account object, which is always in a fully initialised state when it's returned.
BankAccount account = new BankAccount(); //Since the builder is in the BankAccount class, we can invoke its private constructor.
account.accountNumber = this.accountNumber;
account.owner = this.owner;
account.branch = this.branch;
account.balance = this.balance;
account.interestRate = this.interestRate;
return account;
}
}
//Fields omitted for brevity.
private BankAccount() {
//Constructor is now private.
}
//Getters and setters omitted for brevity.
}
BankAccount account = new BankAccount.Builder(1234L)
.withOwner("Marge")
.atBranch("Springfield")
.openingBalance(100)
.atRate(2.5)
.build();
BankAccount anotherAccount = new BankAccount.Builder(4567L)
.withOwner("Homer")
.atBranch("Springfield")
.openingBalance(100)
.atRate(2.5)
.build();
Also in Java:
- Title
- java create jframe
- Category
- Java
- Title
- json request body not getting parsed by spring boot controller
- Category
- Java
- Title
- leap year checker java
- Category
- Java
- Title
- reading string after double in java
- Category
- Java
- Title
- java lambda list of objects cast
- Category
- Java
- Title
- Write a JAVA method that expands a given binomial (ax + by)n, where integers a, b, n are user inputs. For example, if a = 2, b = -12, n = 4 are entered the method should print or return
- Category
- Java
- Title
- parseints(str) java
- Category
- Java
- Title
- java last element in array
- Category
- Java
- Title
- java how to make a gui
- Category
- Java
- Title
- isnumber java
- Category
- Java
- Title
- java 8 list stream delete by name
- Category
- Java
- Title
- how to fill a 2d array in java
- Category
- Java
- Title
- java print type of object
- Category
- Java
- Title
- applicationcontext.xml
- Category
- Java
- Title
- simple javascript to detect browser using java utils
- Category
- Java
- Title
- arraylist to int array java
- Category
- Java
- Title
- double round java integer
- Category
- Java
- Title
- fibonacci sequence in java recursion
- Category
- Java
- Title
- init cap java
- Category
- Java
- Title
- simple calculator program in java
- Category
- Java
- Title
- java cannot find file path
- Category
- Java
- Title
- java list contains object with property
- Category
- Java
- Title
- java 8 loop in map
- Category
- Java
- Title
- how to substring in java
- Category
- Java
- Title
- string to arraylist convert java
- Category
- Java
- Title
- trim() and split() in java
- Category
- Java
- Title
- how to separate no and text in java
- Category
- Java
- Title
- déclarer un tableau en java
- Category
- Java
- Title
- processing pi
- Category
- Java
- Title
- java call another constructor
- Category
- Java
- Title
- how to check if rs next is null
- Category
- Java
- Title
- java array declaration
- Category
- Java
- Title
- java how to convert string to int
- Category
- Java
- Title
- how to make a fixed size array in java
- Category
- Java
- Title
- read csv java
- Category
- Java
- Title
- java hello world
- Category
- Java
- Title
- programa que convierete un archi de c a java
- Category
- Java
- Title
- java generate list of random element
- Category
- Java
- Title
- how do you concatenate an int with a string in java
- Category
- Java
- Title
- java sql question mark
- Category
- Java
- Title
- nth prime number java
- Category
- Java
- Title
- convert char to string java
- Category
- Java
- Title
- catch array out of bounds exception java
- Category
- Java
- Title
- how to get all the names of the files in a folder in java?
- Category
- Java
- Title
- how to create a button in java
- Category
- Java
- Title
- print a string java
- Category
- Java
- Title
- how to count an replace substring string in java
- Category
- Java
- Title
- read integer input java
- Category
- Java
- Title
- java get last char of string
- Category
- Java
- Title
- how to stop a void java
- Category
- Java
- Title
- how to make a copy of an array java
- Category
- Java
- Title
- android how to know when snackbar is done
- Category
- Java
- Title
- inheritance in java
- Category
- Java
- Title
- import java.util.hashset
- Category
- Java
- Title
- java get environment variables
- Category
- Java
- Title
- binary to int java
- Category
- Java
- Title
- jsonobject to java object
- Category
- Java
- Title
- convert int array to integer list java
- Category
- Java
- Title
- butterfly pattern program in java
- Category
- Java
- Title
- how to import another class in java
- Category
- Java
- Title
- loop array using stream java
- Category
- Java
- Title
- coding with WDSL spring
- Category
- Java
- Title
- java console write
- Category
- Java
- Title
- Java how to copy file
- Category
- Java
- Title
- spring data jpa inheritance repository
- Category
- Java
- Title
- string startswith java
- Category
- Java
- Title
- how to do 4th root java
- Category
- Java
- Title
- java array to arraylist
- Category
- Java
- Title
- Gson write json to file
- Category
- Java
- Title
- transition java fx
- Category
- Java
- Title
- java random between two strings
- Category
- Java
- Title
- final variables in java
- Category
- Java
- Title
- java builder pattern example
- Category
- Java
- Title
- adb: command not found
- Category
- Java
- Title
- how to make a searchable list in java
- Category
- Java
- Title
- java loop object
- Category
- Java
- Title
- change button color java swing
- Category
- Java
- Title
- java int passed by reference
- Category
- Java
- Title
- generate all prime number less than n java
- Category
- Java
- Title
- thread sleep java
- Category
- Java
- Title
- login and logout react native and firebase
- Category
- Java
- Title
- android notification addaction example
- Category
- Java
- Title
- compare string integer java
- Category
- Java
- Title
- java 8 seconds to days
- Category
- Java
- Title
- foreach not applicable to type - binary tree sort
- Category
- Java
- Title
- do while loop java
- Category
- Java
- Title
- dataframe to dict without index
- Category
- Java
- Title
- java check if string is number
- Category
- Java
- Title
- java pass arraylist by value
- Category
- Java
- Title
- java nextpermutation
- Category
- Java
- Title
- creating thread in java example
- Category
- Java
- Title
- java bigdecimal third root
- Category
- Java
- Title
- java stack
- Category
- Java
- Title
- java console text color
- Category
- Java
- Title
- how to make a char uppercase in java
- Category
- Java
- Title
- how to do substring java
- Category
- Java
- Title
- calculating the percentile in java
- Category
- Java
- Title
- java how to initialize an array
- Category
- Java
- Title
- java go troght loop object
- Category
- Java
- Title
- spring-boot actuator not working
- Category
- Java