1.精简版
package generic;
import java.util.HashMap;
import java.util.Map;
public class TestGenericMap{
public Builder bd;
public Map map(){
return bd.map;
}
public static class Builder{
Map map;
public Builder(){
map = new HashMap();
}
public Builder put(K k, V v){
map.put(k, v);
return this;
}
}
public static void main(String[] args){
TestGenericMap.Builder tm = new TestGenericMap.Builder();
tm.put(1,"a").put(2,"b").put(3,"c");
System.out.println(tm.map);
}
}
2.相对精简版的增强版
package generic;
import java.util.HashMap;
import java.util.Map;
public class TestGenericMap{
public Builder bd;
public TestGenericMap(Builder bd){
this.bd = bd;
}
public V get(K k){
return bd.map.get(k);
}
public Map map(){
return bd.map;
}
public static class Builder{
Map map;
public Builder(){
map = new HashMap();
}
public Builder put(K k, V v){
map.put(k, v);
return this;
}
public TestGenericMap builder(){
return new TestGenericMap(this);
}
}
public static void main(String[] args){
TestGenericMap.Builder tm = new TestGenericMap.Builder();
tm.put(1,"a").put(2,"b").put(3,"c").builder().map();
System.out.println(tm.builder().get(2));
}
}
以上参考:开发测试21天