我是靠谱客的博主 天真星月,最近开发中收集的这篇文章主要介绍【JAVA进阶系列】JAVA 设计模式 -- 抽象工厂模式(Abstract Factory)【JAVA进阶系列】JAVA 设计模式 -- 抽象工厂模式(Abstract Factory),觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

【JAVA进阶系列】JAVA 设计模式 -- 抽象工厂模式(Abstract Factory)

【1.1】抽象工厂模式简介

抽象工厂者模式的类结构图

AbstractProduct(抽象产品),AbstractProduct角色定义AbstractFactory角色所生成的抽象产品接口;
AbstractFactory(抽象工厂),AbstractFactory角色定义用于生成抽象产品的接口;
Client(委托者),Client角色仅仅调用AbstractFactory角色和AbstractProduct角色的接口进行工作,并不知道产品、工厂的具体实现;
ConcreteProduct(具体产品),ConcreteProduct角色负责实现AbstractProduct角色的接口;
ConcreteFactory(具体工厂),ConcreteFactory角色负责实现AbstractFactory角色的接口;

【1.2】抽象工厂模式的 JAVA 代码实现

代码类结构图示

代码源码示例

Main 类

import AbstractFactory.Sample.factory.*;

public class Main {
    public static void main(String[] args) {

//        Factory factory = Factory.getFactory("AbstractFactory.Sample.listfactory.ListFactory");
        Factory factory = Factory.getFactory("AbstractFactory.Sample.tablefactory.TableFactory");

        Link people = factory.createLink("人民日报", "http://www.people.com.cn/");
        Link gmw = factory.createLink("光明日报", "http://www.gmw.cn/");

        Link us_yahoo = factory.createLink("Yahoo!", "http://www.yahoo.com/");
        Link jp_yahoo = factory.createLink("Yahoo!Japan", "http://www.yahoo.co.jp/");
        Link excite = factory.createLink("Excite", "http://www.excite.com/");
        Link google = factory.createLink("Google", "http://www.google.com/");

        Tray traynews = factory.createTray("日报");
        traynews.add(people);
        traynews.add(gmw);

        Tray trayyahoo = factory.createTray("Yahoo!");
        trayyahoo.add(us_yahoo);
        trayyahoo.add(jp_yahoo);

        Tray traysearch = factory.createTray("检索引擎");
        traysearch.add(trayyahoo);
        traysearch.add(excite);
        traysearch.add(google);

        Page page = factory.createPage("LinkPage", "杨文轩");
        page.add(traynews);
        page.add(traysearch);
        page.output();
    }
}

factory.Factory 类

public 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("没有找到 " + classname + "类。");
        } 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);
}

factory.Item 类

public abstract class Item {
    protected String caption;
    public Item(String caption) {
        this.caption = caption;
    }
    public abstract String makeHTML();
}

factory.Link 类

public abstract class Link extends Item {
    protected String url;
    public Link(String caption, String url) {
        super(caption);
        this.url = url;
    }
}

factory.Page 类

import java.io.*;
import java.util.ArrayList;

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() {
        try {
            String filename = title + ".html";
            Writer writer = new FileWriter(filename);
            writer.write(this.makeHTML());
            writer.close();
            System.out.println(filename + " 编写完成。");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public abstract String makeHTML();
}

factory.Tray 类

import java.util.ArrayList;

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);
    }
}

listfactory.ListFactory 类

import AbstractFactory.Sample.factory.*;

public 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);
    }
}

listfactory.ListLink 类

import AbstractFactory.Sample.factory.*;

public class ListLink extends Link {
    public ListLink(String caption, String url) {
        super(caption, url);
    }
    public String makeHTML() {
        return "  <li><a href="" + url + "">" + caption + "</a></li>n";
    }
}

listfactory.ListPage 类

import AbstractFactory.Sample.factory.*;
import java.util.Iterator;

public class ListPage extends Page {
    public ListPage(String title, String author) {
        super(title, author);
    }
    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();
    }
}

listfactory.ListTray 类

import AbstractFactory.Sample.factory.*;
import java.util.Iterator;

public class ListTray extends Tray {
    public ListTray(String caption) {
        super(caption);
    }
    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();
    }
}

tablefactory.TableFactory 类

import AbstractFactory.Sample.factory.*;

public class TableFactory extends Factory {
    public Link createLink(String caption, String url) {
        return new TableLink(caption, url);
    }
    public Tray createTray(String caption) {
        return new TableTray(caption);
    }
    public Page createPage(String title, String author) {
        return new TablePage(title, author);
    }
}

tablefactory.TableLink 类

import AbstractFactory.Sample.factory.*;

public class TableLink extends Link {
    public TableLink(String caption, String url) {
        super(caption, url);
    }
    public String makeHTML() {
        return "<td><a href="" + url + "">" + caption + "</a></td>n";
    }
}

tablefactory.TablePage 类

import AbstractFactory.Sample.factory.*;
import java.util.Iterator;

public class TablePage extends Page {
    public TablePage(String title, String author) {
        super(title, author);
    }
    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("<table width="80%" border="3">n");
        Iterator it = content.iterator();
        while (it.hasNext()) {
            Item item = (Item)it.next();
            buffer.append("<tr>" + item.makeHTML() + "</tr>");
        }
        buffer.append("</table>n");
        buffer.append("<hr><address>" + author + "</address>");
        buffer.append("</body></html>n");
        return buffer.toString();
    }
}

tablefactory.TableTray 类

import AbstractFactory.Sample.factory.*;
import java.util.Iterator;

public class TableTray extends Tray {
    public TableTray(String caption) {
        super(caption);                     // 使用super(...)表达式  
    }
    public String makeHTML() {
        StringBuffer buffer = new StringBuffer();
        buffer.append("<td>");
        buffer.append("<table width="100%" border="1"><tr>");
        buffer.append("<td bgcolor="#cccccc" align="center" colspan=""+ tray.size() + ""><b>" + caption + "</b></td>");
        buffer.append("</tr>n");
        buffer.append("<tr>n");
        Iterator it = tray.iterator();
        while (it.hasNext()) {
            Item item = (Item)it.next();
            buffer.append(item.makeHTML());
        }
        buffer.append("</tr></table>");
        buffer.append("</td>");
        return buffer.toString();
    }
}

参考致谢

本博客为博主学习笔记,同时参考了网上众博主的博文以及相关专业书籍,在此表示感谢,本文若存在不足之处,请批评指正。

【1】图解设计模式

最后

以上就是天真星月为你收集整理的【JAVA进阶系列】JAVA 设计模式 -- 抽象工厂模式(Abstract Factory)【JAVA进阶系列】JAVA 设计模式 -- 抽象工厂模式(Abstract Factory)的全部内容,希望文章能够帮你解决【JAVA进阶系列】JAVA 设计模式 -- 抽象工厂模式(Abstract Factory)【JAVA进阶系列】JAVA 设计模式 -- 抽象工厂模式(Abstract Factory)所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(64)

评论列表共有 0 条评论

立即
投稿
返回
顶部