首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

《有点模式》- Builder

2012-09-20 
《有些模式》- Builder?或许你已经看惯了javabean和层叠构造.??/** * Builder模式 * @author Macrotea * */p

《有些模式》- Builder

?或许你已经看惯了javabean和层叠构造.

?

?

/** * Builder模式 * @author Macrotea * */public class ManFacts {private String name;private int age;private double height;private double weight;public static class Builder {// 必须private String name;private int age;// 可选private double height;private double weight;public Builder(String name, int age) {super();this.name = name;this.age = age;}public Builder height(double height) {this.height = height;return this;}public Builder weight(double weight) {this.weight = weight;return this;}public ManFacts build() {return new ManFacts(this);}}private ManFacts(Builder builder) {this.age = builder.age;this.name = builder.name;this.height = builder.height;this.weight = builder.weight;}}public static void main(String[] args) {ManFacts manFacts=new ManFacts.Builder("macrotea", 22).height(180).weight(140).build();}

热点排行