Factory Pattern

patternsdev/skills/javascript/factory-pattern

作者 patternsdev48bf58a488cdMIT253 個星標收錄於 2026年10月8日更新於 2026年10月8日儲存庫5 個月前更新

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.

AI 產生的概覽

講解 JavaScript 工廠模式,用於在不使用 new 關鍵字的情況下建立物件。

功能
此技能說明 JavaScript 中的工廠模式,也就是工廠函式會回傳新物件而不使用 new 關鍵字。內容涵蓋適用與不適用的情境,提供 createUser 工廠函式與從陣列建立物件等程式碼範例,並列出優缺點,包括與類別建構函式相比的記憶體效率問題。
適用情境
適用於需要建立多個具有相同屬性的物件,或物件建立取決於環境或設定的情境。也適合在工廠函式與類別建構函式之間做選擇時參考。
執行需求
不需要指令碼或工具,僅為說明性內容。範例使用 JavaScript,技能鎖定 JavaScript 與 TypeScript 檔案。

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

References

來源與署名

來源:patternsdev/skills位於javascript/factory-pattern提交48bf58a

授權條款: MIT

內容歸原作者所有。SourceWeft 從公開儲存庫中收錄這些內容。

檢舉或申請下架