标签:file protected name content author instance 输出 抽象工厂 new
public abstract class Item { protected String caption; public Item(String caption) { this.caption = caption; } public abstract String makeHTML(); }
public abstract class Link extends Item{ protected String url; public Link(String caption,String url) { super(caption); this.url = url; } }
public abstract class Tray extends Item { protected List trayList = new ArrayList(); public Tray(String caption) { super(caption); } public void add(Item item) { trayList.add(item); } }
public abstract class Page{ protected String author; protected String title; protected List content = new ArrayList(); public Page(String author,String title) { this.title = title; this.author = author; } public void add(Item item){ content.add(item); } public void out() { String filename = "D:\\"+title+".html"; try { PrintWriter printWriter = new PrintWriter(new FileWriter(filename)); printWriter.println(this.makeHTML()); printWriter.close(); } catch (IOException e) { e.printStackTrace(); } } public abstract String makeHTML(); }
public abstract class Factory { public static Factory getFactory(String classname) { Factory factory = null; try { factory = (Factory) Class.forName(classname).newInstance(); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InstantiationException e) { e.printStackTrace(); } return factory; } public abstract Link createLink(String caption,String url); public abstract Tray createTray(String caption); public abstract Page createPage(String author,String title); }
public class ListLink extends Link { public ListLink(String caption, String url) { super(caption, url); } @Override public String makeHTML() { return "<li><a href=\""+url+"\">"+caption+"</a><li>\n"; } } public class ListTray extends Tray { public ListTray(String caption) { super(caption); } @Override public String makeHTML() { StringBuilder builder = new StringBuilder(); builder.append("\n"); Iterator iterator = trayList.iterator(); while(iterator.hasNext()){ Item item = (Item) iterator.next(); builder.append(item.makeHTML()); } return builder.toString(); } }
public class ListPage extends Page { public ListPage(String author, String title) { super(author, title); } @Override public String makeHTML() { StringBuilder builder = new StringBuilder(); builder.append(title); builder.append(author); Iterator iterator = content.iterator(); while(iterator.hasNext()) { Item item = (Item) iterator.next(); builder.append(item.makeHTML()); } return builder.toString(); } }
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 author, String title) { return new ListPage(author,title); } }
public class Main { public static void main(String[] args) { Factory factory = ListFactory.getFactory("listfactory.ListFactory"); Link link = factory.createLink("111","222"); Tray tray = factory.createTray("33"); Page page = factory.createPage("作者","标题"); page.add(link); page.add(tray); page.out(); } }
标签:file protected name content author instance 输出 抽象工厂 new
原文地址:https://www.cnblogs.com/use-D/p/9581938.html