s8 Flashcards
(46 cards)
What is a pattern?
A recurring theme, design, or arrangement observable in various contexts; a regularity in data, behavior, structures, or events.
What is a Design Pattern in software?
A general, reusable solution to a commonly occurring software design problem. It’s a template to be modified and implemented, not ready-made code.
What are Design Patterns concerned with?
Application & System Design.
Abstractions on top of code.
Relationships between classes & other collaborators.
Problems that have already been solved.
What are Design Patterns NOT concerned with?
Algorithms.
Specific implementations or classes.
How do Design Patterns differ from Architectural Styles?
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).
Why are Design Patterns useful?
Help anticipate and manage change in software.
Can target complex conditional logic.
Provide a common language for developers to communicate solutions.
What is an Anti-Pattern?
A design pattern that is ineffective and counterproductive, representing common pitfalls in software design.
Name some common Anti-Patterns and briefly describe them.
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.
What are the SOLID Principles and who introduced them?
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.
What does SOLID stand for?
Single Responsibility Principle (SRP)
Open-Closed Principle (OCP)
Liskov Substitution Principle (LSP)
Interface Segregation Principle (ISP)
Dependency Inversion Principle (DIP)
Explain the Single Responsibility Principle (SRP).
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.
Explain the Open-Closed Principle (OCP).
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.
Explain the Liskov Substitution Principle (LSP).
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.
Explain the Interface Segregation Principle (ISP).
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.
Explain the Dependency Inversion Principle (DIP).
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.
Name the main categories of Design Patterns
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.
What is the main goal of Creational Design Patterns?
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.
What is the Prototype Pattern?
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.
What are the main components of the Prototype Pattern?
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.
When should you use the Prototype Pattern?
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).
List pros of the Prototype Pattern.
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
List cons of the Prototype Pattern.
Cloning complex objects with circular references can be very tricky.
What is the Factory Method Pattern (or Simple Factory concept)?
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.”
What are the main components of the Factory Method Pattern?
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.