OOP Design Patterns Flashcards
What are Design Patterns in OOP?
Design patterns are reusable solutions to common software design problems. They provide proven approaches to structure code and improve maintainability, flexibility, and reusability.
What are the different categories of Design Patterns?
Design patterns are typically categorized as Creational, Structural, and Behavioral. Creational patterns focus on object creation, Structural patterns on object composition, and Behavioral patterns on communication between objects.
What is the Singleton Pattern?
Ensures a class has only one instance and provides a global access point to it.
What is the Factory Method Pattern?
Creates objects without specifying the exact class to be instantiated.
What is the Abstract Factory Pattern?
Provides an interface for creating families of related objects.
What is the Builder Pattern?
Separates object construction from its representation, allowing step-by-step object creation.
What is the Prototype Pattern?
Creates new objects by cloning an existing object.
What is the Adapter Pattern?
Allows incompatible interfaces to work together by wrapping an object with an adapter interface.
What is the Proxy Pattern?
Provides a surrogate or placeholder for another object to control access or add additional functionality.
What is the Decorator Pattern?
Adds new functionality to an object dynamically without altering its structure.
What is the Facade Pattern?
Provides a simplified interface to a complex system or a set of classes.
What is the Composite Pattern?
Treats a group of objects as a single object, allowing for composing parts into a tree structure.
What is the Strategy Pattern?
Defines a family of algorithms, encapsulates each one, and makes them interchangeable.
What is the Observer Pattern?
Defines a one-to-many dependency between objects when one object changes state, all its dependents are notified.
What is the Command Pattern?
Encapsulates a request as an object, allowing for parameterizing clients with different requests, queuing or logging requests, and undo/redo functionality.
What is the Template Method Pattern?
Defines the skeleton of an algorithm in an operation, deferring some steps to subclasses.
What is the Iterator Pattern?
Provides a way to access the elements of an object collection sequentially without exposing its underlying representation.
What is the State Pattern?
Allows an object to alter its behavior when its internal state changes.
What is the Memento Pattern?
Without violating encapsulation, captures and externalizes an object’s internal state so it can be restored later.
When to Use Design Patterns?
Use design patterns when you encounter recurring problems and need a proven, flexible solution. They are not a silver bullet and should be applied thoughtfully to avoid over-engineering.
Situation: Only one “Settings” object needed in your app for global access.
Singleton Pattern - Ensures a single “Settings” instance exists for app-wide configuration access.
Situation: Deciding between creating a “Button” or a “Dropdown” based on user input.
Factory Method Pattern - Hides button/dropdown creation logic, allowing for flexible UI element generation based on user choice.
Situation: Integrating an old “Music Player” library with a new system that uses different controls.
Adapter Pattern - Acts as a bridge, allowing the new system to work with the old “Music Player” library despite incompatible interfaces.
Situation: Adding new functionalities (like encryption) to an existing “Email” object without modifying its core functionality.
Decorator Pattern - Lets you dynamically add encryption functionality to the “Email” object without changing its core logic.