Flask接口签名sign原理与实例代码浅析
423
2023-03-28
Java Builder模式构建MAP/LIST的实例讲解
我们在构建一个MAP时,要不http://停的调用put,有时候看着觉得很麻烦,刚好,看了下builder模式,觉得这思路不错,于是乎,照着用builder模式写了一个构建MAP的示例,
代码如下:
import java.util.HashMap;
import java.util.Map;
public class MapBuilder
public Builder
public MapBuilder(Builder
this.b = b;
}
public Map
return b.map;
}
public T get(String key){
return b.map.get(key);
}
public static class Builder
public Map
public Builder(){
map = new HashMap
}
public Builder
map.put(key, value);
return this;
}
public MapBuilder
return new MapBuilder
}
}
public static void main(String[] args) {
MapBuilder
System.out.println(build.get("a"));
}
}
public class ListBuilder
public Builder
public ListBuilder(Builder
this.b = b;
}
public List
return b.list;
}
public T get(int index){
return b.list.get(index);
}
public static class Builder
public List
public Builder(){
list = new ArrayList
}
public Builder
list.add(value);
return this;
}
public ListBuilder
return new ListBuilder
}
}
public static void main(String[] args) {
ListBuilder
System.out.println(build.get(0));
}
}
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~