package ;
import ;
/**
* Created by IntelliJ IDEA.
* User: yingkuohao
* Date: 14-1-26
* Time: 上午10:20
* CopyRight:360buy
* Descrption: 建造者模式
* To change this template use File | Settings | File Templates.
*/
public class Dog {
private final String name; //名称
private final String sex; //性别
private final int age; //年龄
private final String fur; //毛发
private final String breed; //品种
private final float weight; //体重
private final float height; //身高
private Dog(Builder builder) { //私有构造方法,入参为内部类对象
name = ;
sex = ;
age = ;
fur = ;
breed = ;
weight = ;
height = ;
}
@Override
public String toString() {
return "Dog{" +
"name='" + name + '\'' +
", sex='" + sex + '\'' +
", age=" + age +
", fur='" + fur + '\'' +
", breed='" + breed + '\'' +
", weight=" + weight +
", height=" + height +
'}';
}
public static class Builder {
private final String name; //名称
private String sex; //性别
private int age; //年龄
private String fur; //毛发
private String breed; //品种
private float weight; //体重
private float height; //身高
public Builder(String name) {
= name;
}
/**
* 同名方法,返回this,即builder对象,方面连续build
*
* @param sex
* @return
*/
public Builder sex(String sex) {
= sex;
return this;
}
public Builder age(int age) {
= age;
return this;
}
public Builder fur(String fur) {
= fur;
return this;
}
public Builder breed(String breed) {
= breed;
return this;
}
public Builder weight(float weight) {
= weight;
return this;
}
public Builder height(float height) {
= height;
return this;
}
/**
* build方法,构造Dog对象,也是一种单例模式的使用方式
*
* @return
*/
public Dog build() {
return new Dog(this);
}
}
}