design patterns Flashcards

1
Q

What distinguishes a “good” factory implementation from a “bad” one?

A

Good factories use interfaces/abstraction for type safety extensibility and eliminate conditional logic in object creation

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

When is Factory Method pattern inappropriate?

A

When products lack common interfaces creation logic is simple or you have few product variations

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How does Abstract Factory differ from Factory Method?

A

Abstract Factory creates product families (e.g. furniture sets) while Factory Method creates single products (e.g. chairs)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What constructor limitations does Builder solve?

A

Solves telescoping constructor anti-pattern by enabling stepwise object creation with optional parameters and method chaining

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

When should Prototype pattern be preferred over new instantiation?

A

When object creation is resource-heavy (DB calls/complex math) or needing numerous similar objects (game entity cloning)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What critical decision must Prototype’s clone() method address?

A

Whether to perform deep copy (duplicate nested objects) or shallow copy (share references) based on object composition

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What three elements define a Singleton implementation?

A

Private constructor static instance storage and static getInstance() method controlling instantiation

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Why is Singleton considered problematic in modern development?

A

Introduces global state that hinders testing creates hidden dependencies and causes concurrency issues

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What are Singleton alternatives for shared resource management?

A

Dependency injection containers factory patterns or module exports (ES6 modules for single instances)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What key advantage does Builder provide over direct object construction?

A

Enforces valid object states through progressive construction and provides fluent API for complex object assembly

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What JavaScript language feature helps implement Prototype pattern?

A

Object.create() method and prototype inheritance naturally support cloning through prototype chain delegation

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How does Builder pattern ensure object validity?

A

By enforcing required parameters early and validating before build() call preventing partially constructed objects

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

When would Abstract Factory become impractical?

A

When product families frequently change or new product types need to be added regularly to existing families

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What’s a common real-world Factory Method example in JavaScript?

A

Document.createElement() - the DOM spec uses factory methods to create elements while hiding concrete classes

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How to implement thread-safe Singleton in Node.js?

A

Use module caching (Node modules are singletons by default) rather than class-based implementation

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What’s the main drawback of Prototype pattern in JavaScript?

A

Deep cloning complex objects can be performance-intensive and may require explicit handling of circular references

17
Q

How does Builder pattern handle mandatory vs optional parameters?

A

Mandatory parameters are set in constructor/builder initialization optional through chained methods

18
Q

What’s a code smell indicating need for Abstract Factory?

A

Multiple factory methods that are always used together to create related objects

19
Q

Why prefer Factory Method over direct constructor calls?

A

Enables subclassing for object creation and supports dependency injection frameworks

20
Q

How to test Singleton-dependent code effectively?

A

Use proxy patterns or dependency injection to replace singleton instance during testing