Master Design Patterns with 51 free flashcards. Study using spaced repetition and focus mode for effective learning in Programming.
Design patterns are reusable solutions to commonly occurring problems in software design. They were popularized by the Gang of Four (GoF) in their 1994 book. They are not code, but templates for solving problems that can be adapted to many situations.
Benefits:Provide a shared vocabulary among developersPromote proven, tested solutionsImprove code maintainability and flexibility
The three categories are:
Creational Patterns – Deal with object creation mechanisms (e.g., Singleton, Factory Method, Builder)Structural Patterns – Deal with object composition and relationships (e.g., Adapter, Decorator, Facade)Behavioral Patterns – Deal with communication between objects (e.g., Observer, Strategy, Command)
The Singleton pattern ensures a class has only one instance and provides a global point of access to it.
Key traits:Private constructorStatic method to get the instanceLazy or eager initializationExample: public static Singleton getInstance() { if (instance == null) instance = new Singleton(); return instance; }
Singleton drawbacks include:Introduces global state, making code harder to testViolates the Single Responsibility Principle (manages its own lifecycle)Thread safety issues if not implemented carefullyTight coupling to the Singleton classOften considered an anti-pattern when overused
The Factory Method defines an interface for creating objects but lets subclasses decide which class to instantiate.abstract class Creator {
abstract Product factoryMethod();
}
Benefits:Follows the Open/Closed PrincipleDecouples client code from concrete classesSubclasses control instantiation
The Abstract Factory provides an interface for creating families of related objects without specifying their concrete classes.
Example: A UI toolkit factory that creates buttons, checkboxes, and menus for different OS platforms.interface GUIFactory {
Button createButton();
Checkbox createCheckbox();
}
Use when the system must be independent of how its products are created.
Factory Method uses inheritance — a single method in a subclass creates one productAbstract Factory uses composition — a factory object creates a family of related productsFactory Method creates one product; Abstract Factory creates multiple related productsAbstract Factory often uses Factory Methods internally
The Builder pattern separates the construction of a complex object from its representation, allowing the same construction process to create different representations.Pizza pizza = new Pizza.Builder()
.size("large")
.cheese(true)
.pepperoni(true)
.build();
Use when:An object has many optional parametersConstruction involves multiple steps
The Prototype pattern creates new objects by cloning an existing object (the prototype) rather than using constructors.Prototype clone = original.clone();
Benefits:Avoids costly creation from scratchHides the complexities of creating new instancesUseful when objects differ only slightlyUses the Cloneable interface in Java or copy constructors in C++.
The Adapter pattern converts the interface of a class into another interface that clients expect. It lets classes work together that couldn't otherwise due to incompatible interfaces.
Analogy: A power plug adapter lets a US plug fit a European socket.
Two forms:Class Adapter – uses multiple inheritanceObject Adapter – uses composition (preferred)
The Bridge pattern decouples an abstraction from its implementation so the two can vary independently.
Example: Shape (abstraction) and Color (implementation) — a RedCircle combines both without creating a class for every combination.
Key benefit: Prevents a cartesian product explosion of subclasses when you have multiple dimensions of variation.
The Composite pattern composes objects into tree structures to represent part-whole hierarchies. It lets clients treat individual objects and compositions uniformly.
Example: A file system where both File and Directory implement a common FileSystemComponent interface.interface Component {
void operation();
}
class Leaf implements Component {...}
class Composite implements Component {...}
Flashcards
Flip to reveal
Focus Mode
Spaced repetition
Multiple Choice
Test your knowledge
Type Answer
Active recall
Learn Mode
Multi-round mastery
Match Game
Memory challenge