Design Patterns Flashcards

(10 cards)

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
4
Q

Types of Design Patterns?

A
  1. Creational Pattern
  2. Structural Pattern
  3. Behavioral Pattern
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Creational Pattern

A

These deal with object creation mechanisms:

Singleton, Factory, Abstract, Builder, Prototype

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

Structural Pattern

A

These are concerned with object composition, i.e., how classes and objects are composed to form larger structures.

Adapter, Bridge, Composite, Decorator, Facade, Flyweight, Proxy

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

Behavioral pattern

A

These focus on communication between objects, what goes on between objects and how they operate together:

Observer, Strategy, Command, Chain of Responsibility, State, Template Method, Iterator, Mediator, Visitor, Memento, Interpreter

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

Factory Pattern

A

Category: Creational

Intent: Define an interface for creating an object, but let subclasses decide which class to instantiate.

Use Case: Object creation with conditional logic or varying types (e.g., notifications, shapes).

Key Benefit: Decouples client code from specific classes.

Example:
INotification n = NotificationFactory.CreateNotification(“Email”);
n.Notify(“Hello”);

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

Abstract Factory Pattern

A

Category: Creational

Intent: Provide an interface for creating families of related objects without specifying their concrete classes.

Use Case: UI toolkits across platforms (e.g., Windows vs. Mac buttons/checkboxes).

Key Benefit: Ensures compatibility between related products; enforces consistency.

Example:
IGUIFactory factory = new MacFactory();
factory.CreateButton().Render();
factory.CreateCheckbox().Render();

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