【图解设计模式】Abstract Factory模式

抽象工厂将抽象零件组装为抽象产品。

示例

将带有层次关系的链接的集合制作成HTML文件。

类图

e06cdJ.png

Item类

1
2
3
4
5
6
7
8
9
10
11
package factory;

public abstract class Item {
protected String caption;

public Item(String caption) {
this.caption = caption;
}

public abstract String makeHTML();
}

Link类

1
2
3
4
5
6
7
8
9
10
package factory;

public abstract class Link extends Item {
protected String url;

public Link(String caption, String url) {
super(caption);
this.url = url;
}
}

Tray类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package factory;

import java.util.ArrayList;

public abstract class Tray extends Item {
protected ArrayList tray = new ArrayList();

public Tray(String caption) {
super(caption);
}

public void add(Item item) {
tray.add(item);
}
}

Page类

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
package factory;

import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.ArrayList;

public abstract class Page {
protected String title;
protected String author;
protected ArrayList content = new ArrayList();

public Page(String title, String author) {
this.title = title;
this.author = author;
}

public void add(Item item) {
content.add(item);
}

public void output() {
try {
String filename = title + ".html";
Writer writer = new FileWriter(filename);
writer.write(this.makeHTML());
writer.close();
System.out.println(filename + "编写完成。");
} catch (IOException e) {
e.printStackTrace();
}
}

public abstract String makeHTML();
}

Factory类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package factory;

public abstract class Factory {
public static Factory getFactory(String classname) {
Factory factory = null;

try {
factory = (Factory) Class.forName(classname).newInstance();
} catch (ClassNotFoundException e) {
System.err.println("没有找到" + classname + "类。");
} catch (Exception e) {
e.printStackTrace();
}

return factory;
}

public abstract Link createLink(String caption, String url);
public abstract Tray createTray(String caption);
public abstract Page createPage(String title, String author);
}

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
32
33
34
35
36
37
38
39
40
41
42
43
44
import factory.Factory;
import factory.Link;
import factory.Page;
import factory.Tray;

public class Main {

public static void main(String[] args) {
if (args.length != 1) {
System.out.println("Usage: java Main class.name.of.ConcreteFactory");
System.out.println("Example 1: java Main listfactory.ListFactory");
System.out.println("Example 2: java Main tablefactory.TableFactory");
System.exit(0);
}

Factory factory = Factory.getFactory(args[0]);

Link people = factory.createLink("人民日报", "http://www.people.com.cn/");
Link gmw = factory.createLink("光明日报", "http://www.gmw.cn/");

Link us_yahoo = factory.createLink("Yahoo!", "http://www.yahoo.com/");
Link jp_yahoo = factory.createLink("Yahoo!Japan", "http://www.yahoo.co.jp/");
Link excite = factory.createLink("Excite", "http://www.excite.com/");
Link google = factory.createLink("Google", "http://www.google.com/");

Tray traynews = factory.createTray("日报");
traynews.add(people);
traynews.add(gmw);

Tray trayyahoo = factory.createTray("Yahoo!");
trayyahoo.add(us_yahoo);
trayyahoo.add(jp_yahoo);

Tray traysearch = factory.createTray("检索引擎");
traysearch.add(trayyahoo);
traysearch.add(excite);
traysearch.add(google);

Page page = factory.createPage("LinkPage", "杨文轩");
page.add(traynews);
page.add(traysearch);
page.output();
}
}

ListFactory类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package listfactory;

import factory.Factory;
import factory.Link;
import factory.Page;
import factory.Tray;

public class ListFactory extends Factory {
@Override
public Link createLink(String caption, String url) {
return new ListLink(caption, url);
}

@Override
public Tray createTray(String caption) {
return new ListTray(caption);
}

@Override
public Page createPage(String title, String author) {
return new ListPage(title, author);
}
}

ListLink类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
package listfactory;

import factory.Link;

public class ListLink extends Link {
public ListLink(String caption, String url) {
super(caption, url);
}

@Override
public String makeHTML() {
return " <li><a href=\"" + url + "\">" + caption + "</a></li>\n";
}
}

ListTray类

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
package listfactory;

import factory.Item;
import factory.Tray;

import java.util.Iterator;

public class ListTray extends Tray {
public ListTray(String caption) {
super(caption);
}

@Override
public String makeHTML() {
StringBuffer buffer = new StringBuffer();
buffer.append("<li>\n");
buffer.append(caption + "\n");
buffer.append("<ul>\n");

Iterator it = tray.iterator();
while (it.hasNext()) {
Item item = (Item) it.next();
buffer.append(item.makeHTML());
}

buffer.append("</ul>\n");
buffer.append("</li>\n");
return buffer.toString();
}
}

ListPage类

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
package listfactory;

import factory.Item;
import factory.Page;

import java.util.Iterator;

public class ListPage extends Page {
public ListPage(String title, String author) {
super(title, author);
}

@Override
public String makeHTML() {
StringBuffer buffer = new StringBuffer();
buffer.append("<html><head><title>" + title + "</title></head>\n");
buffer.append("<body>\n");
buffer.append("<h1>" + title + "</h1>\n");
buffer.append("<ul>\n");

Iterator it = content.iterator();
while (it.hasNext()) {
Item item = (Item) it.next();
buffer.append(item.makeHTML());
}

buffer.append("</ul>\n");
buffer.append("<hr><address>" + author + "</address>");
buffer.append("</body></html>\n");
return buffer.toString();
}
}

运行结果

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
<html><head><title>LinkPage</title></head>
<body>
<h1>LinkPage</h1>
<ul>
<li>
日报
<ul>
<li><a href="http://www.people.com.cn/">人民日报</a></li>
<li><a href="http://www.gmw.cn/">光明日报</a></li>
</ul>
</li>
<li>
检索引擎
<ul>
<li>
Yahoo!
<ul>
<li><a href="http://www.yahoo.com/">Yahoo!</a></li>
<li><a href="http://www.yahoo.co.jp/">Yahoo!Japan</a></li>
</ul>
</li>
<li><a href="http://www.excite.com/">Excite</a></li>
<li><a href="http://www.google.com/">Google</a></li>
</ul>
</li>
</ul>
<hr><address>杨文轩</address></body></html>

eD69Gn.png

TableFactory类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package tablefactory;

import factory.Factory;
import factory.Link;
import factory.Page;
import factory.Tray;

public class TableFactory extends Factory {
@Override
public Link createLink(String caption, String url) {
return new TableLink(caption, url);
}

@Override
public Tray createTray(String caption) {
return new TableTray(caption);
}

@Override
public Page createPage(String title, String author) {
return new TablePage(title, author);
}
}

TableLink类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
package tablefactory;

import factory.Link;

public class TableLink extends Link {
public TableLink(String caption, String url) {
super(caption, url);
}

@Override
public String makeHTML() {
return "<td><a href=\"" + url + "\">" + caption + "</a></td>\n";
}
}

TableTray类

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
package tablefactory;

import factory.Item;
import factory.Tray;

import java.util.Iterator;

public class TableTray extends Tray {
public TableTray(String caption) {
super(caption);
}

@Override
public String makeHTML() {
StringBuffer buffer = new StringBuffer();
buffer.append("<td>");
buffer.append("<table width=\"100%\" border=\"1\"><tr>");
buffer.append("<td bgcolor=\"#cccccc\" align=\"center\" colspan=\""
+ tray.size() + "\"><b>" + caption + "</b></td>");
buffer.append("</tr>\n");
buffer.append("<tr>\n");

Iterator it = tray.iterator();
while (it.hasNext()) {
Item item = (Item) it.next();
buffer.append(item.makeHTML());
}

buffer.append("</tr></table>");
buffer.append("</td>");
return buffer.toString();
}
}

TablePage类

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
package tablefactory;

import factory.Item;
import factory.Page;

import java.util.Iterator;

public class TablePage extends Page {
public TablePage(String title, String author) {
super(title, author);
}

@Override
public String makeHTML() {
StringBuffer buffer = new StringBuffer();
buffer.append("<html><head><title>" + title + "</title></head>\n");
buffer.append("<body>\n");
buffer.append("<h1>" + title + "</h1>\n");
buffer.append("<table width=\"80%\" border=\"3\">\n");

Iterator it = content.iterator();
while (it.hasNext()) {
Item item = (Item) it.next();
buffer.append("<tr>" + item.makeHTML() + "</tr>");
}

buffer.append("</table>\n");
buffer.append("<hr><address>" + author + "</address>");
buffer.append("</body></html>\n");
return buffer.toString();
}
}

运行结果

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<html><head><title>LinkPage</title></head>
<body>
<h1>LinkPage</h1>
<table width="80%" border="3">
<tr><td><table width="100%" border="1"><tr><td bgcolor="#cccccc" align="center" colspan="2"><b>日报</b></td></tr>
<tr>
<td><a href="http://www.people.com.cn/">人民日报</a></td>
<td><a href="http://www.gmw.cn/">光明日报</a></td>
</tr></table></td></tr><tr><td><table width="100%" border="1"><tr><td bgcolor="#cccccc" align="center" colspan="3"><b>检索引擎</b></td></tr>
<tr>
<td><table width="100%" border="1"><tr><td bgcolor="#cccccc" align="center" colspan="2"><b>Yahoo!</b></td></tr>
<tr>
<td><a href="http://www.yahoo.com/">Yahoo!</a></td>
<td><a href="http://www.yahoo.co.jp/">Yahoo!Japan</a></td>
</tr></table></td><td><a href="http://www.excite.com/">Excite</a></td>
<td><a href="http://www.google.com/">Google</a></td>
</tr></table></td></tr></table>
<hr><address>杨文轩</address></body></html>

eDgr5Q.png

登场角色

AbstractProduct(抽象产品)

AbstractProduct角色负责定义AbstractFactory角色所产生的抽象零件和产品的接口(API)。在示例程序中,由Link类、Tray类和Page类扮演此角色。

AbstractFactory(抽象工厂)

AbstractFactory角色负责定义用于生成抽象产品的接口(API)。在示例程序中,由Factory类扮演此角色。

Client(委托者)

Client角色仅会调用AbstractFactory角色和AbstractProduct角色的接口(API)来进行工作,对于具体的零件、产品和工厂一无所知。在示例程序中,由Main类扮演此角色。

ConcreteProduct(具体产品)

ConcreteProduct角色负责实现AbstractProduct角色的接口(API)。在示例程序中,由以下包中的以下类扮演此角色:

  • listfactory包:ListLink类、ListTray类和ListPage类
  • tablefactory包:TableLink类、TableTray类和TablePage类

ConcreteFactory(具体工厂)

ConcreteFactory角色负责实现AbstractFactory角色的接口(API)。在示例程序中,由以下包中的以下类扮演此角色:

  • listfactory包:ListFactory类
  • tablefactory包:TableFactory类

类图

eDgRK0.png