蓝色天空.知识 · 实践 · 创造
我的收藏
在线代码实验室

在 OpenJDK 8 中实测本系列 33 个设计模式分类目录统计与经典参考书目索引

JAVAWindows x64、Eclipse Adoptium OpenJDK 1.8.0_492 (javac + java) 本地环境。ExploreResources.java
ExploreResources.java210 行
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

public class ExploreResources {

    static class PatternEntry {
        final String name;
        final String category;
        final String focus;

        PatternEntry(String name, String category, String focus) {
            this.name = name;
            this.category = category;
            this.focus = focus;
        }
    }

    static class ReferenceBook {
        final String title;
        final String primaryLanguage;
        final String topic;

        ReferenceBook(String title, String primaryLanguage, String topic) {
            this.title = title;
            this.primaryLanguage = primaryLanguage;
            this.topic = topic;
        }
    }

    private static List<PatternEntry> buildCatalog() {
        List<PatternEntry> list = new ArrayList<PatternEntry>();
        // Creational (5)
        list.add(new PatternEntry("Factory", "Creational", "object-creation"));
        list.add(new PatternEntry("Abstract Factory", "Creational", "family-creation"));
        list.add(new PatternEntry("Singleton", "Creational", "single-instance"));
        list.add(new PatternEntry("Builder", "Creational", "stepwise-construction"));
        list.add(new PatternEntry("Prototype", "Creational", "cloning"));
        // Structural (8: 7 GoF + Filter/Criteria covered in this course)
        list.add(new PatternEntry("Adapter", "Structural", "interface-adaptation"));
        list.add(new PatternEntry("Bridge", "Structural", "abstraction-impl-decoupling"));
        list.add(new PatternEntry("Filter", "Structural", "criteria-filtering"));
        list.add(new PatternEntry("Composite", "Structural", "tree-hierarchy"));
        list.add(new PatternEntry("Decorator", "Structural", "dynamic-extension"));
        list.add(new PatternEntry("Facade", "Structural", "subsystem-entry"));
        list.add(new PatternEntry("Flyweight", "Structural", "object-sharing"));
        list.add(new PatternEntry("Proxy", "Structural", "access-control"));
        // Behavioral (12: 11 GoF + Null Object covered in this course)
        list.add(new PatternEntry("Chain of Responsibility", "Behavioral", "request-pipeline"));
        list.add(new PatternEntry("Command", "Behavioral", "request-encapsulation"));
        list.add(new PatternEntry("Interpreter", "Behavioral", "grammar-evaluation"));
        list.add(new PatternEntry("Iterator", "Behavioral", "sequential-traversal"));
        list.add(new PatternEntry("Mediator", "Behavioral", "hub-communication"));
        list.add(new PatternEntry("Memento", "Behavioral", "state-snapshot"));
        list.add(new PatternEntry("Observer", "Behavioral", "event-subscription"));
        list.add(new PatternEntry("State", "Behavioral", "state-machine"));
        list.add(new PatternEntry("Null Object", "Behavioral", "null-safety"));
        list.add(new PatternEntry("Strategy", "Behavioral", "interchangeable-algorithm"));
        list.add(new PatternEntry("Template", "Behavioral", "algorithm-skeleton"));
        list.add(new PatternEntry("Visitor", "Behavioral", "double-dispatch-ops"));
        // J2EE (8)
        list.add(new PatternEntry("MVC", "J2EE", "presentation-separation"));
        list.add(new PatternEntry("Business Delegate", "J2EE", "tier-decoupling"));
        list.add(new PatternEntry("Composite Entity", "J2EE", "coarse-grained-entity"));
        list.add(new PatternEntry("Data Access Object", "J2EE", "persistence-abstraction"));
        list.add(new PatternEntry("Front Controller", "J2EE", "central-request-entry"));
        list.add(new PatternEntry("Intercepting Filter", "J2EE", "pre-post-filtering"));
        list.add(new PatternEntry("Service Locator", "J2EE", "service-lookup-cache"));
        list.add(new PatternEntry("Transfer Object", "J2EE", "batch-dto-transfer"));
        return list;
    }

    public static void main(String[] args) {
        List<Map<String, Object>> cases = new ArrayList<Map<String, Object>>();
        List<PatternEntry> catalog = buildCatalog();

        // Case 1: Verify complete category counts across the 33 pattern chapters
        Map<String, Integer> counts = new LinkedHashMap<String, Integer>();
        counts.put("Creational", 0);
        counts.put("Structural", 0);
        counts.put("Behavioral", 0);
        counts.put("J2EE", 0);
        for (PatternEntry e : catalog) {
            counts.put(e.category, counts.get(e.category) + 1);
        }
        Map<String, Object> c1Exp = new LinkedHashMap<String, Object>();
        c1Exp.put("Creational", 5);
        c1Exp.put("Structural", 8);
        c1Exp.put("Behavioral", 12);
        c1Exp.put("J2EE", 8);
        c1Exp.put("total", 33);

        Map<String, Object> c1Act = new LinkedHashMap<String, Object>();
        c1Act.put("Creational", counts.get("Creational"));
        c1Act.put("Structural", counts.get("Structural"));
        c1Act.put("Behavioral", counts.get("Behavioral"));
        c1Act.put("J2EE", counts.get("J2EE"));
        c1Act.put("total", catalog.size());
        addCase(cases, "pattern_catalog_category_counts",
            "核对本系列教程涵盖的四大分类(创建型 5、结构型 8、行为型 12、J2EE 8,共计 33 个模式)统计数量。",
            c1Exp, c1Act);

        // Case 2: Filter and retrieve J2EE architectural patterns
        List<String> j2eeNames = new ArrayList<String>();
        for (PatternEntry e : catalog) {
            if ("J2EE".equals(e.category)) {
                j2eeNames.add(e.name);
            }
        }
        addCase(cases, "j2ee_pattern_lookup",
            "按分类检索全部 8 个 J2EE 企业级架构模式并核对顺序。",
            Arrays.asList(
                "MVC", "Business Delegate", "Composite Entity", "Data Access Object",
                "Front Controller", "Intercepting Filter", "Service Locator", "Transfer Object"
            ),
            j2eeNames);

        // Case 3: Structured bibliography index of classic books across languages
        List<ReferenceBook> books = Arrays.asList(
            new ReferenceBook("Head First 设计模式", "Java", "面向对象入门"),
            new ReferenceBook("设计模式:可复用面向对象软件的基础", "C++/Smalltalk", "GoF 23种经典模式"),
            new ReferenceBook("图解Java多线程设计模式", "Java", "并发与多线程模式"),
            new ReferenceBook("Python设计模式 第2版", "Python", "动态语言模式实践"),
            new ReferenceBook("Learning PHP设计模式", "PHP", "Web后端模式实践")
        );
        List<String> javaBooks = new ArrayList<String>();
        for (ReferenceBook b : books) {
            if ("Java".equals(b.primaryLanguage)) {
                javaBooks.add(b.title);
            }
        }
        Map<String, Object> c3Exp = new LinkedHashMap<String, Object>();
        c3Exp.put("total_books", 5);
        c3Exp.put("java_books", Arrays.asList("Head First 设计模式", "图解Java多线程设计模式"));
        Map<String, Object> c3Act = new LinkedHashMap<String, Object>();
        c3Act.put("total_books", books.size());
        c3Act.put("java_books", javaBooks);
        addCase(cases, "classic_bibliography_index",
            "核对推荐书目索引(共 5 本跨语言经典设计模式书籍)及按 Java 语言筛选结果。",
            c3Exp, c3Act);

        Map<String, Object> root = new LinkedHashMap<String, Object>();
        root.put("runtime", "OpenJDK " + System.getProperty("java.version"));
        root.put("cases", cases);
        System.out.println(toJson(root));
    }

    private static void addCase(List<Map<String, Object>> cases, String name, String summary, Object expected, Object actual) {
        Map<String, Object> c = new LinkedHashMap<String, Object>();
        c.put("name", name);
        c.put("summary", summary);
        c.put("expected", expected);
        c.put("actual", actual);
        c.put("passed", Objects.equals(expected, actual));
        cases.add(c);
    }

    private static String toJson(Object obj) {
        if (obj == null) return "null";
        if (obj instanceof Boolean || obj instanceof Number) return obj.toString();
        if (obj instanceof String) return escapeString((String) obj);
        if (obj instanceof List<?>) {
            StringBuilder sb = new StringBuilder("[");
            List<?> list = (List<?>) obj;
            for (int i = 0; i < list.size(); i++) {
                if (i > 0) sb.append(",");
                sb.append(toJson(list.get(i)));
            }
            return sb.append("]").toString();
        }
        if (obj instanceof Map<?, ?>) {
            StringBuilder sb = new StringBuilder("{");
            boolean first = true;
            for (Map.Entry<?, ?> e : ((Map<?, ?>) obj).entrySet()) {
                if (!first) sb.append(",");
                first = false;
                sb.append(escapeString(String.valueOf(e.getKey()))).append(":").append(toJson(e.getValue()));
            }
            return sb.append("}").toString();
        }
        return escapeString(obj.toString());
    }

    private static String escapeString(String s) {
        StringBuilder sb = new StringBuilder("\"");
        for (int i = 0; i < s.length(); i++) {
            char ch = s.charAt(i);
            switch (ch) {
                case '"': sb.append("\\\""); break;
                case '\\': sb.append("\\\\"); break;
                case '\b': sb.append("\\b"); break;
                case '\f': sb.append("\\f"); break;
                case '\n': sb.append("\\n"); break;
                case '\r': sb.append("\\r"); break;
                case '\t': sb.append("\\t"); break;
                default:
                    if (ch < 0x20) {
                        sb.append(String.format("\\u%04x", (int) ch));
                    } else {
                        sb.append(ch);
                    }
            }
        }
        return sb.append("\"").toString();
    }
}
本例需在 Windows x64、Eclipse Adoptium OpenJDK 1.8.0_492 (javac + java) 本地环境。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程

运行环境:Windows x64、Eclipse Adoptium OpenJDK 1.8.0_492 (javac + java) 本地环境。

入口文件:projects/design-pattern-resources-lab/ExploreResources.java

本地运行步骤快速上手
  1. 点击上方“保存源码”保存为 ExploreResources.java,或下载右侧课程工程包。
  2. 确认本地已准备 Windows x64、Eclipse Adoptium OpenJDK 1.8.0_492 (javac + java) 本地环境。。
  3. 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载

下载课程关联工程包:设计模式

课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a2712.zip

下载课程关联工程包:设计模式

课程关联工程包(登记关联36章) · 含当前章节或实例 · mbskycom202612a2686.zip
SITE SEARCH

搜索教程、工具与资讯

热门搜索

JavaScriptJSON格式化正则表达式AI 编程AI 翻译AI导航专题标签
Esc 关闭

欢迎登录蓝色天空网

使用会员账号,继续你的学习