s8 Flashcards

(46 cards)

1
Q

What is a pattern?

A

A recurring theme, design, or arrangement observable in various contexts; a regularity in data, behavior, structures, or events.

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

What is a Design Pattern in software?

A

A general, reusable solution to a commonly occurring software design problem. It’s a template to be modified and implemented, not ready-made code.

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

What are Design Patterns concerned with?

A

Application & System Design.
Abstractions on top of code.
Relationships between classes & other collaborators.
Problems that have already been solved.

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

What are Design Patterns NOT concerned with?

A

Algorithms.
Specific implementations or classes.

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

How do Design Patterns differ from Architectural Styles?

A

Architectural Styles: Focus on components and their relationships (higher-level diagrams like component, activity diagrams).

Design Patterns: Focus on low-level code, objects, and classes (expect code, class diagrams). (Note: The line between them can be blurry).

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

Why are Design Patterns useful?

A

Help anticipate and manage change in software.
Can target complex conditional logic.
Provide a common language for developers to communicate solutions.

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

What is an Anti-Pattern?

A

A design pattern that is ineffective and counterproductive, representing common pitfalls in software design.

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

Name some common Anti-Patterns and briefly describe them.

A

Spaghetti Code (Big Ball of Mud): Ad hoc software structure, difficult to extend and optimize.

Lava Flow: Unready code put into production and added to while still unfinished.

Golden Hammer: Obsessively applying a familiar tool to every software problem.

Boat Anchor: Code that doesn’t do anything is left in the codebase “just in case.”

God Class: One class taking on too many responsibilities.

Poltergeist Class: Useless classes with no real responsibility, often just invoking methods in another class or adding unneeded abstraction.

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

What are the SOLID Principles and who introduced them?

A

A set of guidelines for writing high-quality, maintainable, and scalable software, introduced by Robert C. Martin. They help developers write software that is easy to understand, modify, and extend.

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

What does SOLID stand for?

A

Single Responsibility Principle (SRP)
Open-Closed Principle (OCP)
Liskov Substitution Principle (LSP)
Interface Segregation Principle (ISP)
Dependency Inversion Principle (DIP)

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

Explain the Single Responsibility Principle (SRP).

A

A class should have only one reason to change, meaning it should have only one responsibility or job. Too many responsibilities lead to code that is difficult to understand and maintain.

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

Explain the Open-Closed Principle (OCP).

A

Software entities (classes, modules, functions) should be open for extension but closed for modification. Behavior can be extended without modifying existing source code, promoting extensibility and maintainability.

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

Explain the Liskov Substitution Principle (LSP).

A

Any instance of a derived class should be substitutable for an instance of its base class without affecting the program’s correctness. A derived class should behave like its base class in all contexts.

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

Explain the Interface Segregation Principle (ISP).

A

No client should be forced to depend on methods it does not use. It’s better to create smaller, more focused interfaces for specific use cases rather than large, general-purpose interfaces. This results in more cohesive and less coupled interfaces.

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

Explain the Dependency Inversion Principle (DIP).

A

High-level modules should not depend on low-level modules; both should depend on abstractions. Abstractions should not depend on details; details should depend on abstractions. This aims to reduce coupling and increase modularity.

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

Name the main categories of Design Patterns

A

Creational: Deal with object creation mechanisms.

Structural: Deal with organizing objects into larger structures and providing new functionality.

Behavioral: Deal with common communication patterns between objects.

Concurrency: Deal with multi-threaded programs.

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

What is the main goal of Creational Design Patterns?

A

To abstract object creation, allowing flexibility (polymorphism), decoupling clients from concrete classes, and managing how objects are created. They often shift emphasis from pure inheritance to composition and interfaces.

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

What is the Prototype Pattern?

A

Creates instances of an object by cloning a prototypical instance. Cloning existing objects can be computationally cheaper and copies encapsulated attributes that the client might not know.

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

What are the main components of the Prototype Pattern?

A

Prototype: Defines the interface for cloned objects (e.g., a clone() method).
ConcretePrototype: Implements the Prototype interface to perform cloning.
Client: Creates an initial instance of a ConcretePrototype and then clones it to make additional instances.

20
Q

When should you use the Prototype Pattern?

A

When a system should be independent of how its products are created, composed, and represented.

When the class to be instantiated is selected at run-time.

When instances of a class have few different combinations of state (cloning pre-configured prototypes is easier).

21
Q

List pros of the Prototype Pattern.

A

Clone objects without coupling to their concrete classes.

Reduce repeated initialization code by cloning pre-built prototypes.

Produce complex objects more conveniently.

Alternative to inheritance for configuration presets of complex object

22
Q

List cons of the Prototype Pattern.

A

Cloning complex objects with circular references can be very tricky.

23
Q

What is the Factory Method Pattern (or Simple Factory concept)?

A

Suggests replacing direct object construction calls (new operator) with calls to a special factory method. The new operator is still used, but within the factory method. Objects returned are often called “products.”

24
Q

What are the main components of the Factory Method Pattern?

A

Product: Defines the interface for objects the factory method create
s.
ConcreteProduct: Implements the Product interface.

Creator (or Factory): Declares the factory method, which returns an object of type Product. May also define a default implementation.

ConcreteCreator (or ConcreteFactory): Overrides the factory method to return an instance of a ConcreteProduct.

25
When should you use the Factory Method Pattern?
When a class can't anticipate the class of objects it must create. When a class wants its subclasses to specify the objects it creates. When you want to localize knowledge about object creation.
26
List pros of the Factory Method Pattern.
Modular expandability (Open-Closed Principle). Delegates object creation (Single Responsibility Principle). Straightforward to test.
27
List cons of the Factory Method Pattern.
Requires more classes than a straightforward constructor call.
28
What is the Abstract Factory Pattern (Kit Pattern)?
Provides an interface for creating families of related or dependent objects without specifying their concrete classes. Factories can be implemented using factory methods or prototypes.
29
What are the main components of the Abstract Factory Pattern?
AbstractProduct: Interface for a type of product. ConcreteProduct: Implements an AbstractProduct interface. AbstractFactory: Declares an interface for operations that create abstract product objects. ConcreteFactory: Implements the operations to create concrete product objects.   Client: Uses interfaces declared by AbstractFactory and AbstractProduct.
30
When should you use the Abstract Factory Pattern?
When a system should be independent of how its products are created, composed, and represented. When a system should be configured with one of multiple families of products. When a family of related product objects is designed to be used together, and this constraint needs to be enforced. When providing a class library of products, revealing only interfaces, not implementations.
31
List pros of the Abstract Factory Pattern.
Ensures products from a factory are compatible. Avoids tight coupling between concrete products and client code. Product creation code is centralized (SRP). Can introduce new variants of products without breaking client code (OCP).
32
List cons of the Abstract Factory Pattern.
Code can become more complicated due to many new interfaces and classes.
33
What is the Builder Pattern?
Separates the construction of a complex object from its representation, allowing the same construction process to create different representations.  
34
What is the motivation for using the Builder Pattern?
To avoid a large number of subclasses for different configurations or a very messy constructor with many parameters when creating complex objects. It breaks object construction into steps.
35
What are the main components of the Builder Pattern?
Product: The complex object to be built. Builder: Interface for creating parts of the Product. ConcreteBuilder: Implements the Builder interface to construct and assemble parts of the product. Director (Optional): Coordinates the Builder to construct the object in a specific sequence.
36
When should you use the Builder Pattern?
When the algorithm for creating a complex object should be independent of the parts that make it up and how they're assembled. When the construction process must allow different representations for the object being constructed (e.g., stone house vs. wooden house).
37
List pros of the Builder Pattern.
Construct objects step-by-step, defer steps, or run steps recursively. Reuse the same construction code for various product representations. Isolate complex construction code from business logic (SRP).
38
List cons of the Builder Pattern.
Overall code complexity increases due to creating multiple new classes.
39
What is the Singleton Pattern?
Restricts the instantiation of a class to a single instance and provides a global point of access to it.
40
How is the Singleton Pattern typically implemented?
The default constructor is made private. A static creation method is used, which calls the private constructor only the first time it's invoked and returns the same instance on subsequent calls
41
How do Singletons differ from Global Variables?
Both are globally accessible, but Singletons can encapsulate/hide information, avoid cluttering namespaces, allow for lazy allocation (memory allocated only when needed), and can be subclassed.
42
When should you use the Singleton Pattern?
When there must be exactly one instance of a class, and it must be accessible to clients from a well-known access point. (Often considered an anti-pattern by some).
43
List pros of the Singleton Pattern.
Ensures only a single instance of a class. Provides a global access point to that instance. The object is initialized only when first requested (lazy initialization).
44
List cons of the Singleton Pattern.
Can mask bad design (components know too much about each other). Can lead to tighter coupling. Hard to execute correctly in multi-threaded environments. Hard to create mock singletons for testing. Not future-proof if more than one instance is needed later. Violates Single Responsibility Principle (manages its own lifecycle in addition to its primary role).
45
Name some other Creational Patterns not in the original GoF list.
Dependency Injection: An object receives objects it depends on. Lazy Initialization: Object creation is delayed until actually needed. Multiton: Generalizes Singleton to multiple instances. Object Pool: Recycles objects from a pool instead of creating/destroying them.
46
What are some pitfalls of using Design Patterns?
Don't design for patterns; refactor initial designs to patterns if appropriate. Optimization is often secondary to getting a product out. Uncritical use can turn them into anti-patterns. Some patterns might be unnecessary due to language features. Approach to patterns can change with different languages/paradigms.