1. Home
  2. Explore
  3. Computer Science
  4. Design Patterns Review

Study methods

Flashcards
Quiz
Fill in the blanks
Written test
Lesson
Notes
Podcast
Content

Design Patterns Review

Design Patterns: A Complete Review

Design patterns are a way to capture and communicate effective solutions to recurring software-design problems. Rather than memorizing a collection of names, the student should understand the reasoning behind patterns: what problem each pattern addresses, what forces make that problem difficult, how the proposed solution works, and what benefits and drawbacks result.

1. What Is a Design Pattern?

A design pattern is a proven solution idea or solution template for a problem that occurs repeatedly in software development.

Three parts of this definition are especially important:

  1. The problem recurs.
    A pattern is not usually created for a single, unusual situation. It addresses a type of problem that developers encounter repeatedly.

  2. The solution has been proven through experience.
    A design pattern is based on solutions that have been applied successfully in multiple projects or situations.

  3. The pattern describes an idea, not one fixed implementation.
    A pattern does not prescribe one exact class or one exact programming-language construct. Instead, it explains the structure and reasoning of a solution so that it can be adapted to different systems and languages.

For example, the Adapter pattern does not require one particular programming language or class hierarchy. Its central idea is to make one interface usable where another interface is expected. The exact implementation depends on the programming language and the surrounding software.

A design pattern is a proven, reusable solution idea for a recurring problem in a particular context.

Patterns should not be used automatically. A small script or a “Hello, World” program normally does not need a large collection of design patterns. Patterns become valuable when a system is complex enough that developers must manage flexibility, dependencies, communication, object creation, resource use, or changing behavior.

Why Patterns Matter

Patterns provide several important benefits:

  • They allow developers to reuse design knowledge instead of reinventing solutions.
  • They provide a shared vocabulary for discussing software design.
  • They help developers recognize common problems more quickly.
  • They make design decisions easier to explain.
  • They help developers compare alternatives by examining benefits and drawbacks.
  • They can make larger systems more flexible and easier to extend.

Knowing the name of a pattern is useful because it compresses a larger design discussion into a short expression. A developer can say, “This component could use a mediator,” or “The object’s changing behavior suggests the state pattern.” The name acts as a reference to a known design idea.

2. The Origins of Pattern Thinking

The pattern movement began in architecture, not software. Christopher Alexander established the foundation of the movement in 1977 through his work on A Pattern Language.

Alexander’s central idea was that recurring problems in architecture could be described using reusable patterns. A pattern should not merely state a solution. It should also explain:

  • The situation in which the pattern applies
  • The problem that must be solved
  • The forces or constraints that make the problem difficult
  • The solution to the problem
  • The consequences of applying the solution

The pattern movement later spread to software development and software architecture. In software, patterns became a way to preserve and communicate design experience.

One of the most influential software-pattern books was Design Patterns: Elements of Reusable Object-Oriented Software, written by the Gang of Four:

  • Erich Gamma
  • Richard Helm
  • Ralph Johnson
  • John Vlissides

The Gang of Four collected and organized patterns from different authors, projects, and pattern conferences. Their work helped establish a common vocabulary for object-oriented design.

Another important body of work is the Pattern-Oriented Software Architecture series, often called the POSA books. Douglas Schmidt is associated with this pattern community and contributed to the collection and explanation of software patterns. These books and lectures help demonstrate that patterns are not merely theoretical labels; they come from practical software-development experience.

3. The Structure of a Design Pattern

A complete pattern description contains several connected elements. These elements can be remembered through a “pattern house” metaphor:

  • The name is the roof.
  • The context is the surrounding structure.
  • The problem and forces explain why the situation is difficult.
  • The solution describes the design approach.
  • The consequences explain the resulting benefits and liabilities.
  • The known uses provide the foundation, because repeated real-world use demonstrates that the pattern works.

Pattern Name

A pattern needs a concise, memorable name, such as:

  • Adapter
  • Broker
  • Active Object
  • Strategy
  • Command
  • State

A good name is more useful than a long sentence because it gives developers a compact way to refer to a larger body of knowledge.

Context

The context describes the setting in which the pattern applies. Patterns can exist in many domains, including architecture, hardware, social organization, and software.

A software-development pattern should therefore be described in a software context. A pattern that solves a software problem cannot simply be transferred without consideration to an unrelated domain such as mountain camping. The context identifies the type of environment, system, or development situation in which the pattern is meaningful.

Problem

The problem explains what must be solved. A meaningful pattern problem is generally not trivial. If the correct action is obvious, a pattern is unnecessary.

The problem becomes especially interesting when several goals conflict. For example, a system might need to be:

  • Flexible but efficient
  • Easy to extend but simple to understand
  • Highly decoupled but not overloaded with communication
  • Resource-safe but lightweight
  • General enough for reuse but small enough for limited hardware

Forces

The forces are the competing pressures, constraints, and requirements that make the problem difficult.

Forces may arise from:

  • Limited memory
  • Limited processor capacity
  • Low energy availability
  • Communication overhead
  • Requirements for flexibility
  • Requirements for simplicity
  • The need to support changing behavior
  • The need to coordinate many objects

The forces explain why there is no perfect solution. Choosing a design usually means improving one concern while accepting some cost in another.

For example, a broker can decouple clients and servers, but it introduces communication overhead and requires additional infrastructure. An embedded system may benefit from a lightweight design even if that design offers less flexibility than a more elaborate architecture.

Solution

The solution gives a generic description of how the problem can be addressed. It usually includes both a static and a dynamic part.

The static part describes the participating classes or objects and their relationships. It answers questions such as:

  • Which objects exist?
  • Which objects provide interfaces?
  • Which objects depend on other objects?
  • Which responsibilities belong to which components?

The dynamic part describes what happens at runtime. It explains:

  • Which object calls which other object
  • How objects communicate
  • How objects are created or connected
  • How control and data flow through the design
  • How the objects cooperate to solve the problem

Consequences

The consequences describe what happens after the pattern is applied. This includes both benefits and liabilities.

For example, applying a broker may provide:

  • Separation between clients and servers
  • Greater flexibility in locating or replacing components
  • A clearer communication structure

However, it may also introduce:

  • Communication overhead
  • Additional processes or machines
  • The need to register components
  • More complicated deployment
  • Less direct awareness of where a service is located

Consequences are essential because patterns are not universally beneficial. A pattern is appropriate only when its benefits justify its costs in the given context.

The consequences also reveal the pattern’s limitations. For example, the Observer pattern can be useful for notifying dependent objects about a change. However, if a single change must be delivered synchronously to an extremely large number of observers, the application may become slow because every observer must be notified.

Known Uses

Known uses are real examples of the pattern being applied successfully. A solution becomes a pattern through repeated use and experience. Known uses demonstrate that the solution is not merely an untested idea.

The pattern’s history therefore matters: repeated application reveals which benefits are real, which drawbacks are acceptable, and which situations are unsuitable.

A pattern is not complete without its consequences. The consequences contain the practical experience that tells developers when the pattern is useful and when it may cause problems.

Think About It

If a pattern provides flexibility but also introduces overhead, what information is needed before deciding whether the pattern is appropriate?

4. Major Categories of Design Patterns

Patterns can be grouped according to the kind of design problem they address. These categories are useful for orientation, although they are not absolute rules. Some patterns can reasonably be associated with more than one category.

5. Wrapping Patterns

Wrapping patterns place a layer of abstraction around an existing component or complexity. The general strategy is to avoid exposing the original complexity directly and instead provide a new interface or layer around it.

Important examples include:

  • Adapter
  • Proxy
  • Decorator
  • Facade

An Adapter makes an existing component compatible with a different interface. It is useful when two components have suitable functionality but incompatible interfaces.

A Proxy represents or stands in for another object. The proxy can control access to the actual object or provide an additional layer between the client and the real object.

A Decorator adds responsibilities or behavior to an object without changing the object’s underlying class. This supports flexible combinations of behavior.

A Facade provides a simpler interface to a more complicated subsystem. Instead of forcing clients to understand every internal component, the facade presents a convenient entry point.

The shared idea is abstraction: the client interacts with a simpler or more suitable layer instead of dealing directly with the underlying complexity.

6. Creational Patterns

Creational patterns focus on how objects are created. Object creation can become complicated when it involves multiple steps, configuration choices, or restrictions on the number of instances.

Builder

The Builder pattern separates the construction of an object from the final object itself. It is especially useful when an object must be assembled through multiple steps.

Instead of forcing application code to perform every construction step directly, the builder organizes the process. This can make object creation clearer and can allow different construction processes to produce different versions of an object.

Prototype

The Prototype pattern uses an existing object as a template for creating another object. This is helpful when creating a new object from scratch is complicated or when the new object should initially resemble an existing one.

The prototype idea can also support creating objects at runtime rather than relying only on fixed class-construction logic.

Singleton

The Singleton pattern ensures that only one instance of a particular object exists. It also provides a controlled way to access that instance.

The benefit is that the creation restriction is handled by the pattern rather than being left to every application developer. The pattern guarantees that repeated requests do not create multiple instances.

Because a singleton imposes a global uniqueness rule, it should be used only when that restriction is genuinely part of the problem.

7. Behavioral Patterns

Behavioral patterns focus on how objects perform actions, change behavior, and communicate responsibilities.

Strategy

The Strategy pattern represents an algorithm or behavior in an interchangeable form. The client can use the abstract operation without implementing the algorithm itself.

This is helpful when:

  • Several algorithms solve the same general problem
  • The algorithm should be replaceable
  • The choice of algorithm may change at runtime
  • The surrounding object should not be tightly coupled to one implementation

The Strategy pattern supports the Open/Closed Principle because new behavior can often be introduced by adding a new strategy rather than modifying the central class.

Command

The Command pattern packages a request or method call into an object. The command object can then be stored, passed to another component, executed later, or reversed when undo is supported.

This makes it possible to treat an action as data. A system can queue commands, log them, delay them, or provide undo functionality without requiring the original requester to perform the action immediately.

State

The State pattern changes an object’s behavior according to its current state. It is especially useful when an object can be viewed as a state machine.

For example, the same request may produce different behavior depending on whether the object is in one state or another. Instead of placing all state-dependent behavior into a large collection of conditional statements, the design organizes behavior around states and transitions.

The pattern is useful when:

  • An object has several distinct states
  • Each state changes how the object responds
  • State transitions are important
  • Conditional logic would otherwise become difficult to maintain

The State pattern is frequently combined with other patterns, because state-dependent behavior often also requires strategies, commands, or communication between objects.

8. Architectural Patterns

Architectural patterns influence the organization of an entire software system or a large portion of it. They shape the major components, responsibilities, and communication paths.

Examples include:

  • Layers
  • Broker
  • Master-Slave
  • Client-Server
  • Pipes and Filters
  • Model-View-Controller

A Layers architecture divides a system into levels, with each layer having a particular responsibility and interacting according to defined relationships.

A Broker architecture introduces an intermediary that helps clients and services communicate without requiring each client to know the direct location or details of each service.

A Master-Slave structure assigns central coordination to a master while distributing work or responsibility to slave components.

A Client-Server architecture separates requesters of services from providers of services.

A Pipes and Filters architecture processes data through a sequence of independent processing stages. Each filter transforms the data, and pipes connect the stages.

Model-View-Controller

Model-View-Controller, or MVC, separates responsibilities into three roles:

  • The Model represents the data and underlying application state.
  • The View presents information to users.
  • The Controller coordinates input and determines how the model and view should respond.

The main purpose is to prevent all responsibilities from being concentrated in one component. MVC also defines a structured flow of communication and control between the roles.

Although MVC is sometimes classified as a communication-related pattern, its more important architectural purpose is the separation of responsibilities across the system.

9. Collection-Related Patterns

The Iterator pattern provides a way to access elements in a collection sequentially without exposing the collection’s internal representation.

The client can move through the collection without needing to know whether the collection is stored as an array, list, tree, or another structure.

The pattern separates two concerns:

  1. The collection stores and organizes the elements.
  2. The iterator controls the process of visiting those elements.

Different programming languages implement iteration differently, but the underlying pattern idea remains the same: traversal is separated from the collection’s internal structure.

10. Communication Patterns

Communication patterns organize how objects and components exchange information.

They can help:

  • Separate senders from receivers
  • Define communication paths
  • Encapsulate data in messages
  • Send information across a network
  • Avoid requiring every object to communicate directly with every other object

A message can be viewed like a letter: the sender places information into a transferable form, and another component receives and processes it. This approach is particularly important in distributed systems, where components may communicate across processes or machines.

Communication patterns are also relevant to MVC and broker architectures because these designs define which roles communicate and how information flows through the system.

11. Concurrency Patterns

Concurrency patterns address systems in which multiple activities can execute at the same time or appear to execute simultaneously.

Important examples include:

  • Locks
  • Monitor
  • Active Object
  • Future
  • Scoped Locking
  • Asynchronous operations such as async and await

A lock protects shared data or resources so that conflicting operations do not occur simultaneously.

A Monitor combines synchronization with controlled access to shared behavior or data. It helps coordinate concurrent execution.

An Active Object separates method invocation from method execution. A request may be accepted by one component and executed independently, often allowing the caller and execution process to proceed separately.

A Future represents a result that may not be available yet. The caller can continue working and obtain the result later.

Scoped locking ties the acquisition and release of a lock to a defined scope. When execution leaves that scope, the lock can be released automatically or systematically. This reduces the risk of forgetting to release the lock.

Asynchronous mechanisms such as async and await support operations that can continue without blocking the entire execution flow while waiting for a result.

12. Resource-Management Patterns

Resource patterns focus on acquiring, using, and releasing resources safely. Resources may include memory, files, connections, locks, or other limited system facilities.

A central danger is forgetting to release a resource. If resources remain held after they are no longer needed, the system can eventually run out of memory, connections, file handles, or other capacity.

Important resource-management ideas include:

  • Garbage collection, which automatically reclaims memory that is no longer needed
  • Leasing, in which a resource is held only for a limited period or under an explicit ownership arrangement
  • Scoped resources, in which acquisition and release are tied to a defined scope

The general goal is to make correct resource management part of the design rather than relying entirely on every developer to remember cleanup manually.

13. Other Important Patterns

Some patterns do not fit neatly into one broad category.

Memento

The Memento pattern preserves an object’s state so that the state can later be restored. This can support undo operations or state snapshots without requiring external code to understand all the object’s internal details.

Smart Pointer and Reference-Counting Ideas

A smart pointer, sometimes called a counted pointer, manages ownership or references to an object.

The central idea is to count how many references still point to an object. When no references remain, the object can be deleted. This removes some of the burden of manually deciding when an object should be released.

This approach is especially useful when multiple parts of a program may share an object and no single component can easily determine when the object is no longer needed.

Chain of Responsibility

The Chain of Responsibility pattern passes a request through a sequence of potential handlers. Each handler can decide whether to process the request or pass it to the next handler.

This avoids requiring the sender to know exactly which object should handle the request.

Interpreter

The Interpreter pattern represents and evaluates expressions in a language or grammar. It is closely related to an abstract syntax tree, which represents the structure of an expression as connected nodes.

The interpreter uses that structured representation to process or evaluate the expression. The pattern is useful when a system must understand and execute a small language, command format, or expression syntax.

14. Design Patterns, Idioms, and Levels of Design

Not all reusable design knowledge operates at the same scale.

Architectural Patterns

Architectural patterns influence the structure of an entire application or a major subsystem. Examples include:

  • Layers
  • Broker
  • Client-Server
  • Model-View-Controller

Classical Design Patterns

Classical design patterns influence several components or classes, but usually do not determine the complete architecture. Examples include:

  • Strategy
  • Decorator
  • Iterator
  • Command
  • State

Idioms

An idiom solves a small problem in a way that is specific to a particular programming language. Unlike general design patterns, idioms depend strongly on the language’s syntax, conventions, and features.

Examples include:

  • A Python-specific module-entry idiom using if __name__ == "__main__":
  • Language-specific boilerplate
  • Small resource-management or pointer conventions

A general design pattern should be independent of a particular programming language. If a solution is tied to one language, it is better described as an idiom.

15. SOLID Principles

Design patterns are guided by broader software-design principles. One important group is the SOLID principles.

Single Responsibility Principle

The Single Responsibility Principle states that a class should have one responsibility and therefore one primary reason to change.

A class that handles database access, user-interface presentation, logging, and business rules has several reasons to change. Separating those responsibilities makes the system easier to understand and maintain.

Open/Closed Principle

The Open/Closed Principle states that software entities should be open for extension but closed for modification.

A component should allow new behavior to be added without repeatedly changing its existing source code. The Strategy and State patterns can help achieve this by moving variable behavior into separate objects.

For example, the main state-machine structure can remain unchanged while new states or transitions are supplied through configuration or additional implementations.

Liskov Substitution Principle

The Liskov Substitution Principle states that objects of a derived class should be usable wherever objects of the base class are expected.

The derived object should preserve the expectations established by the base abstraction. If substituting a child object causes existing code to fail because the child behaves in an incompatible way, the design violates this principle.

Interface Segregation Principle

The Interface Segregation Principle states that clients should not be forced to depend on methods they do not need.

Interfaces should be fine-grained and focused. A client should receive only the operations appropriate for its role rather than being exposed to every possible capability of an object.

This supports the idea that interfaces should be easy to use correctly and difficult to use incorrectly.

Dependency Inversion Principle

The Dependency Inversion Principle encourages high-level components to depend on abstractions rather than concrete implementations.

Instead of directly depending on one specific class, a component can depend on an interface or abstract class. Different implementations can then be substituted without changing the high-level component.

This improves flexibility and makes it easier for another developer to provide a new implementation.

16. General Principles of Good Programming

Several general principles support both patterns and ordinary software design.

Decomposition

Decomposition breaks a large problem into smaller problems.

A large task may be too complex to solve directly. By dividing it into smaller parts, each part becomes manageable. For example, a large project can be divided into chapters, individual tasks, and smaller implementation steps.

The parts can then be solved one at a time and combined into a solution to the original problem.

Abstraction

Abstraction hides unnecessary detail and presents a simpler interface.

If an application needs to send a message over the Internet, the application developer generally does not need to understand every detail of the underlying network frames and headers. A networking abstraction provides a simpler operation, such as sending a message, while hiding the lower-level implementation.

Adapters, proxies, and facades all use abstraction to reduce complexity.

Decoupling

Decoupling reduces direct dependencies between components.

A tightly coupled component knows too much about another component’s implementation. A small change in one component may force changes or recompilation elsewhere.

Decoupling allows decisions to be delayed until runtime or until a later stage of development. Components can communicate through interfaces, configuration, messages, or intermediaries rather than directly depending on concrete implementations.

Reusability

Reusable design allows components and solutions to be applied in more than one situation. Patterns support reuse primarily at the level of design knowledge. They give developers a structure that can be adapted to different projects.

Simplicity

Simplicity means implementing what is actually needed without introducing unnecessary complexity.

A developer should think ahead enough to avoid obvious problems, but designing for every imaginable future requirement can lead to over-engineering. A pattern should be introduced because it addresses a real design force, not merely because the pattern is available.

Consistency with Expectations

Software should follow the conventions of its programming language, development environment, and surrounding project.

A developer who applies a personal style from one language to another may produce code that looks unusual or confusing in the new context. Code is easier to understand when it follows the conventions that other developers already expect.

The same principle applies to development tools. In a team that uses a particular integrated development environment, using a different tool may create unnecessary friction if it makes collaboration or shared workflows more difficult.

Easy to Use Correctly and Difficult to Use Incorrectly

A strong interface guides developers toward correct behavior. It should make common operations clear and intuitive while reducing opportunities for misuse.

This principle connects closely with interface segregation, abstraction, simplicity, and dependency inversion.

Avoiding Premature Optimization

Premature optimization means optimizing before the actual performance problem is understood.

Optimization can introduce complexity and make the design harder to maintain. The design should first meet its real requirements. Optimization should then be guided by actual needs and constraints rather than speculation.

17. Choosing and Combining Patterns

Patterns should be selected by balancing the problem’s forces against the pattern’s consequences.

For example, the Observer pattern and the Mediator pattern can both organize communication, but they do so differently. The correct choice depends on the required communication structure, the degree of coupling, the number of participants, and the performance consequences.

Similarly, a broker should not be used in every application. It is useful when an intermediary improves communication or decoupling, but it may be inappropriate when direct communication is simpler and the additional overhead is not justified.

A thoughtful design process asks:

  1. What problem is occurring?
  2. Is the problem recurring and sufficiently complex to justify a pattern?
  3. What forces or constraints make the problem difficult?
  4. Which patterns address those forces?
  5. What benefits would each pattern provide?
  6. What liabilities or overhead would each pattern introduce?
  7. Are the consequences acceptable for the specific requirements?
  8. Would the pattern create unnecessary complexity?

This approach prevents over-engineering and pattern misuse.

18. Patterns as a Shared Language for Knowledge

Patterns do more than organize code. They organize and transfer knowledge.

A pattern makes knowledge:

  • Explicit by writing the design experience down
  • Findable by publishing it in books, papers, conferences, or videos
  • Understandable by presenting it in a consistent structure
  • Applicable by explaining how it can be used in real situations

The final goal is application. Memorizing that a pattern exists is less valuable than recognizing when it fits a real design problem and evaluating whether its consequences are acceptable.

This is why known uses and consequences are so important. They transform a pattern from an abstract idea into practical engineering knowledge.

Comprehensive Review

A design pattern is a proven solution template for a recurring problem. It is described through a name, context, problem, forces, solution, consequences, and known uses. The problem is difficult because competing forces create trade-offs, and the consequences explain what is gained and what must be sacrificed.

The pattern movement originated with Christopher Alexander’s work in architecture and later became influential in software through the Gang of Four and the Pattern-Oriented Software Architecture community.

The major pattern groups include:

  • Wrapping patterns such as Adapter, Proxy, Decorator, and Facade
  • Creational patterns such as Builder, Prototype, and Singleton
  • Behavioral patterns such as Strategy, Command, and State
  • Architectural patterns such as Layers, Broker, Client-Server, Pipes and Filters, and MVC
  • Collection-related patterns such as Iterator
  • Communication patterns that organize message flow and decouple senders from receivers
  • Concurrency patterns such as Monitor, Active Object, Future, locks, and scoped locking
  • Resource patterns that manage acquisition and release
  • Other patterns such as Memento, smart pointers, Chain of Responsibility, and Interpreter

Patterns are supported by SOLID principles and by broader design principles such as decomposition, abstraction, decoupling, reusability, simplicity, consistency, and careful avoidance of premature optimization.

The most important skill is not recognizing a pattern name in isolation. It is making a reasonable design decision by matching a pattern to the problem, weighing its forces and consequences, and using it only when the resulting structure improves the software.

Ask anything

We've read this whole study set, so questions about it get the best answers.

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent