设计模式-抽象工厂模式

抽象工厂模式

定义

抽象工厂模式(Abstract Factory Pattern)是围绕一个超级工厂创建其他工厂。该超级工厂又称为其他工厂的工厂。这种类型的设计模式属于创建型模式,它提供了一种创建对象的最佳方式。

在抽象工厂模式中,接口是负责创建一个相关对象的工厂,不需要显式指定它们的类。每个生成的工厂都能按照工厂模式提供对象。

类图

抽象工厂模式类图

AbstractProduct(抽象产品)

负责定义AbstractFactory角色所生成的抽象零件和产品的接口

AbstractFactory(抽象工厂)

负责定义用于生成抽象产品的接口的接口

ConcreteProduct(具体产品)

负责实现AbstractProduct角色的接口

ConcreteFactory(具体工厂)

负责实现AbstractFactory角色的接口

示例

类图

抽象工厂模式示例类图

Item

1
2
3
4
5
6
7
8
9
public abstract class Item {
protected String caption;

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

public abstract String makeHTML();
}
1
2
3
4
5
6
7
8
9
10
11
public abstract class Link extends Item {

protected String url;

public Link(String caption, String url) {
super(caption);
// TODO Auto-generated constructor stub
this.url = url;
}
}

Tray

1
2
3
4
5
6
7
8
9
10
11
12
public abstract class Tray extends Item {
protected ArrayList<Item> tray = new ArrayList<>();

public Tray(String caption) {
super(caption);
// TODO Auto-generated constructor stub
}

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
public abstract class Page {
protected String title;

protected String author;

protected ArrayList<Item> 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();
} 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
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);
}

1
2
3
4
5
6
7
8
9
10
public class ListLink extends Link {

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

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
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<Item> it = tray.iterator();
while (it.hasNext()) {
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
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<Item> it = content.iterator();
while (it.hasNext()) {
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();
}

}

ListFactory

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
public class ListFactory extends Factory {

@Override
public Link createLink(String caption, String url) {
// TODO Auto-generated method stub
return new ListLink(caption, url);
}

@Override
public Tray createTray(String caption) {
// TODO Auto-generated method stub
return new ListTray(caption);
}

@Override
public Page createPage(String title, String author) {
// TODO Auto-generated method stub
return new ListPage(title, 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
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("人民日报", "1");
Link gmw = factory.createLink("光明日报", "2");
Link us_yahoo = factory.createLink("美国雅虎", "3");
Link cn_yahoo = factory.createLink("中国亚虎", "4");
Link excite = factory.createLink("Excite", "5");
Link google = factory.createLink("Google", "6");

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

Tray trayYahoo = factory.createTray("雅虎");
trayYahoo.add(us_yahoo);
trayYahoo.add(cn_yahoo);

Tray traySearch = factory.createTray("搜索引擎");
traySearch.add(trayYahoo);
traySearch.add(excite);
traySearch.add(google);

Page page = factory.createPage("LinkPage", "XXX");
page.add(traynews);
page.add(traySearch);
page.output();
}
}

总结

在抽象工厂模式中增加具体的工厂是非常容易的。假设我们要在示例程序中增加新的具体工厂,那么需要做的就是编写Factory、Link、Tray、Page这4个类的子类,并实现它们定义的抽象方法。

但是要增加新的零件是比较困难的。如果要增加一个Picture零件的话,需要对所有具体工厂进行修改才行。已经完成的具体工厂类越多,修改的工作量就越大

0%