Java設計模式之抽象工廠模式詳解

一、什麼是抽象工廠模式

為創建一組相關或相互依賴的對象提供一個接口,而且無需指定他們的具體類,這稱之為抽象工廠模式(Abstract Factory)。我們並不關心零件的具體實現,而是隻關心接口(API)。我們僅使用該接口(API)將零件組裝稱為產品。

二、示例程序

 

1、抽象的零件:Item類

package com.as.module.abstractfactory;
 
/**
 * 抽象的零件
 * @author Andy
 * @date 2021/4/29 23:16
 */
public abstract class Item {
    protected String caption;
 
    public Item(String caption) {
        this.caption = caption;
    }
 
    public abstract String makeHTML();
}

2、抽象的零件:Link類

package com.as.module.abstractfactory;
 
/**
 * TODO
 *
 * @author Andy
 * @date 2021/4/29 23:18
 */
public abstract class Link extends Item {
    protected String url;
    public Link(String caption,String url) {
        super(caption);
        this.url = url;
    }
 
 
 
}

3、抽象的零件:Tray類

package com.as.module.abstractfactory;
 
import java.util.ArrayList;
 
/**
 * TODO
 *
 * @author Andy
 * @date 2021/4/29 23:20
 */
public abstract class Tray extends Item{
    protected ArrayList tray = new ArrayList();
 
    public Tray(String caption) {
        super(caption);
    }
    public void add(Item item){
        tray.add(item);
    }
}

4、抽象的產品:Page類

package com.as.module.abstractfactory;
 
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.ArrayList;
 
/**
 * 抽象的產品
 * @author Andy
 * @date 2021/4/29 23:22
 */
public abstract class Page {
    protected String title;
    protected String author;
    protected ArrayList content = new ArrayList();
 
    public Page(String title, String author) {
        this.title = title;
        this.author = author;
    }
 
    public void add(Item item){
        content.add(item);
    }
 
    public void output(){
        String filename = title+".html";
        try {
            Writer writer = new FileWriter(filename);
            writer.write(this.makeHTML());
            writer.close();
            System.out.println(filename+"編寫完成");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public abstract String makeHTML();
 
}

5、抽象的工廠:Factory類

package com.as.module.abstractfactory;
 
/**
 * 抽象的工廠
 * @author Andy
 * @date 2021/4/29 23:31
 */
public abstract class Factory {
    public static Factory getFactory(String className){
        Factory factory = null;
        try {
            factory = (Factory)Class.forName(className).newInstance();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } 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);
 
}

6、具體的工廠:ListFactory類

package com.as.module.abstractfactory;
 
/**
 * 具體的工廠
 * @author Andy
 * @date 2021/4/29 23:36
 */
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 title, String author) {
        return new ListPage(title,author);
    }
}

7、具體的零件:ListLink類

package com.as.module.abstractfactory;
 
/**
 * 具體的零件
 * @author Andy
 * @date 2021/4/29 23:37
 */
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";
    }
 
 
}

8、具體的零件:ListTray類

package com.as.module.abstractfactory;
 
import java.util.Iterator;
 
/**
 * TODO
 *
 * @author Andy
 * @date 2021/4/29 23:43
 */
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 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();
    }
}

9、具體的零件:ListPage類

package com.as.module.abstractfactory;
 
import java.util.Iterator;
 
/**
 * 具體的產品
 * @author Andy
 * @date 2021/5/4 10:42
 */
public class ListPage extends Page {
 
    public ListPage(String title, String author) {
        super(title, author);
    }
 
    @Override
    public 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 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();
    }
}

10、抽象工廠方法測試用例

package com.as.module.abstractfactory;
 
import java.util.List;
 
/**
 *
 * @author Andy
 * @date 2021/5/4 10:54
 */
public class TestAbstractFactory {
 
    public static void main(String[] args) {
        Factory factory = Factory.getFactory("com.as.module.abstractfactory.ListFactory");
        Link people = factory.createLink("人民日報","http://www.people.com.cn/");
        Link gmw = factory.createLink("光明日報","http://www.gmw.cn/");
 
        Link baidu = factory.createLink("Baidu","http://www.baidu.com/");
        Link google = factory.createLink("Google","http://www.google.com/");
 
        Tray traynews = factory.createTray("日報");
        traynews.add(people);
        traynews.add(gmw);
 
        Tray traysearch = factory.createTray("搜索引擎");
        traysearch.add(baidu);
        traysearch.add(google);
 
        Page page = factory.createPage("LinkPage","Andy");
        page.add(traynews);
        page.add(traysearch);
        page.output();
 
    }
}

運行結果:

三、UML

登場角色:

1、AbstractProduct(抽象產品)

AbstractProduct角色負責定義AbstractPFactory角色所生成的抽象零件和產品的接口。在示例程序中,由Link類,Tray類和Page類扮演此角色

2、AbstractFactory(抽象工廠)

AbstractFactory角色負責定義用於生成抽象產品的接口。在示例程序中,由Factory

3、Client(委托者)

Client角色僅會調用AbstractProduct角色和AbstractFactory角色來進行工作,對於具體的零件,產品,工廠一無所知。示例程序中,由具體的TestAbstractFactory 扮演此角色

4、ConcreteProduct(具體產品)

ConcreteProduct角色負責實現抽象產品角色的接口,示例程序中,由ListLink,ListTray,ListPage類扮演此角色

5、ConcreteFactory(具體工廠)

到此這篇關於Java設計模式之抽象工廠模式詳解的文章就介紹到這瞭,更多相關Java抽象工廠模式內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!

推薦閱讀: