Design Patterns

104 flashcards by MentisLibrary

The 23 Gang of Four patterns with correct examples, plus SOLID and the design principles behind them, concurrency and architectural patterns, and the anti-patterns that show what happens when they are misapplied.

What is a design pattern?
A named, reusable solution to a problem that recurs in a particular context, described together with its consequences. It is a design template to be adapted, not code to be copied.
Who wrote the Gang of Four book, and what does it contain?
Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides published "Design Patterns: Elements of Reusable Object-Oriented Software" in 1994. It catalogues 23 patterns in three categories: creational, structural and behavioral.
Where did the idea of design patterns come from?
From the architect Christopher Alexander, whose 1977 book "A Pattern Language" described recurring solutions in building and town design, each stated as a context, a problem and a solution.
What sections make up a pattern description?
A name, the intent, the problem and its context, the solution structure with participants, the consequences and trade-offs, and known uses. The consequences section is the one most often skipped.
Idiom, design pattern and architectural style: how do they differ?
An idiom is a low-level, language-specific technique. A design pattern is a mid-level, language-independent object structure. An architectural style shapes the whole system.
What does "program to an interface, not an implementation" mean?
Depend on the abstract type describing what an object does, not on the concrete class that does it, so implementations can be substituted without changing the callers.
What does "favor object composition over class inheritance" mean?
Prefer assembling behaviour from objects held as fields over inheriting it, because composition can change at runtime and does not expose the parent's internals to the subclass.
When should you NOT apply a design pattern?
When the problem it addresses has not appeared yet. A pattern adds indirection, and indirection you do not need makes code harder to read for no benefit.
What is refactoring to patterns?
Arriving at a pattern by refactoring existing code toward it when the code's actual pressures call for it, rather than designing it in up front. Joshua Kerievsky wrote the book of the same name.
What is the main criticism of the Gang of Four catalogue?
That several patterns compensate for features missing from the languages of the time. In languages with first-class functions, closures or built-in iteration, some patterns collapse into ordinary code.
What is the practical value of pattern names?
Shared vocabulary. Saying "wrap it in a Decorator" conveys structure, intent and consequences in three words, where describing the same design takes a paragraph.
Which pattern catalogues exist beyond the Gang of Four?
"Pattern-Oriented Software Architecture" for architectural and concurrency patterns, Martin Fowler's "Patterns of Enterprise Application Architecture" for data and domain patterns, and "Enterprise Integration Patterns" for messaging.
What problem do creational patterns solve?
They decouple a system from how its objects are created, so the code that uses an object does not name its concrete class or manage the details of building it.
Singleton
Ensures a class has only one instance and provides a global access point to it. The class itself enforces the constraint by making the constructor private.
Why is Singleton the most criticized pattern?
It introduces hidden global state and hidden dependencies: callers reach for it directly, so it never appears in a signature. Tests cannot substitute it, and its lifetime is uncontrolled.
What is the safe way to create a lazy singleton in a multithreaded program?
Use a language mechanism guaranteeing one-time initialization: static initialization, a holder class, or a purpose-built lazy primitive. Hand-written double-checked locking is easy to get subtly wrong.
Factory Method
Defines a method for creating an object but lets subclasses decide which concrete class to instantiate, so the base class can use the product without knowing its type.
Abstract Factory
Provides an interface for creating families of related objects without naming their concrete classes, so a whole family can be swapped at once.
Factory Method vs Abstract Factory
Factory Method creates one product and varies it through subclassing. Abstract Factory creates a family of related products and varies them by swapping the whole factory object.
Builder
Separates the construction of a complex object from its representation, assembling it step by step so the same process can produce different results.
What problem does Builder solve that constructors do not?
Telescoping constructors: with many optional parameters you either write an overload for every combination or accept a long argument list where the meaning of each position is invisible.
Prototype
Creates new objects by copying an existing instance rather than constructing from scratch, so the prototype's configuration is carried over.
Object Pool
Reuses a fixed set of expensive-to-create objects, lending them out and returning them rather than creating and destroying them repeatedly.
How does dependency injection relate to the creational patterns?
It moves construction out of the object entirely: dependencies are supplied from outside, usually through the constructor, so the class never chooses its collaborators.
What problem do structural patterns solve?
How classes and objects are composed into larger structures while keeping the composition flexible and the pieces independently replaceable.
Adapter
Converts the interface of an existing class into the interface a client expects, letting classes work together that could not otherwise.
Object adapter vs class adapter
An object adapter holds the adaptee as a field and delegates to it. A class adapter inherits from both the target interface and the adaptee, which requires multiple inheritance.
Bridge
Separates an abstraction from its implementation so the two can vary independently, by holding the implementation as a field rather than inheriting it.
Bridge vs Adapter
Bridge is designed in up front to let two hierarchies vary independently. Adapter is applied afterwards to make an existing incompatible interface usable.
Composite
Composes objects into tree structures and lets clients treat individual objects and compositions uniformly through one interface.