Skip to content

Commit 1531bc0

Browse files
authored
建造者模式
建造者模式
1 parent 29046e6 commit 1531bc0

File tree

6 files changed

+123
-0
lines changed

6 files changed

+123
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.java.design.build;
2+
3+
/**
4+
* 建造者模式 -----> 是将一个复杂的对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示
5+
*
6+
* 如:去肯德基,汉堡、可乐、薯条、炸鸡翅等是不变的,而其组合是经常变化的,生成出所谓的"套餐"
7+
*
8+
* @author Administrator
9+
*
10+
*/
11+
public class BuilderPattern {
12+
13+
public static void main(String[] args) {
14+
15+
PersonDirector personDirector = new PersonDirector();
16+
Person person = personDirector.construtorPerson(new ManBuilder());
17+
System.out.println(person);
18+
}
19+
20+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.java.design.build;
2+
3+
public class Man extends Person {
4+
5+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.java.design.build;
2+
3+
public class ManBuilder implements PersonBuilder {
4+
5+
Person person;
6+
7+
public ManBuilder() {
8+
9+
person = new Man();
10+
}
11+
12+
@Override
13+
public void buildBody() {
14+
15+
person.setBody("建造身体...");
16+
}
17+
18+
@Override
19+
public void buildFoot() {
20+
21+
person.setFoot("建造脚...");
22+
}
23+
24+
@Override
25+
public void buildHead() {
26+
27+
person.setHead("建造头...");
28+
}
29+
30+
@Override
31+
public Person buildPerson() {
32+
return person;
33+
}
34+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.java.design.build;
2+
3+
public class Person {
4+
5+
private String head;
6+
private String body;
7+
private String foot;
8+
9+
public String getHead() {
10+
return head;
11+
}
12+
13+
public void setHead(String head) {
14+
this.head = head;
15+
}
16+
17+
public String getBody() {
18+
return body;
19+
}
20+
21+
public void setBody(String body) {
22+
this.body = body;
23+
}
24+
25+
public String getFoot() {
26+
return foot;
27+
}
28+
29+
public void setFoot(String foot) {
30+
this.foot = foot;
31+
}
32+
33+
@Override
34+
public String toString() {
35+
return "Person [head=" + head + ", body=" + body + ", foot=" + foot + "]";
36+
}
37+
38+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.java.design.build;
2+
3+
public interface PersonBuilder {
4+
5+
void buildHead();
6+
7+
void buildBody();
8+
9+
void buildFoot();
10+
11+
Person buildPerson();
12+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.java.design.build;
2+
3+
public class PersonDirector {
4+
5+
public Person construtorPerson(PersonBuilder pb) {
6+
7+
pb.buildHead();
8+
pb.buildBody();
9+
pb.buildFoot();
10+
11+
return pb.buildPerson();
12+
13+
}
14+
}

0 commit comments

Comments
 (0)