Agent SkillsPatternsDev/skills › factory-pattern

factory-pattern

GitHub

介绍工厂模式,用于动态创建对象而避免使用 new 关键字,集中化创建逻辑。适用于需根据环境或配置生成共享属性对象,对比类构造函数以平衡灵活性与内存效率。

javascript/factory-pattern/SKILL.md PatternsDev/skills

Trigger Scenarios

需要动态创建多个具有相同结构的对象 对象创建逻辑依赖于环境或特定配置

Install

npx skills add PatternsDev/skills --skill factory-pattern -g -y
More Options

Non-standard path

npx skills add https://github.com/PatternsDev/skills/tree/main/javascript/factory-pattern -g -y

Use without installing

npx skills use PatternsDev/skills@factory-pattern

指定 Agent (Claude Code)

npx skills add PatternsDev/skills --skill factory-pattern -a claude-code -g -y

安装 repo 全部 skill

npx skills add PatternsDev/skills --all -g -y

预览 repo 内 skill

npx skills add PatternsDev/skills --list

SKILL.md

Frontmatter
{
    "name": "factory-pattern",
    "paths": [
        "**\/*.js",
        "**\/*.ts"
    ],
    "license": "MIT",
    "metadata": {
        "author": "patterns.dev",
        "version": "1.1"
    },
    "description": "Teaches the factory pattern for flexible object creation. Use when you need to create objects dynamically without using the new keyword or when object creation logic should be centralized.",
    "related_skills": [
        "module-pattern",
        "singleton-pattern"
    ]
}

Factory Pattern

With the factory pattern we can use factory functions in order to create new objects. A function is a factory function when it returns a new object without the use of the new keyword!

Say that we need many users for our application. We can create new users with a firstName, lastName, and email property. The factory function adds a fullName property to the newly created object as well, which returns the firstName and the lastName.

When to Use

  • Use this when you need to create multiple objects that share the same properties
  • This is helpful when object creation depends on a certain environment or configuration

When NOT to Use

  • For simple objects where a plain object literal suffices — a factory adds unnecessary indirection
  • When class constructors are the established convention in your project and the team expects new
  • When there's no conditional logic or configuration driving object creation

Instructions

  • Use factory functions to return custom objects based on current environment or user-specific configuration
  • Prefer ES6 arrow functions for concise factory function definitions
  • Consider using class constructors instead when memory efficiency matters, as instances share prototype methods

Details

const createUser = ({ firstName, lastName, email }) => ({
  firstName,
  lastName,
  email,
  fullName() {
    return `${this.firstName} ${this.lastName}`;
  },
});

Perfect! We can now easily create multiple users by invoking the createUser function.

The factory pattern can be useful if we're creating relatively complex and configurable objects. It could happen that the values of the keys and values are dependent on a certain environment or configuration. With the factory pattern, we can easily create new objects that contain the custom keys and values!

const createObjectFromArray = ([key, value]) => ({
  [key]: value,
});

createObjectFromArray(["name", "John"]); // { name: "John" }

Pros

The factory pattern is useful when we have to create multiple smaller objects that share the same properties. A factory function can easily return a custom object depending on the current environment, or user-specific configuration.

Cons

In JavaScript, the factory pattern isn't much more than a function that returns an object without using the new keyword. ES6 arrow functions allow us to create small factory functions that implicitly return an object each time.

However, in many cases it may be more memory efficient to create new instances instead of new objects each time.

class User {
  constructor(firstName, lastName, email) {
    this.firstName = firstName;
    this.lastName = lastName;
    this.email = email;
  }

  fullName() {
    return `${this.firstName} ${this.lastName}`;
  }
}

const user1 = new User({
  firstName: "John",
  lastName: "Doe",
  email: "john@doe.com",
});

const user2 = new User({
  firstName: "Jane",
  lastName: "Doe",
  email: "jane@doe.com",
});

Source

References

Version History

  • 48bf58a Current 2026-07-24 16:33

Same Skill Collection

javascript/bundle-splitting/SKILL.md
javascript/command-pattern/SKILL.md
javascript/compression/SKILL.md
javascript/dynamic-import/SKILL.md
javascript/flyweight-pattern/SKILL.md
javascript/import-on-interaction/SKILL.md
javascript/import-on-visibility/SKILL.md
javascript/islands-architecture/SKILL.md
javascript/js-performance-patterns/SKILL.md
javascript/loading-sequence/SKILL.md
javascript/mediator-pattern/SKILL.md
javascript/mixin-pattern/SKILL.md
javascript/module-pattern/SKILL.md
javascript/observer-pattern/SKILL.md
javascript/prefetch/SKILL.md
javascript/preload/SKILL.md
javascript/prototype-pattern/SKILL.md
javascript/provider-pattern/SKILL.md
javascript/proxy-pattern/SKILL.md
javascript/prpl/SKILL.md
javascript/route-based/SKILL.md
javascript/singleton-pattern/SKILL.md
javascript/static-import/SKILL.md
javascript/third-party/SKILL.md
javascript/tree-shaking/SKILL.md
javascript/view-transitions/SKILL.md
javascript/virtual-lists/SKILL.md
javascript/vite-bundle-optimization/SKILL.md
react/ai-ui-patterns/SKILL.md
react/client-side-rendering/SKILL.md
react/compound-pattern/SKILL.md
react/hoc-pattern/SKILL.md
react/hooks-pattern/SKILL.md
react/incremental-static-rendering/SKILL.md
react/presentational-container-pattern/SKILL.md
react/progressive-hydration/SKILL.md
react/react-2026/SKILL.md
react/react-composition-2026/SKILL.md
react/react-data-fetching/SKILL.md
react/react-render-optimization/SKILL.md
react/react-selective-hydration/SKILL.md
react/react-server-components/SKILL.md
react/render-props-pattern/SKILL.md
react/server-side-rendering/SKILL.md
react/static-rendering/SKILL.md
react/streaming-ssr/SKILL.md
vue/async-components/SKILL.md
vue/components/SKILL.md
vue/composables/SKILL.md

Metadata

Files
0
Version
48bf58a
Hash
74f9e1eb
Indexed
2026-07-24 16:33

inicio - Wiki
Copyright © 2011-2026 iteam. Current version is 2.155.2. UTC+08:00, 2026-08-20 06:36
浙ICP备14020137号-1 $mapa de visitantes$