图解设计模式--Abstract Factory(抽象工厂)
2017-08-17 本文已影响22人
Ginger12
将关联的零件组装成产品
Abstract Factory 模式
抽象工厂的工作是将“抽象零件”组装为“抽象产品”。
我们并不关心零件的具体实现,而是只关心接口。我们仅使用该接口将零件组装成为产品。
示例代码
文件目录
--Main.java
|--factory
| |--Factory.java
| |--Item.java
| |--Link.java
| |--Tray.java
| |--Page.java
|
|--listfacetory
|--ListFactory.java
|--ListLink.java
|--ListTray.java
|--ListPage.java
Item.java
package abstractFactory.factory;
public abstract class Item {
protected String caption;
public Item(String caption) {
this.caption = caption;
}
public abstract String makeHTML();
}
Link.java(AbstractProduct)
package abstractFactory.factory;
public abstract class Link extends Item {
protected String url;
public Link(String caption, String url) {
super(caption);
this.url = url;
}
}
Tray.java(AbstractProduct)
package abstractFactory.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.java(AbstractProduct)
package abstractFactory.factory;
import java.io.*;
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.java(AbstractFactory)
package abstractFactory.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.out.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.java(Client)
package abstractFactory;
import abstractFactory.factory.Factory;
import abstractFactory.factory.Link;
import abstractFactory.factory.Page;
import abstractFactory.factory.Tray;
public class Main {
public static void main(String[] args){
Factory factory = Factory.getFactory("abstractFactory.listfactory.ListFactory");
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.java(ConcreteFactory)
package abstractFactory.listfactory;
import abstractFactory.factory.Factory;
import abstractFactory.factory.Link;
import abstractFactory.factory.Page;
import abstractFactory.factory.Tray;
public class ListFactory extends Factory {
public Link createLink(String caption, String url) {
return new ListLink(caption, url);
}
public Tray createTray(String caption) {
return new ListTray(caption);
}
public Page createPage(String title, String author) {
return new ListPage(title, author);
}
}
ListLink.java(ConcreteProduct)
package abstractFactory.listfactory;
import abstractFactory.factory.Link;
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.java(ConcreteProduct)
package abstractFactory.listfactory;
import abstractFactory.factory.Item;
import abstractFactory.factory.Tray;
import java.util.Iterator;
public class ListTray extends Tray {
public ListTray(String caption) {
super(caption);
}
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.java(ConcreteProduct)
package abstractFactory.listfactory;
import abstractFactory.factory.Item;
import abstractFactory.factory.Page;
import java.util.Iterator;
public class ListPage extends Page {
public ListPage(String title, String author) {
super(title, author);
}
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>\n");
buffer.append("</body></html>\n");
return buffer.toString();
}
}
Abstract Factory 模式中的角色
-
AbstractProduct(抽象产品)
AbstractProduct角色负责定义AbstractFactory角色所生成的抽象零件和产品的接口。
-
AbstractFactory(抽象工厂)
AbstractFactory角色负责定义用户生成抽象产品的接口。
-
Client(委托者)
Client角色仅会调用 AbstractFactory 角色和 AbstractProduct 角色的接口来进行工作,对于具体的零件、产品和工厂一无所知。
-
ConcreteProduct(具体产品)
ConcreteProduct 角色负责实现 AbstractProduct 角色的接口。
-
ConcreteFactory(具体工厂)
ConcreteFactory 角色负责实现 AbstractFactory 角色的接口。
拓展思路
易于增加具体的工厂,难以增加新的零件。