2024-03-28
465
15 MIN READ
Computer类:
public class Computer {
private final String material;
private final String cpu;
private final String ram;
private final String monitor;
private final double price;
private final String brand;
private final String mouse;
private final String keyboard;
private final Set<String> software;
public static class Builder{
// Required parameter
private final String material;
// Optional parameters
private String cpu;
private String ram;
private String monitor;
private double price;
private String brand;
private String mouse;
private String keyboard;
private final Set<String> software = new HashSet<>();
public Builder(String material) {
this.material = material;
}
public Builder cpu(String cpuIn) {
cpu = cpuIn;
return this;
}
public Builder ram(String ramIn) {
ram = ramIn;
return this;
}
public Builder monitor(String monitorIn) {
monitor = monitorIn;
return this;
}
public Builder price(double priceIn) {
price = priceIn;
return this;
}
public Builder brand(String brandIn) {
brand = brandIn;
return this;
}
public Builder mouse(String mouseIn) {
mouse = mouseIn;
return this;
}
public Builder keyboard(String keyboardIn) {
keyboard = keyboardIn;
return this;
}
public Builder software(String softwareIn) {
software.add(softwareIn);
return this;
}
public Computer build() {
return new Computer(this);
}
}
private Computer(Builder builder) {
material = builder.material;
cpu = builder.cpu;
ram = builder.ram;
monitor = builder.monitor;
price = builder.price;
brand = builder.brand;
mouse = builder.mouse;
keyboard = builder.keyboard;
software = builder.software;
}
@Override
public String toString() {
return "Computer{" +
"made with: '" + material + '\'' +
", cpu='" + cpu + '\'' +
", ram='" + ram + '\'' +
", monitor='" + monitor + '\'' +
", price=" + price +
", brand='" + brand + '\'' +
", mouse='" + mouse + '\'' +
", keyboard='" + keyboard + '\'' +
", software=" + software.toString() +
'}';
}
}
Unit Test:
@Test
public void test_builderPattern() {
Computer computer = new Computer.Builder("silicon").cpu("CORE i7").ram("16GB").brand("HP").keyboard("Keyboard").monitor("Lenovo").mouse("Logic").price(9999.99)
.software("WPS").software("MS Office").software("IDEA").software("SQL Server").build();
System.out.println(computer.toString());
}
结果:
Computer{made with: 'silicon', cpu='CORE i7', ram='16GB', monitor='Lenovo', price=9999.99, brand='HP', mouse='Logic', keyboard='Keyboard', software=[IDEA, WPS, SQL Server, MS Office]}
Family 抽象类:
public abstract class Family {
private final String house;
private final String me;
public enum FamilyMember {MOTHER, FATHER, GRANDPA, GRANDMA, SISTER, BROTHER};
private final Set<FamilyMember> familyMembers;
public abstract static class Builder<T extends Builder<T>> {
private final String house;
private final String me;
private final EnumSet<FamilyMember> familyMembers = EnumSet.noneOf(FamilyMember.class);
public Builder(String name){
house = name + "'s Family";
me = name;
}
public T addFamilyMember(FamilyMember familyMember) {
familyMembers.add(Objects.requireNonNull(familyMember));
return self();
}
abstract Family build();
protected abstract T self();
}
public Family(Builder<?> builder) {
house = builder.house;
me = builder.me;
familyMembers = builder.familyMembers.clone();
}
public Set<FamilyMember> getFamilyMembers() {
return familyMembers;
}
public String getHouse() {
return house;
}
public String getMe() {
return me;
}
public String introduceInfo() {
return "My name is " + me + ". This is " + house + ".";
}
@Override
public String toString() {
return "Family{" +
"house='" + house + '\'' +
", me='" + me + '\'' +
", familyMembers=" + familyMembers.toString() +
'}';
}
}
实体类CoCoFamily:
public class CoCoFamily extends Family{
private final String cocoName;
private final String cocoGender;
private CoCoFamily(Builder builder) {
super(builder);
cocoGender = "male";
cocoName = "CoCo";
}
public static class Builder extends Family.Builder<Builder> {
public Builder(String name) {
super(name);
}
@Override
public CoCoFamily build() {
return new CoCoFamily(this);
}
@Override
protected Builder self() {
return this;
}
}
@Override
public String toString() {
return super.introduceInfo() +
" CoCoFamily{" +
"cocoName='" + cocoName + '\'' +
", cocoGender='" + cocoGender + '\'' +
", cocoFamilyMembers='" + super.getFamilyMembers().toString() + '\'' +
'}';
}
}
Unit Test:
@Test
public void test_hierarchyBuilderPatter() {
CoCoFamily cocoFamily = new CoCoFamily.Builder("CoCo").addFamilyMember(Family.FamilyMember.MOTHER).addFamilyMember(Family.FamilyMember.FATHER).build();
System.out.println(cocoFamily.toString());
}
结果:
My name is CoCo. This is CoCo's Family. CoCoFamily{cocoName='CoCo', cocoGender='male', cocoFamilyMembers='[MOTHER, FATHER]'}
private final Set<String> software = new HashSet<>();