Skip to content

Instantly share code, notes, and snippets.

@fakeyanss
Created September 13, 2018 06:56
Show Gist options
  • Save fakeyanss/682e902214d4a7e0cfff3375ca1fbe03 to your computer and use it in GitHub Desktop.
Save fakeyanss/682e902214d4a7e0cfff3375ca1fbe03 to your computer and use it in GitHub Desktop.
/**
* Fluent API + Singleton
*
* Created by yanss on 2018/9/13.
*/
public class Fluent {
private String name;
private int age;
public String getName() {
return name;
}
public Fluent setName(String name) {
this.name = name;
return this;
}
public int getAge() {
return age;
}
public Fluent setAge(int age) {
this.age = age;
return this;
}
public static Fluent build() {
if (instance == null) {
synchronized (Fluent.class) {
if (instance == null) {
instance = new Fluent();
}
}
}
return instance;
}
@Override
public String toString() {
return "Fluent{" + "name='" + name + '\'' + ", age=" + age + '}';
}
private Fluent() {}
private static volatile Fluent instance = null;
public static void main(String[] args) {
Fluent.build().setName("erica").setAge(20);
Fluent.build().setAge(21);
System.out.println(Fluent.build());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment