design patterns Flashcards
What distinguishes a “good” factory implementation from a “bad” one?
Good factories use interfaces/abstraction for type safety extensibility and eliminate conditional logic in object creation
When is Factory Method pattern inappropriate?
When products lack common interfaces creation logic is simple or you have few product variations
How does Abstract Factory differ from Factory Method?
Abstract Factory creates product families (e.g. furniture sets) while Factory Method creates single products (e.g. chairs)
What constructor limitations does Builder solve?
Solves telescoping constructor anti-pattern by enabling stepwise object creation with optional parameters and method chaining
When should Prototype pattern be preferred over new instantiation?
When object creation is resource-heavy (DB calls/complex math) or needing numerous similar objects (game entity cloning)
What critical decision must Prototype’s clone() method address?
Whether to perform deep copy (duplicate nested objects) or shallow copy (share references) based on object composition
What three elements define a Singleton implementation?
Private constructor static instance storage and static getInstance() method controlling instantiation
Why is Singleton considered problematic in modern development?
Introduces global state that hinders testing creates hidden dependencies and causes concurrency issues
What are Singleton alternatives for shared resource management?
Dependency injection containers factory patterns or module exports (ES6 modules for single instances)
What key advantage does Builder provide over direct object construction?
Enforces valid object states through progressive construction and provides fluent API for complex object assembly
What JavaScript language feature helps implement Prototype pattern?
Object.create() method and prototype inheritance naturally support cloning through prototype chain delegation
How does Builder pattern ensure object validity?
By enforcing required parameters early and validating before build() call preventing partially constructed objects
When would Abstract Factory become impractical?
When product families frequently change or new product types need to be added regularly to existing families
What’s a common real-world Factory Method example in JavaScript?
Document.createElement() - the DOM spec uses factory methods to create elements while hiding concrete classes
How to implement thread-safe Singleton in Node.js?
Use module caching (Node modules are singletons by default) rather than class-based implementation
What’s the main drawback of Prototype pattern in JavaScript?
Deep cloning complex objects can be performance-intensive and may require explicit handling of circular references
How does Builder pattern handle mandatory vs optional parameters?
Mandatory parameters are set in constructor/builder initialization optional through chained methods
What’s a code smell indicating need for Abstract Factory?
Multiple factory methods that are always used together to create related objects
Why prefer Factory Method over direct constructor calls?
Enables subclassing for object creation and supports dependency injection frameworks
How to test Singleton-dependent code effectively?
Use proxy patterns or dependency injection to replace singleton instance during testing