jeudi 29 septembre 2016

Why to use Builder Pattern instead of using methods return self-instance of the class (factory method)

What is the difference to use this approach to standard way of creating Builder Pattern within the class:

public class MyClass {
private String name;
private String address;
private String email;

public MyClass(){}

public MyClass withName(String name) {
 this.name = name; 
 return this;
}
public MyClass withAddress(String address) {..}
public MyClass withEmail(String email) {..}

}

MyClass clazz = new MyClass().withAddress("").withName("").withEmail(""); Effect is the same. What I've missed?

Aucun commentaire:

Enregistrer un commentaire