Article directory
It is often seen that some codes are initialized with consecutive. set() methods, which are chained method calls.
Traditional way
In general, when initializing an object, we can call the. set() method to assign a value to a property, for example:
public class FixedDepositDetails { private long id; private float depositAmount; private int tenure; private String email; public FixedDepositDetails() { } public FixedDepositDetails(long id, float depositAmount, int tenure, String email) { this.id = id; this.depositAmount = depositAmount; this.tenure = tenure; this.email = email; } public long getId() { return id; } public void setId(long id) { this.id = id; } public float getDepositAmount() { return depositAmount; } public void setDepositAmount(float depositAmount) { this.depositAmount = depositAmount; } public int getTenure() { return tenure; } public void setTenure(int tenure) { this.tenure = tenure; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } @Override public String toString() { return "FixedDepositDetails{" + "id=" + id + ", depositAmount=" + depositAmount + ", tenure=" + tenure + ", email='" + email + '\'' + '}'; } }
The way of use is also traditional, for example:
import org.junit.Test; public class FixedDepositDetailsTest { @Test public void testObjectCreate() { FixedDepositDetails fixedDepositDetails = new FixedDepositDetails(); fixedDepositDetails.setId(1L); fixedDepositDetails.setDepositAmount(1000); fixedDepositDetails.setEmail("someone@somedomain.com"); fixedDepositDetails.setTenure(100); System.out.println(fixedDepositDetails); } }
Nothing special. This is a familiar pattern.
Chain method
Now, let's look at the chain call. Its code is as follows:
import org.junit.Test; public class FixedDepositDetailsTest { @Test public void testObjectCreate() { FixedDepositDetails fixedDepositDetails = new FixedDepositDetails(); fixedDepositDetails.setId(1L) .setDepositAmount(1000) .setEmail("someone@somedomain.com") .setTenure(100); System.out.println(fixedDepositDetails); } }
Is it very different from the way you created the object before.
To implement chained method calls, it is very simple, that is, each set method can return to itself, and it is also easy to implement, for example:
import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class FixedDepositDetails { private static Logger logger = LoggerFactory.getLogger(FixedDepositDetails.class); private long id; private float depositAmount; private int tenure; private String email; public FixedDepositDetails() { logger.info("Created instance"); } public long getId() { return id; } public FixedDepositDetails setId(long id) { this.id = id; return this; } public float getDepositAmount() { return depositAmount; } public FixedDepositDetails setDepositAmount(float depositAmount) { this.depositAmount = depositAmount; return this; } public int getTenure() { return tenure; } public FixedDepositDetails setTenure(int tenure) { this.tenure = tenure; return this; } public String getEmail() { return email; } public FixedDepositDetails setEmail(String email) { this.email = email; return this; } public String toString() { return "id :" + id + ", deposit amount : " + depositAmount + ", tenure : " + tenure + ", email : " + email; } }
Automatic generation of idea
You can use the idea environment to automatically generate the set method that returns this. Select set template: to generate the desired set method for Builder, as shown in the following figure:
Of course, either way, it's OK.