Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do we need to use the builder design pattern when we can do the same thing with setters? [duplicate]

public class Employee {
    private String name;
    private String address;
    private int id;

    public Employee() {
        // TODO Auto-generated constructor stub
    }

    @Override
    public String toString() {
        return "Employee [name=" + name + ", address=" + address + ", id=" + id + "]";
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAddress() {
        return address;
    }

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

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

}

public class Main { 
    public static void main(String[] args) {
        Employee e = new Employee();
        e.setName("Priyanka");
        Employee e1 = new Employee();
        e1.setName("Rahul");
        e1.setAddress("Delhi");
        System.out.println("Value of e :"+ e);
        System.out.println("Value of e1:"+ e1);
    }
}
like image 648
Priyanka Taneja Avatar asked Apr 18 '19 15:04

Priyanka Taneja


People also ask

Why should we use builder pattern?

Builder pattern aims to “Separate the construction of a complex object from its representation so that the same construction process can create different representations.” It is used to construct a complex object step by step and the final step will return the object.

What are the consequences of applying the Builder design pattern?

Here are key consequences of the Builder pattern: It lets you vary a product's internal representation. The Builder object provides the director with an abstract interface for constructing the product. The interface lets the builder hide the representation and internal structure of the product.

When should I use builder pattern in Java?

The builder pattern, as the name implies, is an alternative way to construct complex objects. This pattern should be used when we want to build different immutable objects using the same object building process.

Why should I use Builder in Java?

The builder pattern simplifies the creation of objects. It also simplifies the code as your do not have to call a complex constructor or call several setter methods on the created object. The builder pattern can be used to create an immutable class.


1 Answers

The builder pattern can be useful to:

  • apply some check on the data used to initialize the object. For example if you need a double check between variables
  • create immutable objects. You can't change an object once initialized, so you can't use setters
  • add readability of code.
  • reduce the code used to initialize the object
  • have the instance in a valid state. Using setters the object instance can be in a not valid state before all the setters are called.

Note on using the builder to create immutable objects.

When you work in a multithread environment an immutable object can be shared between threads without explicit synchronization. Because the object can't change during the time is not possible to have a race condition accessing and modifying it by two threads at the same time.

like image 91
Davide Lorenzo MARINO Avatar answered Oct 05 '22 23:10

Davide Lorenzo MARINO