设计模式-建造者模式

建造者模式

定义

建造者模式(Builder Pattern)使用多个简单的对象一步一步构建成一个复杂的对象。这种类型的设计模式属于创建型模式,它提供了一种创建对象的最佳方式。

一个 Builder 类会一步一步构造最终的对象。该 Builder 类是独立于其他对象的。

类图

建造者模式类图

Builder(建造者)

负责定义用于生成实例的接口。准备了用于生成实例的方法

ConcreteBuilder(具体的建造者)

负责实现Builder角色的接口的类。定义了在生成实例时实际被调用的方法。此外,还定义了获取最终结果的方法。

Director(监工)

负责使用Builder的接口生成实例,不依赖于ConcreteBuilder。为了确保不论ConcreteBuilder是如何被定义的,Director都能正常工作,它只调用在Builder中被定义的方法。

Client(使用者)

使用了Builder模式

建造者模式时序图

示例

类图

建造者模式示例类图

Builder

1
2
3
4
5
6
public abstract class Builder {
public abstract void makeTitle(String title);
public abstract void makeString(String str);
public abstract void makeItem(String[] items);
public abstract void close();
}

Director

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public class Director {
private Builder builder;

public Director(Builder builder) {
this.builder = builder;
}

public void construct() {
builder.makeTitle("Greating");
builder.makeString("from morning to afternoon");
builder.makeItem(new String[] {"Good morning", "Good afternoon"});
builder.makeString("Evening");
builder.makeItem(new String[] {"Good evening", "Good night", "Bye"});
builder.close();
}
}

HTMLBuilder

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
public class HTMLBuilder extends Builder {
private String fileName;

private PrintWriter writer;

@Override
public void makeTitle(String title) {
// TODO Auto-generated method stub
fileName = title + ".html";
try {
writer = new PrintWriter(new FileWriter(fileName));
} catch (IOException e) {
e.printStackTrace();
}
writer.println("<html><head><title>" + title + "</title></head><<body>");
writer.println("<h1>" + title + "</h1>");
}

@Override
public void makeString(String str) {
// TODO Auto-generated method stub
writer.println("<p>" + str + "</p>");
}

@Override
public void makeItem(String[] items) {
// TODO Auto-generated method stub
writer.println("<ul>");
for (int i = 0;i < items.length;i++) {
writer.println("<li>" + items[i] + "</li>");
}
writer.println("</ul>");
}

@Override
public void close() {
// TODO Auto-generated method stub
writer.println("</body></html>");
writer.close();
}

public String getResult() {
return fileName;
}

}

TextBuilder

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
public class TextBuilder extends Builder {
private StringBuffer buffer = new StringBuffer();

@Override
public void makeTitle(String title) {
// TODO Auto-generated method stub
buffer.append("================================\n");
buffer.append("[" + title + "]\n");
buffer.append("\n");
}

@Override
public void makeString(String str) {
// TODO Auto-generated method stub
buffer.append(" " + str + "\n");
buffer.append("\n");
}

@Override
public void makeItem(String[] items) {
// TODO Auto-generated method stub
for (int i = 0;i < items.length;i++) {
buffer.append(" . " + items[i] + "\n");
}
buffer.append("\n");
}

@Override
public void close() {
// TODO Auto-generated method stub
buffer.append("================================\n");
}

public String getResult() {
return buffer.toString();
}
}

Main

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
public class Main {
public static void main(String[] args) {
if (args.length != 1) {
usage();
System.exit(0);
}

if (args[0].equals("plain")) {
TextBuilder textBuilder = new TextBuilder();
Director director = new Director(textBuilder);
director.construct();
String result = textBuilder.getResult();
System.out.println(result);
} else if (args[0].equals("html")) {
HTMLBuilder htmlBuilder = new HTMLBuilder();
Director director = new Director(htmlBuilder);
director.construct();
String result = htmlBuilder.getResult();
System.out.println(result + " 文件编写完成");
} else {
usage();
System.exit(0);
}
}

public static void usage() {
System.out.println("Usage: java main plain 编写纯文本文档");
System.out.println("Usage: java main html 编写html文档");
}
}

0%