Builder设计模式
GOF Builder 设计模式
Car.java
public class Car { private String engine; private String transmission; private String chassis; public String getEngine() { return engine; } public void setEngine(String engine) { this.engine = engine; } public String getTransmission() { return transmission; } public void setTransmission(String transmission) { this.transmission = transmission; } public String getChassis() { return chassis; } public void setChassis(String chassis) { this.chassis = chassis; } @Override public String toString() { return "Car{" + "engine='" + engine + '\'' + ", transmission='" + transmission + '\'' + ", chassis='" + chassis + '\'' + '}'; } }
Builder.java
public abstract class Builder { public abstract void builderEngine(String engine); public abstract void builderTransmission(String transmission); public abstract void builderChassis(String chassis); public abstract Car build(); }
CarBuilder.java
public class CarBuilder extends Builder { private Car car = new Car(); @Override public void builderEngine(String engine) { car.setEngine(engine); } @Override public void builderTransmission(String transmission) { car.setTransmission(transmission); } @Override public void builderChassis(String chassis) { car.setChassis(chassis); } @Override public Car build() { return car; } }
Director.java
public class Director { private Builder builder = null; public Director(Builder builder) { this.builder = builder; } public Car createCar(String engine, String transmission, String chassis) { builder.builderEngine(engine); builder.builderTransmission(transmission); builder.builderChassis(chassis); return builder.build(); } public static void main(String[] args) { Builder builder = new CarBuilder(); Car car = new Director(builder).createCar("engine", "transmission", "chassis"); System.out.println(car); } }
Builder设计模式变种--Java多参数问题
JavaBean多参数创建Bean时,有三种方式,创建多个构造器,使用set()/get(),使用Builder.
使用构造器: 当参数很多时,bean中需要定义很多构造器,而且对于相同类型的参数,传参顺序可能会出错,虽然可以归类参数(Effectice Java第二章有讲解),但是构造器仍然会很多
使用set/get:步骤繁多,每个参数都需要set
使用Builder:支持Fluent Interface,可以方便的创建实例
public class Person { private int id; private String name; private String sex; private String friend; private Person() { } private Person(Builder builder) { this.id = builder.id; this.name = builder.name; this.sex = builder.sex; this.friend = builder.friend; } public int getId() { return id; } public String getName() { return name; } public String getSex() { return sex; } public String getFriend() { return friend; } @Override public String toString() { return "Person{" + "id=" + id + ", name='" + name + '\'' + ", sex='" + sex + '\'' + ", friend=" + friend + '}'; } public static class Builder { private int id; private String name; private String sex; private String friend; public Builder withId(int id) { this.id = id; return this; } public Builder withName(String name) { this.name = name; return this; } public Builder withSex(String sex) { this.sex = sex; return this; } public Builder withFriend(String friend) { this.friend = friend; return this; } public Person build() { return new Person(this); } } public static void main(String[] args) { Person newP = new Person.Builder().withId(999).withName("hahaha").withSex("girl").withFriend("gou").build(); System.out.println(newP); Person p = new Person.Builder().build(); System.out.println(p); } }