Skip to main content
skill CX Codex

factory-pattern — Claude Code skill: factory functions without new

Teaches the agent to build objects with factory functions. Useful when creation logic depends on config or environment.

Install into Claude Code · Skills CLI (Vercel) All methods and platforms ↓
npx skills add PatternsDev/skills --skill factory-pattern -a claude-code
Open on GitHub

What factory-pattern does

The skill covers the factory pattern in JavaScript and TypeScript: a function returns a new object without the new keyword. Inside are ES6 arrow function examples, a section on when the pattern fits, and a separate list of cases where it only adds indirection. The agent picks it up on **/*.js and **/*.ts files.

Beyond the basic createUser with firstName, lastName, email and a fullName method, there is a factory that builds an object from a key-value pair. The text also warns that class instances are more memory efficient, since methods live on the prototype instead of being copied into every object. Worth checking how many instances your code creates.

Install it if your project already avoids new or builds objects from environment settings. If the team expects constructors, the skill will argue with that convention. Setup runs through npx skills add.

What it helps with

  • create objects without the new keyword
  • build an object from a key-value pair
  • centralize object creation logic in one place
  • shape object fields around environment configuration
  • decide between a factory and a class with prototype methods

Example prompts for the agent

  • Rewrite user creation as a factory function without new
  • Write a factory that turns a key and value array into an object
  • Check where a factory is overkill in this file and an object literal would do

How to install factory-pattern

1Skills CLI (Vercel)

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

2Manually: put the files in place

~/.claude/skills/factory-pattern/SKILL.md

Copy the whole skill folder; for one project use .claude/skills/factory-pattern/

Step by step, with commands for each platform: How to install skills in Claude Code: five ways

Questions about factory-pattern

How do I install factory-pattern in Claude?

Run npx skills add PatternsDev/skills --skill factory-pattern -a claude-code. The skill lands in ~/.claude/skills/factory-pattern/SKILL.md.

What does the factory-pattern skill do?

It explains the factory pattern and guides the agent toward factory functions that return an object without new. The text includes examples and a list of cases where the pattern is not worth it.

When should I skip the factory pattern?

Plain objects are enough for simple cases, and a factory adds indirection. If your project uses classes and the team expects new, stay with constructors.

Source text

SKILL.md · MIT
---
name: factory-pattern
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.
paths:
  - "**/*.js"
  - "**/*.ts"
license: MIT
metadata:
  author: patterns.dev
  version: "1.1"
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

```js
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!

```js
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.

```js
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: "[email protected]",
});

const user2 = new User({
  firstName: "Jane",
  lastName: "Doe",
  email: "[email protected]",
});
```

## Source

- [patterns.dev/vanilla/factory-pattern](https://patterns.dev/vanilla/factory-pattern)

### References

- [JavaScript Factory Functions with ES6+](https://medium.com/javascript-scene/javascript-factory-functions-with-es6-4d224591a8b1) - Eric Elliott

© patterns.dev · MIT · PatternsDev/skills

Similar skills

All skills for Programming languages →

From the same set44

vite-bundle-optimization A skill with bundle and build optimization patterns for Vite + React. Useful when builds drag or chunks come out larger than expected. CX Codex +1 667installs command-pattern Teaches AI agents how to implement the Command pattern in JavaScript and TypeScript. Useful for queuing tasks, scheduling actions, and building undo/redo flows. CX Codex +1 645installs singleton-pattern Teaches the Singleton pattern in JavaScript and TypeScript for managing a single shared instance. Useful when coordinating global configuration or shared application resources. CX Codex +1 637installs observer-pattern A skill that teaches your agent the observer pattern for decoupled publish/subscribe in JavaScript and TypeScript. CX Codex +1 638installs module-pattern Teaches AI agents to organize JavaScript and TypeScript into encapsulated ES2015 modules with explicit exports and private state. CX Codex +1 631installs islands-architecture A skill that walks through the islands architecture pattern and partial hydration. Useful when most of a page is static and only small parts need interactivity. CX Codex +1 624installs provider-pattern Teaches your agent to move shared data into React Context and hand it out through a Provider when props crawl through every layer. CX Codex +1 620installs proxy-pattern A skill that teaches the JavaScript Proxy pattern: intercepting property reads and writes, adding validation and logging. Useful when your agent edits that kind of code. CX Codex +1 612installs

Project sponsors

Companies that keep this analytics open
Gold sponsor

Impressions, clicks and code copies — open statistics. We keep the numbers public for the same reason NeBlask itself is public. Become a sponsor