关于工厂模式,参见链接(设计模式:Factory Method模式)在Abstract Factory模式中,不仅有“抽象工厂”,还有“抽象零件”和“抽象产品”。抽象工厂的工作是将“抽象零件”组装成“抽象产品”。
我们并不关心零件的具体实现,而是只关心接口(API)。我们仅使用该接口(API)将零件组装成为产品。
下面的示例程序的功能是将带有层次关系的链接的集合制作成HTML文件。
在示例程序中,类被划分为以下3个包。
包 | 名字 | 说明 |
---|---|---|
factory | Factory | 表示抽象工厂的类(制作Link,Tray,Page) |
factory | Item | 方便统一处理Link和Tray的类 |
factory | Link | 抽象零件:表示HTML的链接的类 |
factory | Tray | 抽象零件:表示含有Link和Tray的类 |
factory | Page | 抽象零件:表示HTML页面的类 |
default | Main | 测试程序行为的类 |
listfactory | ListFactory | 表示具体工厂的类(制作ListLink,ListTray,ListPage) |
listfactory | ListLink | 具体零件:表示HTML的链接的类 |
listfactory | ListTray | 具体零件:表示含有Link和Tray的类 |
listfactory | ListPage | 具体零件:表示HTML页面的类 |
tablefactory | TableFactory | 表示具体工厂的类(制作ListLink,ListTray,ListPage) |
tablefactory | TableLink | 具体零件:表示HTML的链接的类 |
tablefactory | TableTray | 具体零件:表示含有Link和Tray的类 |
tablefactory | TablePage | 具体零件:表示HTML页面的类 |
每个包中的类依次如下。
package factory;
public abstract class Item {
protected String caption;
public Item(String caption) {
this.caption = caption;
}
public abstract String makeHTML();
}
package factory;
public abstract class Link extends Item {
protected String url;
public Link(String caption, String url) {
super(caption);
this.url = url;
}
}
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);
}
}
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 + " has wrote.");
} catch (IOException e) {
e.printStackTrace();
}
}
public abstract String makeHTML();
}
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("have not found " + classname + " class.");
} 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);
}
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", "F.Mortal");
page.add(traynews);
page.add(traysearch);
page.output();
}
}
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);
}
}
package listfactory;
import factory.Link;
public class ListLink extends Link {
public ListLink(String caption, String url) {
super(caption, url);
}
@Override
public String makeHTML() {
return " + url + "\">" + caption + " \n";
}
}
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("\n" );
buffer.append(caption + "\n");
buffer.append("\n"
);
Iterator it = tray.iterator();
while (it.hasNext()) {
Item item = (Item) it.next();
buffer.append(item.makeHTML());
}
buffer.append("\n");
buffer.append("\n");
return buffer.toString();
}
}
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("" + title + "\n");
buffer.append("\n");
buffer.append(""
+ title + "\n");
buffer.append("\n"
);
Iterator it = content.iterator();
while (it.hasNext()) {
Item item = (Item) it.next();
buffer.append(item.makeHTML());
}
buffer.append("\n");
buffer.append("
" + author + "");
buffer.append("\n");
return buffer.toString();
}
}
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);
}
}
package tablefactory;
import factory.Link;
public class TableLink extends Link {
public TableLink(String caption, String url) {
super(caption, url);
}
@Override
public String makeHTML() {
return " + url + "\">" + caption + " \n";
}
}
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("");
buffer.append("");
buffer.append(" +
tray.size() + "\">" + caption + " ");
buffer.append(" \n");
buffer.append("\n");
Iterator it = tray.iterator();
while (it.hasNext()) {
Item item = (Item) it.next();
buffer.append(item.makeHTML());
}
buffer.append("
");
buffer.append(" ");
return buffer.toString();
}
}
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("" + title + "\n");
buffer.append("\n");
buffer.append(""
+ title + "\n");
buffer.append("\n");
Iterator it = content.iterator();
while (it.hasNext()) {
Item item = (Item)it.next();
buffer.append("" + item.makeHTML() + " ");
}
buffer.append("
\n");
buffer.append("
" + author + "");
buffer.append("\n");
return buffer.toString();
}
}
在程序运行的时候需要传入参数“listfactory.ListFactory”或“tablefactory.TableFactory”来决定使用List还是Table来生成HTML文档
示例输出如下:
<html><head><title>LinkPagetitle>head>
<body>
<h1>LinkPageh1>
<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!Japana>li>
ul>
li>
<li><a href="http://www.excite.com/">Excitea>li>
<li><a href="http://www.google.com/">Googlea>li>
ul>
li>
ul>
<hr><address>F.Mortaladdress>body>html>
<html><head><title>LinkPagetitle>head>
<body>
<h1>LinkPageh1>
<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!Japana>td>
tr>table>td><td><a href="http://www.excite.com/">Excitea>td>
<td><a href="http://www.google.com/">Googlea>td>
tr>table>td>tr>table>
<hr><address>F.Mortaladdress>body>html>
AbstractProduct角色负责定义AbstractFactory角色所生成的抽象零件和产品的接口(API)。在示例程序中,由Item类,Link类,Tray类和Page类扮演此角色。
AbstractFactory角色负责定义用于生成抽象产品的接口(API)。在示例程序中,由Factory类扮演此角色。
Client角色仅会调用AbstractFac角色和AbstractProduct角色的接口(API)来进行工作。对于具体的零件,产品和工厂一无所知。在示例程序中,由Main类扮演此角色。
ConcreteProduct角色负责实现AbstractProduct角色的接口(API)。在示例程序中,以下类扮演此角色:
listfactory包:ListLink类,ListTray类和ListPage类。
tablefactory包:TableLink类,TableTray类和TablePage类。
ConcreteFactory角色负责实现AbstractFactory角色的接口(API)。在示例程序中,由ListFactory类和TableFactory类扮演此角色。
假设要在示例程序中增加新的具体工厂,那么只需要编写Factory,Link,Tray和Page这4个类的子类,并实现它们定义的抽象方法。这样,无论要增加多少个具体工厂(或是要修改具体工厂的Bug),都无需修改抽象工厂和Main部分。
而如果要在示例程序中增加新的零件,很多类都需要修改。例如,新增Picture零件,那么我们必须做以下修改:
想了解更多关于设计模式:设计模式专栏
设计模式总结