Skip to content

Instantly share code, notes, and snippets.

@tranngoclam
Created September 23, 2016 03:38
Show Gist options
  • Save tranngoclam/6b790948df06f730759d239e7d4ba6f3 to your computer and use it in GitHub Desktop.
Save tranngoclam/6b790948df06f730759d239e7d4ba6f3 to your computer and use it in GitHub Desktop.
Example for Builder Pattern
package net.lamtran.rxjava2;
import org.junit.Test;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.IsEqual.equalTo;
import static org.junit.Assert.assertNotNull;
/**
* Created by lam on 9/23/16.
*/
public class BuilderTest {
@Test
public void testRequestPaymentBuilder() throws Exception {
RequestPayment request = new RequestPayment.Builder()
.setBillId("bill_id")
.setServiceId("service_id")
.setProviderId("provider_id")
.setCustomerCode("customer_code")
.build();
assertNotNull(request);
assertThat(request.getBillId(), equalTo("bill_id"));
assertThat(request.getServiceId(), equalTo("service_id"));
assertThat(request.getProviderId(), equalTo("provider_id"));
assertThat(request.getCustomerCode(), equalTo("customer_code"));
}
}
package net.lamtran.rxjava2;
/**
* Created by lam on 9/23/16.
*/
public class RequestPayment {
private final String mBillId;
private final String mCustomerCode;
private final String mProviderId;
private final String mServiceId;
RequestPayment(Builder builder) {
mBillId = builder.billId;
mCustomerCode = builder.customerCode;
mProviderId = builder.providerId;
mServiceId = builder.serviceId;
}
public String getBillId() {
return mBillId;
}
public String getCustomerCode() {
return mCustomerCode;
}
public String getProviderId() {
return mProviderId;
}
public String getServiceId() {
return mServiceId;
}
public static final class Builder {
private String billId;
private String customerCode;
private String providerId;
private String serviceId;
public RequestPayment build() {
return new RequestPayment(this);
}
public Builder setBillId(String billId) {
this.billId = billId;
return this;
}
public Builder setCustomerCode(String customerCode) {
this.customerCode = customerCode;
return this;
}
public Builder setProviderId(String providerId) {
this.providerId = providerId;
return this;
}
public Builder setServiceId(String serviceId) {
this.serviceId = serviceId;
return this;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment