1 package com.test;
 2 
 3 /**
 4  * Created by xiaonanhai on 2015/5/30.
 5  */
 6 public class Builder {
 7 
 8     private String name;
 9     private int age;
10     private String gender;
11 
12     private Builder(BuilderFactoryImpl builderFactoryImpl){
13 
14         this.name = builderFactoryImpl.name;
15         this.age = builderFactoryImpl.age;
16         this.gender = builderFactoryImpl.gender;
17     }
18 
19     public String getName() {
20         return name;
21     }
22 
23     public int getAge() {
24         return age;
25     }
26 
27     public String getGender() {
28         return gender;
29     }
30 
31     public static class BuilderFactoryImpl implements BuilderFactory{
32 
33         private String name;
34         private int age;
35         private String gender;
36 
37         public BuilderFactoryImpl setName(String name) {
38             this.name = name;
39             return this;
40         }
41 
42         public BuilderFactoryImpl setAge(int age) {
43             this.age = age;
44             return this;
45         }
46 
47         public BuilderFactoryImpl setGender(String gender) {
48             this.gender = gender;
49             return this;
50         }
51 
52         @Override
53         public Builder build() {
54             return new Builder(this);
55         }
56     }
57 
58     public interface BuilderFactory{
59 
60         Builder build();
61     }
62 
63     @Override
64     public String toString() {
65         return "Builder{" +
66                 "name='" + name + '\'' +
67                 ", age=" + age +
68                 ", gender='" + gender + '\'' +
69                 '}';
70     }
71 
72     public static void main(String[] args) {
73 
74         Builder builder = new Builder.BuilderFactoryImpl()
75                 .setName("小男孩")
76                 .setGender("男")
77                 .setAge(29)
78                 .build();
79         System.out.println(builder.toString());
80     }
81 }

 

 posted on 2015-05-30 22:14  小网民  阅读(312)  评论(0编辑  收藏  举报