package abstractfactory.factory;
/**
* 抽象工厂
*
* @author Administrator
* @version [版本号, 2012-10-19]
* @see [相关类/方法]
* @since [产品/模块版本]
*/
public abstract class Factory
{
/**
* 根据具体工厂的类名获取对应的实例
* @param classname
* @return
* @see [类、类#方法、类#成员]
*/
public static Factory getFactory(String classname)
{
Factory factory = null;
try
{
factory = (Factory)Class.forName(classname).newInstance();
}
catch (ClassNotFoundException e)
{
e.printStackTrace();
}
catch (InstantiationException e)
{
e.printStackTrace();
}
catch (IllegalAccessException 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);
}
package abstractfactory.factory;
/**
* Link和Tray的父类
*
* @author Administrator
* @version [版本号, 2012-10-19]
* @see [相关类/方法]
* @since [产品/模块版本]
*/
public abstract class Item
{
/**
* 项目的标题
*/
protected String caption;
public Item(String caption)
{
this.caption = caption;
}
/**
* 返回值为HTML字符串
* @return
* @see [类、类#方法、类#成员]
*/
public abstract String makeHTML();
}
package abstractfactory.factory;
/**
*
* 以抽象方式表示HTML超链接的类
*
* @author Administrator
* @version [版本号, 2012-10-19]
* @see [相关类/方法]
* @since [产品/模块版本]
*/
public abstract class Link extends Item
{
/**
* 存储链接网站的url
*/
protected String url;
public Link(String caption, String url)
{
super(caption);
this.url = url;
}
}
package abstractfactory.factory;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.Vector;
/**
* 以抽象的方式表示整个HTML网页的类。如果说Link和Tray是抽象的零件,Page就是抽象的产品
*
* @author Administrator
* @version [版本号, 2012-10-19]
* @see [相关类/方法]
* @since [产品/模块版本]
*/
public abstract class Page
{
/**
* 网页的标题
*/
protected String title;
/**
* 网页作者
*/
protected String author;
protected Vector<Item> content = new Vector<Item>();
public Page(String title, String author)
{
this.title = title;
this.author = author;
}
/**
* 在网页上新增其他item(即Link或Tray)
* @param item
* @see [类、类#方法、类#成员]
*/
public void add(Item item)
{
content.add(item);
}
/**
* 模板方法,调用自身的makeHTML抽象方法生成HTML文件
* @see [类、类#方法、类#成员]
*/
public void output()
{
String filename = title + ".html";
try
{
Writer writer = new FileWriter(filename);
writer.write(this.makeHTML());
writer.close();
System.out.println("已产生" + filename + "。");
}
catch (IOException e)
{
e.printStackTrace();
}
}
/**
* 返回HTML字符串的方法,需要由子类去实现
* @return
* @see [类、类#方法、类#成员]
*/
public abstract String makeHTML();
}
package abstractfactory.factory;
import java.util.Vector;
/**
* 表示收集一个以上的Link或Tray的类(tray="浅盘",请把它想成是将一个个标有项目符号的项目放在盘子上)
*
* @author Administrator
* @version [版本号, 2012-10-19]
* @see [相关类/方法]
* @since [产品/模块版本]
*/
public abstract class Tray extends Item
{
protected Vector<Item> tray = new Vector<Item>();
public Tray(String caption)
{
super(caption);
}
public void add(Item item)
{
tray.add(item);
}
}
package abstractfactory.listfactory;
import abstractfactory.factory.Factory;
import abstractfactory.factory.Link;
import abstractfactory.factory.Page;
import abstractfactory.factory.Tray;
/**
* 具体的工厂
*
* @author Administrator
* @version [版本号, 2012-10-19]
* @see [相关类/方法]
* @since [产品/模块版本]
*/
public class ListFactory extends Factory
{
/** {@inheritDoc} */
@Override
public Link createLink(String caption, String url)
{
return new ListLink(caption, url);
}
/** {@inheritDoc} */
@Override
public Page createPage(String title, String author)
{
return new ListPage(title, author);
}
/** {@inheritDoc} */
@Override
public Tray createTray(String caption)
{
return new ListTray(caption);
}
}
/*
* 文 件 名: ListLink.java
* 版 权: Huawei Technologies Co., Ltd. Copyright YYYY-YYYY, All rights reserved
* 描 述: <描述>
* 修 改 人: Administrator
* 修改时间: 2012-10-19
* 跟踪单号: <跟踪单号>
* 修改单号: <修改单号>
* 修改内容: <修改内容>
*/
package abstractfactory.listfactory;
import abstractfactory.factory.Link;
/**
* 具体产品,用标签<li>和<a>产生片段
*
* @author Administrator
* @version [版本号, 2012-10-19]
* @see [相关类/方法]
* @since [产品/模块版本]
*/
public class ListLink extends Link
{
public ListLink(String caption, String url)
{
super(caption, url);
}
/** {@inheritDoc} */
@Override
public String makeHTML()
{
String html = "<li><a href=\"" + url + "\">" + caption + "</a></li>";
return html;
}
}
package abstractfactory.listfactory;
import java.util.Iterator;
import abstractfactory.factory.Item;
import abstractfactory.factory.Page;
/**
* 利用字段的内容产生网页
*
* @author Administrator
* @version [版本号, 2012-10-19]
* @see [相关类/方法]
* @since [产品/模块版本]
*/
public class ListPage extends Page
{
public ListPage(String title, String author)
{
super(title, author);
}
/** {@inheritDoc} */
@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>");
buffer.append("<ul\n>");
Iterator<Item> iterator = content.iterator();
while (iterator.hasNext())
{
Item item = iterator.next();
buffer.append(item.makeHTML());
}
buffer.append("</ul>");
buffer.append("<hr><address>" + author + "</address>");
buffer.append("</body></html>");
return buffer.toString();
}
}
package abstractfactory.listfactory;
import java.util.Iterator;
import abstractfactory.factory.Item;
import abstractfactory.factory.Tray;
/**
* 先利用 <li>输出项目符号, 然后再以<ul>和<li>一个个输出item
*
* @author Administrator
* @version [版本号, 2012-10-19]
* @see [相关类/方法]
* @since [产品/模块版本]
*/
public class ListTray extends Tray
{
public ListTray(String caption)
{
super(caption);
}
/** {@inheritDoc} */
@Override
public String makeHTML()
{
StringBuffer buffer = new StringBuffer();
buffer.append("<li>\n");
buffer.append(caption + "\n");
buffer.append("<ul>+\n");
Iterator<Item> iterator = tray.iterator();
while (iterator.hasNext())
{
Item item = iterator.next();
buffer.append(item.makeHTML());
}
buffer.append("</ul>");
buffer.append("</li>");
return buffer.toString();
}
}
package abstractfactory;
import abstractfactory.factory.Factory;
import abstractfactory.factory.Link;
import abstractfactory.factory.Page;
import abstractfactory.factory.Tray;
/**
* 测试抽象工厂模式的测试类
*
* @author Administrator
* @version [版本号, 2012-10-19]
* @see [相关类/方法]
* @since [产品/模块版本]
*/
public class TestAbstractFactory
{
/** 主函数
* @param args
* @see [类、类#方法、类#成员]
*/
public static void main(String[] args)
{
Factory factory = Factory.getFactory("abstractfactory.listfactory.ListFactory");
Link asahi = factory.createLink("朝日新闻", "http://asahi.com/");
Link youmiuri = factory.createLink("读卖新闻", "http://www.youmiuri.co.jp");
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(asahi);
traynews.add(youmiuri);
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", "asgsadg");
page.add(traynews);
page.add(traysearch);
page.output();
}
}