import java.io.*; import java.util.*; abstract class Item { protected String caption; public Item(String caption){ this.caption = caption; } public abstract String makeHTML(); }; /*ÒÔ³éÏó·½Ê½±íʾHTML³¬Á´½ÓµÄÀà*/ abstract class Link extends Item { protected String url; public Link(String caption,String url){ super(caption); this.url = url; } }; /*±íʾÊÕ¼¯Ò»¸öÒÔÉϵÄLink»òTrayµÄÀà*/ abstract class Tray extends Item { protected Vector tray = new Vector(); public Tray(String caption){ super(caption); } public void add(Item item){ tray.add(item); } }; /*Page±íʾÕû¸öHTMLÍøÒ³*/ /*Link ºÍ TrayÊdzéÏóµÄ¡°Áã¼þ¡±£¬PageÀàÊdzéÏóµÄ¡°²úÆ·¡±*/ /*Filed authorÍøÒ³×÷Õߣ¬TitleÍøÒ³±êÌâ*/ abstract class Page { protected String title; protected String author; protected Vector content = new Vector(); 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(); }; /*ÖÆÔìÁã¼þ»ò²úÆ·*/ 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); }; class Main { public static void main(String[] args){ if(args.length != 1){ System.out.println("Usage: Java Main class.name.of.ConcreateFactory"); System.out.println("Example 1: Java Main ListFacory"); System.out.println("Example 1: Java Main TableFactory"); System.exit(0); } Factory factory = Factory.getFactory(args[0]); Link asahi = factory.createLink("³¯ÈÕÐÂÎÅ","http://www.asahi.com/"); Link yomiuri = factory.createLink("¶ÁÂôÐÂÎÅ","http://www.yomuri.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(yomiuri); 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(); } }; 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); } }; class ListLink extends Link { public ListLink(String caption,String url){ super(caption,url); } public String makeHTML(){ return "<li><a href="/" mce_href="/"""+url+"/">"+caption+"</a></li>"; } }; 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(); } }; 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("<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(); } };