|
| 1 | +/** |
| 2 | + * MIT License |
| 3 | + * <p> |
| 4 | + * Copyright (c) 2017 James |
| 5 | + * <p> |
| 6 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | + * of this software and associated documentation files (the "Software"), to deal |
| 8 | + * in the Software without restriction, including without limitation the rights |
| 9 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | + * copies of the Software, and to permit persons to whom the Software is |
| 11 | + * furnished to do so, subject to the following conditions: |
| 12 | + * <p> |
| 13 | + * The above copyright notice and this permission notice shall be included in all |
| 14 | + * copies or substantial portions of the Software. |
| 15 | + * <p> |
| 16 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | + * SOFTWARE. |
| 23 | + */ |
| 24 | +package me.zbl.builder; |
| 25 | + |
| 26 | +/** |
| 27 | + * 人 |
| 28 | + */ |
| 29 | +public final class Person { |
| 30 | + |
| 31 | + private final String name; |
| 32 | + private final Integer age; |
| 33 | + private final Nationality nationality; |
| 34 | + |
| 35 | + public String getName() { |
| 36 | + return name; |
| 37 | + } |
| 38 | + |
| 39 | + public Integer getAge() { |
| 40 | + return age; |
| 41 | + } |
| 42 | + |
| 43 | + public Nationality getNationality() { |
| 44 | + return nationality; |
| 45 | + } |
| 46 | + |
| 47 | + public SkinColor getSkinColor() { |
| 48 | + return skinColor; |
| 49 | + } |
| 50 | + |
| 51 | + private final SkinColor skinColor; |
| 52 | + |
| 53 | + public Person(Builder builder) { |
| 54 | + this.name = builder.name; |
| 55 | + this.age = builder.age; |
| 56 | + this.skinColor = builder.skinColor; |
| 57 | + this.nationality = builder.nationality; |
| 58 | + } |
| 59 | + |
| 60 | + @Override |
| 61 | + public String toString() { |
| 62 | + StringBuilder sb = new StringBuilder(); |
| 63 | + sb.append(name); |
| 64 | + if (null != nationality) { |
| 65 | + sb.append(" 是来自 " + nationality.toString() + " 的"); |
| 66 | + } |
| 67 | + if (null != age) { |
| 68 | + sb.append(" " + age + "岁的"); |
| 69 | + } |
| 70 | + if (null != skinColor) { |
| 71 | + sb.append(" 有着" + skinColor + "皮肤的"); |
| 72 | + } |
| 73 | + sb.append("一个人"); |
| 74 | + return sb.toString(); |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * 创建者 |
| 79 | + */ |
| 80 | + public static class Builder { |
| 81 | + |
| 82 | + private String name; |
| 83 | + private Integer age; |
| 84 | + private Nationality nationality; |
| 85 | + private SkinColor skinColor; |
| 86 | + |
| 87 | + public Builder age(Integer age) { |
| 88 | + this.age = age; |
| 89 | + return this; |
| 90 | + } |
| 91 | + |
| 92 | + public Builder name(String name) { |
| 93 | + if (null == name) { |
| 94 | + throw new IllegalArgumentException("人必须有名字!"); |
| 95 | + } |
| 96 | + this.name = name; |
| 97 | + return this; |
| 98 | + } |
| 99 | + |
| 100 | + public Builder nationality(Nationality nationality) { |
| 101 | + this.nationality = nationality; |
| 102 | + return this; |
| 103 | + } |
| 104 | + |
| 105 | + public Builder skinColor(SkinColor skinColor) { |
| 106 | + this.skinColor = skinColor; |
| 107 | + return this; |
| 108 | + } |
| 109 | + |
| 110 | + public Person build() { |
| 111 | + return new Person(this); |
| 112 | + } |
| 113 | + } |
| 114 | +} |
0 commit comments