Design Patterns Flashcards

1
Q

Strategy Pattern

A

Instead of hardcoding behavior, you define different strategies (algorithms) and switch them at runtime.

Example: Normal, Sporty, Off Road Vehicles

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

Observer Pattern

A

When the state of one object (the subject) changes, all its observers (or subscribers) are notified and updated automatically.

Example: Amazon - Notify when the product is back in stock.

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

Decorator Pattern

A

It allows behavior to be added to individual objects, dynamically and transparently, without modifying the class itself.

Example - Pizza and toppings, Coffee and additional toppings.

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

Why cannot we use Static class instead of Singleton class?

A
  1. Static class is not initialized while Singleton class is initialized only if it is required.
  2. Static class cannot be passed as a parameter, which is widely used with Singleton.

By dependency injection we keep it loosely coupled.

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