public class ReallyHeavyObject {
// Just to illustrate...
private int[] tonsOfValues;
private Resource[] tonsOfResources;
// This method almost does nothing
public Map quickHarmlessMethod() {
Map source = new HashMap()\{\{
put("firstName", "John");
put("lastName", "Smith");
put("organizations", new HashMap()\{\{
put("0", new HashMap()\{\{
put("id", "1234");
}});
put("abc", new HashMap()\{\{
put("id", "5678");
}});
}});
}};
return source;
}
}
public class MyClass {
public static class Builder {
public int first = -1 ;
public double second = Double.NaN;
public String third = null ;
public MyClass create() {
return new MyClass(first, second, third);
}
}
protected final int first ;
protected final double second;
protected final String third ;
protected MyClass(
int first ,
double second,
String third
) {
this.first = first ;
this.second= second;
this.third = third ;
}
public int first () { return first ; }
public double second() { return second; }
public String third () { return third ; }
}
用法:
MyClass my = new MyClass.Builder()\{\{ first = 1; third = "3"; }}.create();