Searching...
Thursday, 18 December 2014

Java Builder Pattern Simple example

09:19

Java Builder pattern comes under creational pattern and it provides simple way of creating complex objects.

Here is one scenario, i have a Customer class and while creating object need to pass multiple parameters. Some of these parameters are mandatory and some of them are optional. The optional parameters and i do not want to pass null values all the times and it should take the default null values.

See how builder pattern work for this scenario.

1. Create a Customer Class

Customer class will have one static nested class , which will help us to set the mandatory and optional values.

public class Customer {
    // Mandatory fields
    private String firstName;
    private String lastName;

    // Optional fields
    private String address;
    private String city;
    private String zipCode;

    public String getFirstName() {
        return firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public String getAddress() {
        return address;
       
    }

    public String getCity() {
        return city;
    }

    public String getZipCode() {
        return zipCode;
    }

    private Customer(CustomerBuilder builder) {
        this.firstName = builder.firstName;
        this.lastName = builder.lastName;
        this.address = builder.address;
        this.city = builder.city;
        this.zipCode = builder.zipCode;

    }

    @Override
    public String toString() {
        return "Person [firstName=" + firstName + ", lastName=" + lastName
                + ", address=" + address + ", city=" + city + ", zipCode="
                + zipCode + "]";
    }

    public static class CustomerBuilder {
        private String firstName;
        private String lastName;

        // Optional fields
        private String address;
        private String city;
        private String zipCode;

        public CustomerBuilder(String firstName, String lastName) {
            this.firstName = firstName;
            this.lastName = lastName;

        }

        public CustomerBuilder setAddress(String address) {
            this.address = address;
            return this;
        }

        public CustomerBuilder setCity(String city) {
            this.city = city;
            return this;

        }

        public CustomerBuilder setZipCode(String zipCode) {
            this.zipCode = zipCode;
            return this;

        }

        public Customer build() {
            return new Customer(this);

        }
    }

}

2. Test class to create Customer Object

public class TestBuilderPattern {

    public static void main(String[] args) {
        //Creating customer object with only mandatory fields
        Customer customer = new Customer.CustomerBuilder("Vinod", "Kumaran")
                .build();
        System.out.println(customer);

        //Creating customer object with mandatory and some optional fields
        Customer customer1 = new Customer.CustomerBuilder("Vinod", "Kumaran")
                .setAddress("EC").setCity("Bangalore").build();
        System.out.println(customer1);
    }

}

3. Output

Person [firstName=Vinod, lastName=Kumaran, address=null, city=null, zipCode=null]
Person [firstName=Vinod, lastName=Kumaran, address=EC, city=Bangalore, zipCode=null]

0 comments: