AbstractFactory

<textarea cols="50" rows="15" name="code" class="java">import java.io.*; import java.util.*; abstract class Item { protected String caption; public Item(String caption){ this.caption = caption; } public abstract String makeHTML(); }; /*&Ograve;&Ocirc;&sup3;&eacute;&Iuml;&oacute;&middot;&frac12;&Ecirc;&frac12;&plusmn;&iacute;&Ecirc;&frac34;HTML&sup3;&not;&Aacute;&acute;&frac12;&Oacute;&micro;&Auml;&Agrave;&agrave;*/ abstract class Link extends Item { protected String url; public Link(String caption,String url){ super(caption); this.url = url; } }; /*&plusmn;&iacute;&Ecirc;&frac34;&Ecirc;&Otilde;&frac14;&macr;&Ograve;&raquo;&cedil;&ouml;&Ograve;&Ocirc;&Eacute;&Iuml;&micro;&Auml;Link&raquo;&ograve;Tray&micro;&Auml;&Agrave;&agrave;*/ 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&plusmn;&iacute;&Ecirc;&frac34;&Otilde;&ucirc;&cedil;&ouml;HTML&Iacute;&oslash;&Ograve;&sup3;*/ /*Link &ordm;&Iacute; Tray&Ecirc;&Ccedil;&sup3;&eacute;&Iuml;&oacute;&micro;&Auml;&iexcl;&deg;&Aacute;&atilde;&frac14;&thorn;&iexcl;&plusmn;&pound;&not;Page&Agrave;&agrave;&Ecirc;&Ccedil;&sup3;&eacute;&Iuml;&oacute;&micro;&Auml;&iexcl;&deg;&sup2;&uacute;&AElig;&middot;&iexcl;&plusmn;*/ /*Filed author&Iacute;&oslash;&Ograve;&sup3;&times;&divide;&Otilde;&szlig;&pound;&not;Title&Iacute;&oslash;&Ograve;&sup3;&plusmn;&ecirc;&Igrave;&acirc;*/ 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 + &quot;.html&quot;; Writer writer = new FileWriter(filename); writer.write(this.makeHTML()); writer.close(); System.out.println(&quot;&Ograve;&Ntilde;&sup2;&uacute;&Eacute;&uacute;&quot;+filename+&quot;.&quot;); } catch (IOException e) { e.printStackTrace(); } } public abstract String makeHTML(); }; /*&Ouml;&AElig;&Ocirc;&igrave;&Aacute;&atilde;&frac14;&thorn;&raquo;&ograve;&sup2;&uacute;&AElig;&middot;*/ 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(&quot;&Otilde;&Ograve;&sup2;&raquo;&micro;&frac12;&Agrave;&agrave;&quot;+classname+&quot;.&quot;); }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(&quot;Usage: Java Main class.name.of.ConcreateFactory&quot;); System.out.println(&quot;Example 1: Java Main ListFacory&quot;); System.out.println(&quot;Example 1: Java Main TableFactory&quot;); System.exit(0); } Factory factory = Factory.getFactory(args[0]); Link asahi = factory.createLink(&quot;&sup3;&macr;&Egrave;&Otilde;&ETH;&Acirc;&Icirc;&Aring;&quot;,&quot;http://www.asahi.com/&quot;); Link yomiuri = factory.createLink(&quot;&para;&Aacute;&Acirc;&ocirc;&ETH;&Acirc;&Icirc;&Aring;&quot;,&quot;http://www.yomuri.co.jp/&quot;); Link us_yahoo = factory.createLink(&quot;Yahoo!&quot;,&quot;http://www.yahoo.com/&quot;); Link jp_yahoo = factory.createLink(&quot;Yahoo!Japan&quot;,&quot;http://www.yahoo.co.jp/&quot;); Link excite = factory.createLink(&quot;Excite&quot;,&quot;http://www.excite.com/&quot;); Link google = factory.createLink(&quot;Google&quot;,&quot;http://www.google.com&quot;); Tray traynews = factory.createTray(&quot;&ETH;&Acirc;&Icirc;&Aring;&quot;); traynews.add(asahi); traynews.add(yomiuri); Tray trayyahoo = factory.createTray(&quot;Yahoo!&quot;); trayyahoo.add(us_yahoo); trayyahoo.add(jp_yahoo); Tray traysearch = factory.createTray(&quot;&Euml;&Ntilde;&Euml;&divide;&Ograve;&yacute;&Ccedil;&aelig;&quot;); traysearch.add(trayyahoo); traysearch.add(excite); traysearch.add(google); Page page = factory.createPage(&quot;LinkPage&quot;,&quot;&frac12;&aacute;&sup3;&Eacute;&ordm;&Atilde;&quot;); 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 &quot;&lt;li&gt;&lt;a href=&quot;/&quot; mce_href=&quot;/&quot;&quot;&quot;+url+&quot;/&quot;&gt;&quot;+caption+&quot;&lt;/a&gt;&lt;/li&gt;&quot;; } }; class ListTray extends Tray { public ListTray(String caption){ super(caption); } public String makeHTML(){ StringBuffer buffer = new StringBuffer(); buffer.append(&quot;&lt;li&gt;/n&quot;); buffer.append(caption+&quot;/n&quot;); buffer.append(&quot;&lt;ul&gt;/n&quot;); Iterator it = tray.iterator(); while(it.hasNext()){ Item item = (Item)it.next(); buffer.append(item.makeHTML()); } buffer.append(&quot;&lt;/ul&gt;/n&quot;); buffer.append(&quot;&lt;/li&gt;/n&quot;); 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(&quot;&lt;html&gt;&lt;head&gt;&lt;title&gt;&quot;+title+&quot;&lt;/title&gt;&lt;/head&gt;/n&quot;); buffer.append(&quot;&lt;h1&gt;&quot;+title+&quot;&lt;/h1&gt;/n&quot;); buffer.append(&quot;&lt;ul&gt;/n&quot;); Iterator it = content.iterator(); while(it.hasNext()){ Item item = (Item)it.next(); buffer.append(item.makeHTML()); } buffer.append(&quot;&lt;/ul&gt;/n&quot;); buffer.append(&quot;&lt;hr&gt;&lt;address&gt;&quot;+author+&quot;&lt;address&gt;&quot;); buffer.append(&quot;&lt;/body&gt;&lt;/html&gt;/n&quot;); return buffer.toString(); } };</textarea>

你可能感兴趣的:(AbstractFactory)