BuilderParttern(建造者模式)

时间:2022-12-04 20:01:30
/**
* 建造者模式
* 主要用于构造复杂的对象
* 在优朋播放器就是采用建造者构建的,可以说比较有心得吧
* @author TMAC-J
*
*/
public class BuilderPattern {
/**
* 要构建的对象bean
* @author voole
*
*/
public class Person{ private String sex; private String height; private String weight; private String hobby; public String getSex() {
return sex;
} public void setSex(String sex) {
this.sex = sex;
} public String getHeight() {
return height;
} public void setHeight(String height) {
this.height = height;
} public String getWeight() {
return weight;
} public void setWeight(String weight) {
this.weight = weight;
} public String getHobby() {
return hobby;
} public void setHobby(String hobby) {
this.hobby = hobby;
}
} interface IBuilder{ void buildSex(String fale); void buildHegith(String height); void buildWeight(String weight); void buildHobby(String hobby); Person buildPerson();
} public class PersonBuilder implements IBuilder{ Person person; public PersonBuilder() {
person = new Person();
} @Override
public void buildSex(String fale) {
person.setSex(fale);
} @Override
public void buildHegith(String height) {
person.setHeight(height);
} @Override
public void buildWeight(String weight) {
person.setWeight(weight);
} @Override
public void buildHobby(String hobby) {
person.setHobby(hobby);
} @Override
public Person buildPerson() {
return person;
} } public void test(){
IBuilder personBuilder = new PersonBuilder();
personBuilder.buildHegith("我柯一米五!!!");
//后面同上
} }