抽象工厂模式(Abstract Factory)
#### Abstract Factory?
抽象工厂的工作是将“抽象零件”组装为“抽象产品”。在template method模式中由与顶级父类提供算法实现,但在Factory Method 中也是定义好framework中具体的Product与Factory,并且由与子类去实现具体的工厂 产品实现,但是抽象工厂中,多个多个构件的之间组装的例子,并且使用抽象类满足隔离的实现。基本上你可以看作是工厂方法的扩展。
– 假设现在我们要在示例程序中增加新的具体工厂,那么需要做的就是编写Factory、Link、Tray、Page这4个类的子类,并实现它们定义的抽象方法。也就是说将factory包中的抽象部分全部具体化即可。便于添加新的职责
相关设计模式:
◆Builder模式:Abstract Factory模式通过调用抽象产品的接口(API)来组装抽象产品,生成具有复杂结构的实例。
Builder模式则是分阶段地制作复杂实例。·Factory Method模式 有时Abstract Factory 模式中零件和产品的生成会使用到Factory Method模式。
·Composite模式 有时Abstract Factory模式在制作产品时会使用Composite模式。
·Singleton模式 有时Abstract Factory模式中的具体工厂会使用Singleton模式。
#### 具体职责
abstrack:
factory |Factory| 表示抽象工厂的类(制作Link、Tray、Page)
factory |Item| 方便统一处理Link和Tray的类
factory |Link 抽象零件:表示HTML的链接的类
factory |Tray|抽象零件:表示含有Link和Tray的类
factory |Page抽象零件:表示HTML页面的类
|无名|Main测试程序行为的类
ConcreteClass:
|listfactory |ListFactory |表示具体工厂的类(制作ListLink、ListTray、ListPage)
|1istfactory |ListLink 具体零件:表示HTML的链接的类
|1istfactory |ListTray |具体零件:表示含有Link和Tray的类
|1istfactory |ListPage |具体零件:表示HTML页面的类
#### UML
文件结构:
#### Code
AbstractFactory:
1 public abstract class AbstractFactory { 2 3 4 5 /** 6 7 * 根据类名进行装载 8 9 * @param name 10 11 * @return 12 13 */ 14 15 public static AbstractFactory getFactory(String name){ 16 17 18 19 AbstractFactory factory=null; 20 21 try { 22 23 24 25 Class<?> aClass = Class.forName(name); 26 27 factory= ((AbstractFactory) aClass.newInstance()); 28 29 }catch (ClassNotFoundException e){ 30 31 e.printStackTrace(); 32 33 }catch (Exception e){ 34 35 e.printStackTrace(); 36 37 } 38 39 return factory; 40 41 } 42 43 44 45 /** 46 47 * 相应需要抽象的实例 48 49 * @param caption 50 51 * @return 52 53 */ 54 55 public abstract Tray createTray(String caption); 56 57 58 59 public abstract Page createPage(String title, String author); 60 61 62 63 public abstract Link createLink(String caption,String url); 64 65 66 67 } 68 69 70 71
顶级Item:
“`
public abstract class Item {
protected String caption;
public Item(String caption) {
this.caption = caption;
}
/**
* 返回html的内容
*/
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 Page {
protected String title;
protected String author;
protected Vector<Item> pages=new Vector<>();
public Page(String title, String author) {
this.title = title;
this.author = author;
}
public void add(Item item){
pages.add(item);
}
public void ouput(){
String filename=title+”.html”;
try {
Writer writer=new PrintWriter(filename);
writer.write(this.makeHtml());
writer.close();
System.out.println(“编写HTMl文档完成“);
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 输出html 文档
* @return
*/
protected abstract String makeHtml();
}
public abstract class Tray extends Item{
protected Vector<Item> tray=new Vector<>();
public Tray(String caption) {
super(caption);
}
public void add(Item item){
tray.add(item);
}
}
具体工厂的实现:
public class ListFactory extends AbstractFactory {
@Override
public Tray createTray(String caption) {
return new ListTray(caption);
}
@Override
public Page createPage(String title, String author) {
return new ListPage(title,author);
}
@Override
public Link createLink(String caption, String url) {
return new ListLink(caption,url);
}
}
具体的子构件的实现:
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 ListPage extends Page {
public ListPage(String title, String author) {
super(title, author);
}
@Override
protected String makeHtml() {
StringBuffer buffer=new StringBuffer();
buffer.append(“<html><head><title>”+title+”</title></head>\n”);
buffer.append(“<body>\n”);
buffer.append(“<h1>”+title+”</h1>\n”);
buffer.append(“<ul>\n”);
Iterator<Item> iterator = pages.iterator();
while(iterator.hasNext()){
Item item = iterator.next();
buffer.append(item.makeHtml());
}
buffer.append(“</ul>\n”);
buffer.append(“<hr><address>”+author+”</address>\n”);
buffer.append(“</body></html\n”);
return buffer.toString();
}
}
public class ListTray extends Tray {
public ListTray(String caption) {
super(caption);
}
@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>\n”);
buffer.append(“</li>\n”);
return buffer.toString();
}
}
测试:
public class MainTest {
public static void main(String[] args) {
/**
* 根据不同的args入参选择不同的工厂实现
*/
args=new String[]{“abstractfactory.base.factory.absfactory.concretefactory.ListFactory”};
// 实现2
args=new String[]{“abstractfactory.base.factory.absfactory.concreatefactory2.TableFactory”};
if(args.length!=1){
System.out.println(“example : java Main package.ConcreateFactory1”);
System.out.println(“example : java Main package.ConcreateFactory2”);
System.exit(0);
}
AbstractFactory factory = AbstractFactory.getFactory(args[0]);
Link google = factory.createLink(“谷歌“, “www.google.cn”);
Link baidu = factory.createLink(“百度“, “www.baidu.com”);
Link souhu = factory.createLink(“搜狐“, “http://www.sohu.com/”);
Link sina = factory.createLink(“新浪“, “https://www.sina.com.cn/”);
Link gome = factory.createLink(“国美“, “https://www.gome.com.c”);
Link wangyi = factory.createLink(“网易“, “https://www.163.com/”);
Tray reserchtray = factory.createTray(“搜索引擎“);
reserchtray.add(google);
reserchtray.add(baidu);
Tray chinaItCompany = factory.createTray(“china IT Company”);
chinaItCompany.add(souhu);
chinaItCompany.add(sina);
chinaItCompany.add(gome);
chinaItCompany.add(wangyi);
Page page = factory.createPage(“网址导航“, “by dgw”);
page.add(reserchtray);
page.add(chinaItCompany);
page.ouput();
}
}