Design Patterns Flashcards

1
Q

What is the Factory Pattern?

A

Provides an interface for creating objects, with subclasses deciding which class to instantiate.

Example: Pizza Factory

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

What is the Singleton Pattern?

A

Ensures a class has only one instance and provides a global point of access to it.

Example: Logger Class

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

What is the Observer Pattern?

A

Defines a one-to-many dependency between objects so that when one changes state, all its dependents are notified.

Example: Newsletter Subscription

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

What is the Decorator Pattern?

A

Allows behavior to be added to individual objects without affecting the behavior of other objects from the same class.

Example: Coffee Shop (adding milk, sugar, etc.)

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

What is the Adapter Pattern?

A

Converts the interface of a class into another interface that clients expect.

Example: Charging Adapter

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

What is the Strategy Pattern?

A

Defines a family of algorithms, encapsulates each one, and makes them interchangeable.

Example: Payment Processing (Credit Card, PayPal)

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

What is the Command Pattern?

A

Turns a request into a stand-alone object that contains all information about the request.

Example: Remote Control Buttons

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

What is the Prototype Pattern?

A

Creates new objects by copying an existing object (prototype).

Example: Cloning a User Profile

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

What is the Facade Pattern?

A

Provides a unified interface to a set of interfaces in a subsystem.

Example: Home Theater System (unifying controls for TV, sound, etc.)

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

What is the Mediator Pattern?

A

Encapsulates how a set of objects interact, promoting loose coupling.

Example: Chatroom

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

What is the Chain of Responsibility Pattern?

A

Passes a request along a chain of handlers until an object handles it.

Example: Customer Support (tiered escalation)

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

What is the Builder Pattern?

A

Allows constructing complex objects step-by-step.

Example: Building a Meal (burger, drink, sides)

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

What is the Template Method Pattern?

A

Defines the program skeleton in a method, deferring some steps to subclasses.

Example: Game Algorithm (setup, play, end)

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

What is the State Pattern?

A

Allows an object to alter its behavior when its internal state changes.

Example: Vending Machine

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

What is the Proxy Pattern?

A

Provides a surrogate or placeholder for another object.

Example: Virtual Proxy for Image Loading

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

What is the Flyweight Pattern?

A

Reduces memory use by sharing as much data as possible with similar objects.

Example: Text Editor (sharing character objects)

17
Q

What is the Visitor Pattern?

A

Allows adding further operations to objects without modifying them.

Example: Tax Calculation on Various Items

18
Q

What is the Composite Pattern?

A

Composes objects into tree structures to represent part-whole hierarchies.

Example: File System (folders, files as tree structure)