建造者模式(Builder)

特点:

在需要生成复杂内部结构时使用,即将多个简单的对象一步一步构建称为一个复杂对象。

构建方式(1)

新建Person.java类

public class Person {
public int age;
public String name;
public String sex;
public String address;
public String born;

@Override
public String toString() {
return "Person{" +
"age=" + age +
", name='" + name + '\'' +
", sex='" + sex + '\'' +
", address='" + address + '\'' +
", born='" + born + '\'' +
'}';
}

public int getAge() {
return age;
}

public void setAge(int age) {
this.age = age;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getSex() {
return sex;
}

public void setSex(String sex) {
this.sex = sex;
}

public String getAddress() {
return address;
}

public void setAddress(String address) {
this.address = address;
}

public String getBorn() {
return born;
}

public void setBorn(String born) {
this.born = born;
}

}

新建构造(builder)类PersonBuilder .java

public class PersonBuilder {

private Person mPerson;

public PersonBuilder(){
mPerson = new Person();
}
public PersonBuilder setAge(int age){
mPerson.age = age;
return this;
}

public PersonBuilder setName(String name){
mPerson.name = name;
return this;
}

public PersonBuilder setSex(String sex){
mPerson.sex = sex;
return this;
}

public PersonBuilder setAddress(String address){
mPerson.address = address;
return this;
}

public PersonBuilder setBorn(String born){
mPerson.born = born;
return this;
}

public Person create(){
Log.i("msg", mPerson.toString());
return mPerson;
}

}

使用如下:

public void onClick(View view){
PersonBuilder builder = new PersonBuilder();
builder.setAge(20)
.setName("张三")
.setAddress("北京市")
.setSex("男")
.setBorn("厦门")
.create();
PersonBuilder builder1 = new PersonBuilder();
builder1.setAge(22)
.setName("李四")
.setAddress("上海市")
.setSex("女")
.setBorn("福州")
.create();
}

看到这里是不是与构建对话框与OKHTTP3类似呢?

构建方式(2)

新建Person.java类

public class Person {
public int age;
public String name;
public String sex;
public String address;
public String born;

@Override
public String toString() {
return "Person{" +
"age=" + age +
", name='" + name + '\'' +
", sex='" + sex + '\'' +
", address='" + address + '\'' +
", born='" + born + '\'' +
'}';
}

public int getAge() {
return age;
}

public void setAge(int age) {
this.age = age;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getSex() {
return sex;
}

public void setSex(String sex) {
this.sex = sex;
}

public String getAddress() {
return address;
}

public void setAddress(String address) {
this.address = address;
}

public String getBorn() {
return born;
}

public void setBorn(String born) {
this.born = born;
}

}

Ibuilder.java

public interface Ibuilder {
void setAge(int age);
void setName(String name);
void setAddress(String address);
void setBorn(String born);
void setSex(String sex);
}

ImplBuilder.java

public class ImplBuilder implements Ibuilder {

private Person mPerson;

public ImplBuilder(){
mPerson = new Person();
}

@Override
public void setAge(int age) {
mPerson.setAge(age);
}

@Override
public void setName(String name) {
mPerson.setName(name);
}

@Override
public void setAddress(String address) {
mPerson.setAddress(address);
}

@Override
public void setBorn(String born) {
mPerson.setBorn(born);
}

@Override
public void setSex(String sex) {
mPerson.setSex(sex);
}

public Person create(){
Log.i("msg", mPerson.toString());
return mPerson;
}

}

使用如下:

ImplBuilder builder = new ImplBuilder();
builder.setAge(20);
builder.setName("王五");
builder.setAddress("南京");
builder.setBorn("武汉");
builder.setSex("男");
builder.create();