Factory Pattern

2024. 12. 8. 09:26·Frontend/JS Patterns

The term “Factory Pattern” in JavaScript (and in software design in general) comes from the concept of a “factory” in real life, where a factory is

  • a place where objects (products) are created.
  • In the context of software design, the Factory Pattern is a creational design pattern that provides an interface for creating objects without specifying the exact class of object that will be created.

 

즉, new 키워드 사용 없이 객체를 리턴하는 함수를 Factory 함수라 합니다.

 

 

 

장점

  • 팩토리 패턴은 동일한 속성을 공유하는 여러 개의 작은 객체를 생성해야 할 때 유용합니다.
  • 팩토리 함수는 현재 환경이나 사용자별 구성에 따라 사용자 지정 객체를 쉽게 반환할 수 있습니다.

단점

  • JavaScript에서 팩토리 패턴은 new 키워드를 사용하지 않고 객체를 반환하는 함수에 불과합니다.
  • ES6 화살표 함수를 사용하면 매번 객체를 암묵적으로 반환하는 작은 팩토리 함수를 생성할 수 있습니다.

그러나 많은 경우 매번 새 객체 대신 새 인스턴스를 생성하는 것이 메모리 효율성이 더 높을 수 있습니다.

 

새 인스턴스 사용과 메모리 효율성

1. 메모리 누수 방지를 통한 더 나은 메모리 관리

  기존 객체를 재사용할 경우, 예상치 못한 참조 보존으로 인해 객체가 메모리에 남아 있을 수 있습니다. 이런 상황은 메모리 누수를 유발할 수 있습니다. 새로운 인스턴스를 생성하면 이전 인스턴스가 더 이상 필요하지 않을 때 가비지 컬렉터에 의해 회수될 수 있어 메모리 누수를 방지합니다.

 

2. 불변성 유지, 즉 상태 격리

  새 인스턴스를 생성하면 기존 객체의 상태를 변경하지 않기 때문에 불변성을 유지할 수 있습니다. 불변 객체는 예측 가능성과 디버깅의 용이성을 높이며, 메모리 관리 측면에서도 효율적입니다. 불변성은 객체 상태 변화로 인한 예기치 않은 부작용을 방지하는 데 도움이 됩니다.

'Frontend > JS Patterns' 카테고리의 다른 글

Mixin pattern  (0) 2024.12.08
Middleware Pattern  (0) 2024.12.08
Mediator Pattern  (0) 2024.12.08
Flyweight Pattern  (0) 2024.12.08
Command Pattern  (0) 2024.11.09
'Frontend/JS Patterns' 카테고리의 다른 글
  • Middleware Pattern
  • Mediator Pattern
  • Flyweight Pattern
  • Command Pattern
JTB
JTB
웹/앱 개발 정보를 공유하고 있습니다.
  • JTB
    JTechBlog
    JTB
  • 전체
    오늘
    어제
    • All About Programming;) N
      • Computer Science
        • Terminology and Concepts
        • Network
        • Operating System
        • Database
        • Data Structure
      • Frontend
        • Javascript Essentials
        • Perfomance Optimization
        • JS Patterns
        • Next.js
        • Flutter
      • Backend
        • Node.js
      • DevOps
        • Docker & Kubernetes
      • Coding Test N
        • LeetCode N
        • Programmers
      • Tech Books & Lectures
        • Javascript_Modern JS Deep d..
        • Network_IT 엔지니어를 위한 네트워크 입문
      • Projects
        • PolyLingo_2025
        • Build Your Body_2024
        • JStargram_2021
        • Covid19 Tracker_2021
        • JPortfolio_2021
      • BootCamp_Codestates
        • TIL
        • TILookCloser
        • Pre Tech Blog
        • IM Tech Blog
        • Daily Issues and DeBugging
        • First Project
        • Final Project
        • Sprint Review
        • Good to Know
        • Socrative Review
        • HTML & CSS
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
  • 링크

    • 글쓰기
    • 관리
  • 공지사항

  • 인기 글

  • 태그

    mobile app
    이벤트
    testing
    커리어
    Data Structure
    leetcode
    database
    indie hacker
    자바스크립트 딥다이브
    structure of os
    need a database
    모던 자바스크립트 Deep Dive
    CPU scheduling algorithm
    TCP/IP
    JavaScript
    프론트엔드 성능 최적화 가이드
    딥다이브
    polylingo
    Threads and Multithreading
    VoiceJournal
    How memory manage data
    Time complexity and Space complexity
    Binary Tree BFS
    자바스크립트
    js pattern
    Shared resources
    Network
    스코프
    Operating System
    DOM
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.5
JTB
Factory Pattern
상단으로

티스토리툴바