圖解設計模式-Abstract Factory模式
阿新 • • 發佈:2018-09-04
leg trace content pen itl app ioe 代碼 ces 抽象工廠的工作是將“抽象零件”組裝成“抽象產品”。
我們不關心零件的具體實現,而是只關心接口API。我們僅適用該接口API將零件組裝成為產品。
角色:
AbstractProduct抽象產品:負責定義AbstractFactory角色所生成的抽象零件和產品的接口。在示例中,友Link、Tray、Page類扮演。
AbstractFactory抽象工廠:負責定義用於生成抽象產品的API接口,在示例中,有Factory扮演。
Client委托者:該角色僅會調用AbstractProduct角色和AbstractFactory角色的接口API來進行工作。在示例中由Main類扮演。
ConcreteProduct具體產品:負責實現AbstractProduct角色的API接口。在示例中由listfactory包下的ListLink、ListTray、ListPage類與tablefactory包下的TableLink、TableTray、TablePage類來扮演。
ConcreteFactory具體工廠:負責實現AbstractFactory角色的API接口,在示例中,由listfactory包下的ListFactory與tablefactory包下的TableFactory類扮演。
優點:
易於增加具體的工廠:只需要Factory、Link、Tray、Page四個類即可。
代碼:
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(); } }重點: Factory抽象類,通過className獲得具體的類實例,可以靈活的獲得繼承Factory的實現類。 同時Factory抽象類提供了創建Link、Tray、Page的抽象方法。 ListFactory為實現了Factory的具體工廠,在ListFactory中定義了具體的使用哪些零件。 Page抽象類定義了out輸出方法,在out輸出方法中調用了抽象方法makeHTML方法,後續實現了Page的類可以根據需要自己定義個性化的makeHTML方法。
圖解設計模式-Abstract Factory模式