建造者模式 logo

建造者模式

Organization
microwind
建造者模式

分步骤构建复杂对象。在对象有很多可选参数或构建步骤复杂时使用。

Overview

Publishermicrowind
Repositoryai-skills
Skill name建造者模式
Stars
93
Forks
25
Bundled files
3
LicenseMIT
Links
  • Markdown instructions

    A SKILL.md file the model loads on demand, so it only costs tokens when a request actually matches.

  • Works with any LLM

    AI skills are plain Markdown, not provider-specific code, so this works with GPT, Claude, Gemini, Grok, or a local model.

  • 3 bundled files

    Scripts, templates, and references the model can read while it works. Files are read-only and never executed.

  • Open source

    Published by microwind on GitHub. Read the source before you install it.

Installation

Install the 建造者模式 AI skill in TypingMind to use it with any LLM, or drop it into another agent that reads SKILL.md.

1

Install in TypingMind

TypingMind installs a skill straight from its GitHub folder — it reads SKILL.md, bundles the resource files, and stores the result locally.

  1. Open the app and go to Plugins → Skills.
  2. Choose "Install from GitHub".
  3. Paste the skill folder URL below and confirm.
    https://github.com/microwind/ai-skills/tree/main/design-patterns/builder-pattern
  4. Enable the skill in any chat where you want it available.
Plugins → Skills → Add skill → From GitHub URL, then paste the folder URL and press Continue.
2

Install in another agent

Any agent that reads the Agent Skills format can use this skill — copy the folder into that agent's skills directory.

Claude Code — .claude/skills
git clone --depth 1 https://github.com/microwind/ai-skills.git /tmp/ai-skills
mkdir -p .claude/skills
cp -r /tmp/ai-skills/design-patterns/builder-pattern .claude/skills/microwind-24
Restart Claude Code after copying so it picks up the new skill.

Use it in TypingMind

Enable 建造者模式 in any TypingMind chat and the model takes it from there. Its name and description sit in the system prompt, and the moment a request matches, the model loads the full instructions itself — you never invoke it by hand, and it costs no tokens until it is actually used.

The model loads 建造者模式 on its own as soon as a request matches it.

Works with any AI model

AI skills are plain Markdown instructions rather than provider-specific code, so 建造者模式 is not tied to the model it was written for. Install it once in TypingMind and use it with GPT-5, Claude, Gemini, Grok, DeepSeek, Mistral, Llama, or a local model you run yourself — all on your own API keys.

  • Loaded only when it is needed

    The system prompt carries just the name and description. The instructions are fetched on the first matching request, so an idle skill costs nothing.

  • Switch models mid-chat

    Because the skill is instructions rather than code, changing model does not break it — the next model reads the same SKILL.md.

Skill instructions

This is the SKILL.md content the model loads. Read it before installing — a skill is instructions your model will follow.

建造者模式 (Builder Pattern)

概述

建造者模式将复杂对象的构建与其表示分离,使得可以按步骤构建对象。这个模式特别适用于对象有多个可选属性或构建过程复杂的场景。

核心原则:

  • 🏗️ 分离构建和表示: 构建者负责如何构建,客户端负责如何使用
  • 📝 链式调用: 支持流畅的函数式编程风格
  • ✅ 参数验证集中: 在 build 方法中验证所有参数
  • 🔁 灵活性高: 支持多种构建顺序和组合

何时使用

完美适用场景

场景说明优先级
HTTP 请求构建URL、方法、头、参数、超时等可选属性⭐⭐⭐
SQL 查询构建SELECT、WHERE、ORDER BY、LIMIT 等灵活组合⭐⭐⭐
对象配置大量可选配置项的对象初始化⭐⭐⭐
DTO/VO 构建从多个数据源组装 DTO⭐⭐⭐
复杂表单表单验证、多步骤填写⭐⭐
数据库连接池配置众多可选参数的配置对象⭐⭐

触发信号

✅ 以下信号表明应该使用 Builder:

  • "这个类的构造函数有太多参数"
  • "大部分参数都是可选的"
  • "需要多种方式配置同一对象"
  • "构建过程复杂,涉及多个步骤"
  • "参数间有依赖关系,需要验证"

❌ 以下情况不应该使用:

  • 对象只有 1-2 个参数
  • 所有参数都是必需的
  • 参数组合并无实际意义
  • 性能关键路径(build() 有开销)

建造者模式的优缺点

优点 ✅

  1. 参数管理清晰

    • 避免构造函数参数过多
    • 代码易读性大幅提高
    • 无需记忆参数顺序
  2. 支持链式调用

    • 代码风格优雅流畅
    • 支持函数式编程
    • 可读性更高
  3. 灵活的构建过程

    • 支持多种参数组合
    • 构建顺序灵活
    • 支持默认值
  4. 参数验证集中

    • 所有验证在 build() 方法
    • 确保对象始终有效
    • 减少运行时错误
  5. 易于扩展

    • 添加新参数无需改变客户端
    • 向后兼容性好
    • 容易添加新的构建方式

缺点 ❌

  1. 代码量增加

    • 需要编写 Builder 类
    • 代码行数增加
    • 简单对象可能过度设计
  2. 性能开销

    • build() 方法有开销
    • 创建额外对象
    • 不适用于性能关键路径
  3. 复杂度增加

    • 初学者容易混淆
    • 需要理解嵌套类或独立类
    • 调试相对复杂

建造者模式的 5 种实现方法

1. 经典 Builder - 静态内部类

特点: 标准实现方式,安全且易维护

java
public class HttpRequest {
    private String url;
    private String method;
    private Map<String, String> headers;
    private String body;
    private int timeout;
    private boolean followRedirects;
    
    // 私有构造函数,强制使用 Builder
    private HttpRequest(Builder builder) {
        this.url = builder.url;
        this.method = builder.method;
        this.headers = builder.headers;
        this.body = builder.body;
        this.timeout = builder.timeout;
        this.followRedirects = builder.followRedirects;
    }
    
    // Builder 内部类
    public static class Builder {
        private String url;
        private String method = "GET";
        private Map<String, String> headers = new HashMap<>();
        private String body = "";
        private int timeout = 5000;
        private boolean followRedirects = true;
        
        public Builder(String url) {
            this.url = url;
        }
        
        public Builder method(String method) {
            this.method = method;
            return this;
        }
        
        public Builder header(String name, String value) {
            this.headers.put(name, value);
            return this;
        }
        
        public Builder headers(Map<String, String> headers) {
            this.headers.putAll(headers);
            return this;
        }
        
        public Builder body(String body) {
            this.body = body;
            return this;
        }
        
        public Builder timeout(int timeout) {
            this.timeout = timeout;
            return this;
        }
        
        public Builder followRedirects(boolean follow) {
            this.followRedirects = follow;
            return this;
        }
        
        // 构建并验证
        public HttpRequest build() {
            if (url == null || url.isEmpty()) {
                throw new IllegalArgumentException("URL is required");
            }
            if (timeout <= 0) {
                throw new IllegalArgumentException("Timeout must be positive");
            }
            return new HttpRequest(this);
        }
    }
    
    @Override
    public String toString() {
        return String.format("HttpRequest{%s %s, timeout=%d}", method, url, timeout);
    }
}

// 使用
HttpRequest request = new HttpRequest.Builder("https://api.example.com/users")
    .method("POST")
    .header("Content-Type", "application/json")
    .header("Authorization", "Bearer token")
    .body("{\"name\":\"John\"}")
    .timeout(10000)
    .build();

2. 流式 Builder - 链式编程

特点: 支持更流畅的链式调用

java
public class SQLBuilder {
    private List<String> selectClauses = new ArrayList<>();
    private String fromClause;
    private List<String> whereClauses = new ArrayList<>();
    private List<String> orderByClauses = new ArrayList<>();
    private int limit = -1;
    
    public SQLBuilder select(String... columns) {
        selectClauses.addAll(Arrays.asList(columns));
        return this;
    }
    
    public SQLBuilder from(String table) {
        this.fromClause = table;
        return this;
    }
    
    public SQLBuilder where(String condition) {
        whereClauses.add(condition);
        return this;
    }
    
    public SQLBuilder orderBy(String column, String direction) {
        orderByClauses.add(column + " " + direction);
        return this;
    }
    
    public SQLBuilder limit(int limit) {
        this.limit = limit;
        return this;
    }
    
    public String build() {
        if (fromClause == null) {
            throw new IllegalArgumentException("FROM clause is required");
        }
        
        StringBuilder sql = new StringBuilder();
        
        // SELECT
        sql.append("SELECT ");
        if (selectClauses.isEmpty()) {
            sql.append("*");
        } else {
            sql.append(String.join(", ", selectClauses));
        }
        
        // FROM
        sql.append(" FROM ").append(fromClause);
        
        // WHERE
        if (!whereClauses.isEmpty()) {
            sql.append(" WHERE ").append(String.join(" AND ", whereClauses));
        }
        
        // ORDER BY
        if (!orderByClauses.isEmpty()) {
            sql.append(" ORDER BY ").append(String.join(", ", orderByClauses));
        }
        
        // LIMIT
        if (limit > 0) {
            sql.append(" LIMIT ").append(limit);
        }
        
        return sql.toString();
    }
}

// 使用
String sql = new SQLBuilder()
    .select("id", "name", "email")
    .from("users")
    .where("age > 18")
    .where("status = 'active'")
    .orderBy("created_at", "DESC")
    .limit(10)
    .build();

System.out.println(sql);
// SELECT id, name, email FROM users WHERE age > 18 AND status = 'active' ORDER BY created_at DESC LIMIT 10

3. 参数对象 Builder

特点: 用单独的参数对象,适合参数众多的情况

java
// 参数对象
public class QueryParams {
    public int page = 1;
    public int pageSize = 20;
    public String sortBy = "id";
    public String sortOrder = "ASC";
    public List<String> filters = new ArrayList<>();
    
    public static class Builder {
        private QueryParams params = new QueryParams();
        
        public Builder page(int page) {
            params.page = Math.max(1, page);
            return this;
        }
        
        public Builder pageSize(int pageSize) {
            params.pageSize = Math.min(100, pageSize);
            return this;
        }
        
        public Builder sort(String sortBy, String order) {
            params.sortBy = sortBy;
            params.sortOrder = order.toUpperCase();
            return this;
        }
        
        public Builder filter(String condition) {
            params.filters.add(condition);
            return this;
        }
        
        public QueryParams build() {
            return params;
        }
    }
}

// 使用
QueryParams params = new QueryParams.Builder()
    .page(2)
    .pageSize(50)
    .sort("name", "asc")
    .filter("status=active")
    .filter("type=premium")
    .build();

4. 继承 Builder - 支持多态

特点: Builder 本身也支持继承,适合复杂继承体系

java
public abstract class Pizza {
    public enum Topping { HAM, MUSHROOM, ONION, PEPPER, SAUSAGE }
    
    final Set<Topping> toppings;
    
    abstract static class Builder<T extends Builder<T>> {
        EnumSet<Topping> toppings = EnumSet.noneOf(Topping.class);
        
        public T addTopping(Topping topping) {
            toppings.add(Objects.requireNonNull(topping));
            return self();
        }
        
        abstract Pizza build();
        
        protected abstract T self();
    }
    
    Pizza(Builder<?> builder) {
        toppings = builder.toppings.clone();
    }
}

public class NyPizza extends Pizza {
    public enum Size { SMALL, MEDIUM, LARGE }
    
    public final Size size;
    
    public static class Builder extends Pizza.Builder<Builder> {
        private final Size size;
        
        public Builder(Size size) {
            this.size = Objects.requireNonNull(size);
        }
        
        @Override
        public NyPizza build() {
            return new NyPizza(this);
        }
        
        @Override
        protected Builder self() {
            return this;
        }
    }
    
    private NyPizza(Builder builder) {
        super(builder);
        size = builder.size;
    }
}

// 使用
NyPizza pizza = new NyPizza.Builder(NyPizza.Size.SMALL)
    .addTopping(Pizza.Topping.SAUSAGE)
    .addTopping(Pizza.Topping.ONION)
    .build();

5. Lombok @Builder - 自动生成

特点: 使用注解自动生成 Builder 代码

java
@Builder
@Data
public class Document {
    private String title;
    private String content;
    @Builder.Default
    private String author = "Anonymous";
    @Builder.Default
    private LocalDateTime createdAt = LocalDateTime.now();
    private boolean published;
}

// 自动生成的 Builder 代码(由 Lombok 生成)
// 使用时:
Document doc = Document.builder()
    .title("Document Title")
    .content("Content here")
    .author("John Doe")
    .published(true)
    .build();

常见问题与完整解决方案

问题 1: 参数验证与可选参数

症状: 不清楚哪些参数是必需的,哪些是可选的

java
// ✅ 解决方案:明确标记必需参数
public static class Builder {
    // 必需参数在构造函数中
    private String url;
    
    public Builder(String url) {  // 强制提供
        this.url = Objects.requireNonNull(url, "URL is required");
    }
    
    // 可选参数有默认值
    private int timeout = 5000;
    private boolean followRedirects = true;
    private Map<String, String> headers = new HashMap<>();
    
    public Builder timeout(int timeout) {
        if (timeout <= 0) {
            throw new IllegalArgumentException("Timeout must be positive");
        }
        this.timeout = timeout;
        return this;
    }
    
    public HttpRequest build() {
        // 最终验证所有参数
        validateAll();
        return new HttpRequest(this);
    }
    
    private void validateAll() {
        // 检查所有必需参数都有值
        // 检查所有值的有效性
    }
}

问题 2: 不可变对象的构建

症状: 构建后的对象需要保证不可变性

java
// ✅ 解决方案:返回不可变副本
public class ImmutableConfig {
    private final Map<String, String> properties;
    
    private ImmutableConfig(Builder builder) {
        // 使用 Collections.unmodifiableMap 保证不可变
        this.properties = Collections.unmodifiableMap(
            new HashMap<>(builder.properties)
        );
    }
    
    public String get(String key) {
        return properties.get(key);
    }
    
    public static class Builder {
        private Map<String, String> properties = new HashMap<>();
        
        public Builder set(String key, String value) {
            properties.put(key, value);
            return this;
        }
        
        public ImmutableConfig build() {
            return new ImmutableConfig(this);
        }
    }
}

// 使用
ImmutableConfig config = new ImmutableConfig.Builder()
    .set("app.name", "MyApp")
    .set("debug", "true")
    .build();

// config.properties.put(...);  // 编译错误!不可修改

问题 3: 性能 - 创建过多中间对象

症状: 每次 build() 都创建新对象,性能不佳

java
// ✅ 解决方案:对象池或缓存
public class CachedBuilder {
    private static final Map<BuildConfig, CachedObject> cache = 
        Collections.synchronizedMap(new WeakHashMap<>());
    
    public CachedObject build() {
        BuildConfig config = createConfig();
        
        // 检查缓存中是否已存在相同配置
        return cache.computeIfAbsent(config, cfg -> 
            createObject(cfg)
        );
    }
    
    private CachedObject createObject(BuildConfig config) {
        // 创建新对象
        return new CachedObject(...);
    }
}

问题 4: 嵌套对象的构建

症状: 需要构建含有嵌套对象的复杂对象

java
// ✅ 解决方案:嵌套 Builder
public class User {
    private String name;
    private Address address;
    
    public static class Builder {
        private String name;
        private Address.Builder addressBuilder = new Address.Builder();
        
        public Builder name(String name) {
            this.name = name;
            return this;
        }
        
        // 支持地址构建
        public Builder city(String city) {
            addressBuilder.city(city);
            return this;
        }
        
        public Builder zipCode(String zipCode) {
            addressBuilder.zipCode(zipCode);
            return this;
        }
        
        public User build() {
            User user = new User();
            user.name = this.name;
            user.address = addressBuilder.build();
            return user;
        }
    }
}

// 使用
User user = new User.Builder()
    .name("John")
    .city("New York")
    .zipCode("10001")
    .build();

Builder vs 其他模式对比

方面Builder工厂方法构造函数
参数数量很多(10+)中等(3-5)少(<3)
可选参数支持很好不支持不支持
灵活性很高中等低
代码量多中等少
学习曲线中等简单简单

最佳实践

  1. ✅ 参数验证集中 - 所有验证在 build() 方法
  2. ✅ 提供明确的必需参数 - 在构造函数中
  3. ✅ 使用流畅接口 - 支持链式调用
  4. ✅ 文档化默认值 - 说明每个参数的默认值
  5. ✅ 考虑线程安全 - 如果 Builder 在多线程中使用
  6. ✅ 返回不可变对象 - build() 的结果应该是不可变的

何时避免使用

  • ❌ 对象只有 1-2 个参数
  • ❌ 所有参数都是必需的
  • ❌ 性能关键路径(实时系统)
  • ❌ 极小型项目(过度设计)

Bundled files

The model reads these on demand while the skill is loaded. They are exposed as readable files and are never executed.

Frequently asked questions

What does the 建造者模式 AI skill do?

分步骤构建复杂对象。在对象有很多可选参数或构建步骤复杂时使用。

Why use 建造者模式 on TypingMind?

Because you install it once and use it with any model. 建造者模式 is plain Markdown rather than provider-specific code, so the same skill runs on GPT-5, Claude, Gemini, Grok, or a local model — and you can switch model mid-chat without it breaking. TypingMind runs on your own API keys, so you pay providers directly instead of a per-seat subscription, and your skills and chats stay in your own storage.

How do I install 建造者模式 in TypingMind?

Open Plugins → Skills → Install from GitHub in TypingMind and paste https://github.com/microwind/ai-skills/tree/main/design-patterns/builder-pattern. TypingMind reads its SKILL.md and bundles its files and installs it as a skill you can enable per chat.

Which AI models can use 建造者模式?

Any model you connect in TypingMind. AI skills are plain Markdown instructions rather than provider-specific code, so GPT, Claude, Gemini, Grok, and local models can all load this skill when a request matches it.

How many AI models can I use with 建造者模式?

As many as you like. As long as a model supports skills, you can use 建造者模式 with it — GPT, Claude, Gemini, Grok, DeepSeek, Mistral, Llama and more — all on TypingMind with your own API keys.

Is the 建造者模式 AI skill free?

Yes. It is published on GitHub by microwind under the MIT license. You only pay your own AI provider for the tokens you use.

What are AI skills?

An AI skill is a reusable instruction bundle that teaches an AI model how to do one specific task. It follows the open Agent Skills format: a SKILL.md file with a name and description, plus any scripts, templates or reference files the model may need. The model reads the instructions only when your request matches the skill, so an installed skill costs nothing until it is used.

How are AI skills different from plugins or MCP servers?

A plugin or MCP server gives a model new tools to call — code that runs somewhere and returns a result. An AI skill gives the model knowledge and process instead: how to approach a task, which steps to follow, what good output looks like. Skills are plain Markdown, so they need no server, no API key and no runtime, and they work with any model.

View all

Set up your own AI workspace now

Get notified about new features and future giveaways by subscribing to our newsletter 👇