jeudi 18 avril 2019

Why we need to use builder design pattern when same thing we can do with setters also?

Why we need to use builder design pattern when same thing we can do with setters also? What exactly is the use of builder design pattern if we can achieve the same thing using setters as well.

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);

}

}

Aucun commentaire:

Enregistrer un commentaire