Skip to content
Back to skills

Js Factory Pattern

ASecurity

> Create objects via a factory function without exposing instantiation logic to callers

  • 20 stars
  • 0 votes
  • 0 copies
  • 2 views
  • Added September 11, 2026
toolsjavascriptjava

Security analysis

A100/100

Pro scans all 2 files and shows the line behind each finding

Scanned September 11, 2026

npx -y skills add Intense-Visions/harness-engineering --skill js-factory-pattern --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Js Factory Pattern?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Js Factory Pattern
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/intense-visions-js-factory-pattern/badge)](https://www.skillsdirectory.com/skills/intense-visions-js-factory-pattern)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
# JS Factory Pattern

> Create objects via a factory function without exposing instantiation logic to callers

## When to Use

- Object creation logic is complex and should be encapsulated
- The exact type of object to create is determined at runtime
- You want to return different implementations based on input without exposing `new` to callers

## Instructions

1. Write a factory function (or static class method) that accepts configuration and returns the correct object.
2. The caller never uses `new` directly — they call the factory.
3. Validate inputs inside the factory before creating the object.
4. Return a consistent interface regardless of which concrete type was created.

```javascript
function createUser(type, name) {
  const roles = {
    admin: { permissions: ['read', 'write', 'delete'] },
    editor: { permissions: ['read', 'write'] },
    viewer: { permissions: ['read'] },
  };

  if (!roles[type]) throw new Error(`Unknown user type: ${type}`);

  return {
    name,
    type,
    ...roles[type],
    greet() {
      return `Hi, I'm ${name} (${type})`;
    },
  };
}

const admin = createUser('admin', 'Alice');
console.log(admin.permissions); // ['read', 'write', 'delete']
```

## Details

The Factory pattern is one of the most common patterns in JavaScript. Unlike `new ClassName()`, a factory function can return different types, apply caching, run validation, or perform async initialization.

**Trade-offs:**

- Callers cannot use `instanceof` to check the type (unless the factory returns a class instance)
- Can become a large switch/if-else block if not maintained

**When NOT to use:**

- When all instances are always the same type — just use `new` directly
- For very simple objects with no creation logic — plain object literals are sufficient

## Source

https://patterns.dev/javascript/factory-pattern

## Process

1. Read the instructions and examples in this document.
2. Apply the patterns to your implementation, adapting to your specific context.
3. Verify your implementation against the details and edge cases listed above.

## Harness Integration

- **Type:** knowledge — this skill is a reference document, not a procedural workflow.
- **No tools or state** — consumed as context by other skills and agents.

## Success Criteria

- The patterns described in this document are applied correctly in the implementation.
- Edge cases and anti-patterns listed in this document are avoided.

Files in this skill

  • SKILL.md2.4 KB
  • skill.yaml685 B

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

Comments

Loading comments…