首页 > 代码库 > Builder(建造者模式)
Builder(建造者模式)
public interface Builder {
public void buildHead();
public void buildBody();
public void buildHand();
public void buildFoot();
public Person buildPerson();
}
public class ManBuilder implements Builder{
Person person;
public ManBuilder(){
person = new Man();
}
@Override
public void buildHead() {
person.setHead("build man‘s head!");
}
@Override
public void buildBody() {
person.setBody("build man‘s body!");
}
@Override
public void buildHand() {
person.setHand("build man‘s hand!");
}
@Override
public void buildFoot() {
person.setFoot("build man‘s food!");
}
@Override
public Person buildPerson() {
return person;
}
}
public class Person {
private String head;
private String hand;
private String body;
private String foot;
public String getHead() {
return head;
}
public void setHead(String head) {
this.head = head;
}
public String getHand() {
return hand;
}
public void setHand(String hand) {
this.hand = hand;
}
public String getBody() {
return body;
}
public void setBody(String body) {
this.body = body;
}
public String getFoot() {
return foot;
}
public void setFoot(String foot) {
this.foot = foot;
}
}
public class PersonDirector {
//指挥创建
public Person ConstructPerson(Builder b)
{
b.buildFoot();
b.buildHand();
b.buildHead();
b.buildBody();
return b.buildPerson();
}
}
public class Man extends Person{
}
public class BuilderDemo {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
PersonDirector pd = new PersonDirector();
Person person = pd.ConstructPerson(new ManBuilder());
System.out.println(person.getHead());
System.out.println(person.getBody());
System.out.println(person.getHand());
System.out.println(person.getFoot());
}
}
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。