Design Patterns Flashcards
What is the Factory Pattern?
Provides an interface for creating objects, with subclasses deciding which class to instantiate.
Example: Pizza Factory
What is the Singleton Pattern?
Ensures a class has only one instance and provides a global point of access to it.
Example: Logger Class
What is the Observer Pattern?
Defines a one-to-many dependency between objects so that when one changes state, all its dependents are notified.
Example: Newsletter Subscription
What is the Decorator Pattern?
Allows behavior to be added to individual objects without affecting the behavior of other objects from the same class.
Example: Coffee Shop (adding milk, sugar, etc.)
What is the Adapter Pattern?
Converts the interface of a class into another interface that clients expect.
Example: Charging Adapter
What is the Strategy Pattern?
Defines a family of algorithms, encapsulates each one, and makes them interchangeable.
Example: Payment Processing (Credit Card, PayPal)
What is the Command Pattern?
Turns a request into a stand-alone object that contains all information about the request.
Example: Remote Control Buttons
What is the Prototype Pattern?
Creates new objects by copying an existing object (prototype).
Example: Cloning a User Profile
What is the Facade Pattern?
Provides a unified interface to a set of interfaces in a subsystem.
Example: Home Theater System (unifying controls for TV, sound, etc.)
What is the Mediator Pattern?
Encapsulates how a set of objects interact, promoting loose coupling.
Example: Chatroom
What is the Chain of Responsibility Pattern?
Passes a request along a chain of handlers until an object handles it.
Example: Customer Support (tiered escalation)
What is the Builder Pattern?
Allows constructing complex objects step-by-step.
Example: Building a Meal (burger, drink, sides)
What is the Template Method Pattern?
Defines the program skeleton in a method, deferring some steps to subclasses.
Example: Game Algorithm (setup, play, end)
What is the State Pattern?
Allows an object to alter its behavior when its internal state changes.
Example: Vending Machine
What is the Proxy Pattern?
Provides a surrogate or placeholder for another object.
Example: Virtual Proxy for Image Loading