Design Patterns 1 Flashcards

(46 cards)

1
Q

a reusable solution to a
common problem that occurs in a given
context within software design.

A

design pattern

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q
  • It captures best practices and proven strategies
    from experienced developers and architects.
  • Inspired by architectural patterns in mature
    engineering fields (e.g., civil, automotive).
A

design pattern

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

Why Use Design Patterns?

A

EHER

  • Encourage reuse of design structures rather than
    writing code from scratch.
  • Help in creating systems that are scalable,
    flexible, and maintainable.
  • Establish a common vocabulary for developers
    (e.g., “Use a Singleton here”).
  • Reduce design errors by reusing well-tested
    solutions.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q
  • Introduced by Gamma, Helm, Johnson, and
    Vlissides in their 1995 book “Design Patterns:
    Elements of Reusable Object-Oriented
    Software.”
  • Cataloged 23 patterns classified into three
    categories: Creational, Structural, and
    Behavioral.
A

The “Gang of Four” (GoF) Design Patterns

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

Gang of Four Design Patterns was introduced by

A

Gamma, Helm, Johnson, and Vlissides in their 1995 book “Design Patterns: Elements of Reusable Object-Oriented Software.”

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

The “Gang of Four” (GoF) Design Patterns Cataloged 23 patterns classified into three

A

CSB

Creational,
Structural, and
Behavioral.

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

Essential Elements of a Design Pattern

A

Pattern Name
Problem
Solution
Consequences

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

Unique identifier to facilitate communication (essential elements of design pattern)

A

Pattern Name

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

Describes the scenario where the pattern applies (essential elements of design pattern)

A

Problem

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

Abstract design, note code, often expressed in UML (essential elements of design pattern)

A

Solution

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

Results and trade-offs of applying the pattern (essential elements of design pattern)

A

Consequences

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

Types of Design Patterns

A

CSB

  • Creational patterns
  • Structural patterns
  • Behavioral patterns
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Focus on object creation mechanisms, trying
to create objects in a manner suitable to the
situation.

A

Creational Patterns

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

Creational Patterns Examples

A

SFABP

Singleton
Factory Method
Abstract Factory
Builder
Prototype

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

Ensure only one instance of a class exist (CP)

A

Singleton

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

Let subclasses decide which class to instantiate (CP)

A

Factory method

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

Interface for creating families of related objects (CP)

A

Abstract Factory

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

Construct complex objects step-by-step (CP)

A

Builder

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

Clone an object to create a new one (CP)

20
Q

Concerned with composing classes and
objects to form larger structures.

A

Structural Patterns

20
Q

Structural Patterns Examples

A

ABCDFFP

Adapter
Bridge
Composite
Decorator
Facade
Flyweight
Proxy

21
Q

Converts one interface into another (SP)

22
Q

Separates abstraction from implementation. (SP)

23
Q

Treats individual objects and compositions uniformly (SP)

24
Adds responsibilities to objects dynamically (SP)
Decorator
25
Provides a simplified interface to a complex subsystem (SP)
Facade
26
Reduces memory usage by sharing objects (SP)
Flyweight
27
Acts as a placeholder for another object (SP)
Proxy
28
Deal with communication between objects, and the delegation of responsibility.
Behavioral Patterns
29
Behavioral Patterns Examples
COSTV Command Observer Strategy Template Method Visitor, Mediator, Memento, Chain of Responsibility, Interpreter, State (VMMCIS)
30
Encapsulates a request as an object (BP)
Command
31
Notifies dependents of changes to an object (BP)
Observer
32
Defines a family of interchangeable algorithms (BP)
Strategy
33
Defines skeleton of algorithm; subclasses define steps (BP)
Template Method
34
Problem: * Ensure that a class has only one instance and provide a global point of access to it. * This is useful when exactly one object is needed to coordinate actions across the system—such as in the case of a configuration manager, logging service, or database connection handler.
Singleton Pattern
35
Solution: * Make the class responsible for keeping track of its sole instance. * Restrict instantiation of the class using private or protected constructors (in Python, simulate this using naming conventions and internal logic). * Provide a static access method (e.g., get_instance()) that returns the same instance every time it is called.
Singleton Pattern
36
Singleton Pattern Advantages
Consequences: Advantages: * Controlled access to the sole instance. * Reduced memory usage (only one object is created). * Useful for shared resources like logging or configuration.
37
Singleton Pattern Advantages Disadvantages
Disadvantages: * Difficult to unit test due to global state. * Can introduce hidden dependencies across the system. * May violate the Single Responsibility Principle by controlling its own lifecycle.
38
Problem: * You have an existing class with a useful implementation, but its interface doesn't match the one a client (or another part of your code) expects
Adapter Pattern
39
Solution: * Create an adapter class that translates the interface of the existing class into the interface the client expects. * The adapter sits in between and "converts" requests from the client into a form the adaptee can understand, and then may also convert the response back.
Adapter Pattern
40
Adapter Pattern Advantages
Advantages: * Allows reuse of existing functionality without modifying original code. * Promotes loose coupling between client code and legacy/third-party code. * Makes systems more flexible and extensible.
41
Adapter Pattern Disadvantages:
Disadvantages: * Adds an extra layer of complexity. * Overuse can lead to too many layers or overly abstract systems. * If behavior translation is complex, the adapter itself can become bloated.
42
Problem: You want to decouple the object that issues a request from the object that knows how to perform it. This is especially useful when you need to support operations like undo/redo, queuing requests, or logging actions.
Command Pattern
43
Solution: Encapsulate a request as an object, separating the command execution from the command invocation. Each command object implements a standard interface (e.g., execute()), and contains all the details needed to perform an action.
Command Pattern
44
Command Pattern Advantages
Advantages: * Decouples sender and receiver of a request. * Makes it easy to add new commands without changing existing code. * Supports undo/redo, logging, and transaction-based operations.
45
Command Pattern Disadvantages
Disadvantages: * Can introduce a large number of small command classes. * Code can become harder to follow due to indirection.