Skip to main content

Difference between Singleton and Factory Patterns.

Mid JavaScript
Quick Answer Singleton: only one instance ever created รขโ‚ฌโ€ a shared DB connection, a config object. class Config { static instance; static getInstance() { if (!this.instance) this.instance = new Config(); return this.instance; } }. Factory: creates objects without exposing constructor logic รขโ‚ฌโ€ function createUser(role) returns different user objects based on role. Factory is more flexible; Singleton is for shared global state.

Answer

Singleton: Only one instance allowed.

Factory: Creates objects without exposing creation logic.

S
SugharaIQ Editorial Team Verified Answer

This answer has been peer-reviewed by industry experts holding senior engineering roles to ensure technical accuracy and relevance for modern interview standards.

Want to bookmark, take notes, or join discussions?

Sign in to access all features and personalize your learning experience.

Sign In Create Account

Source: SugharaIQ

Ready to level up? Start Practice