Design Patterns «Prev Next»

Lesson 3What Is a Design Pattern?
ObjectiveRecognize a design pattern as a reusable design description, not copied code or a guaranteed solution.

What Is a Software Design Pattern?

A software design pattern names a recurring design problem, describes the forces that make the problem difficult, and records a reusable arrangement of collaborating objects. It is a design vocabulary rather than a finished implementation. Developers still have to adapt the pattern to the language, constraints, and behavior of their application.

Patterns are useful because they make design intent visible. Saying that a service uses an Observer, for example, conveys more than saying that one object calls several others: it signals that observers subscribe to a subject and are notified when relevant state changes.

Gang of Four Patterns

Pattern, Algorithm, and Framework

ConceptWhat it providesWhat the developer supplies
AlgorithmA finite procedure for computing a resultInputs and an implementation in a programming language
Design patternRoles, collaborations, consequences, and implementation guidanceApplication-specific classes, policies, and trade-offs
FrameworkReusable executable code with extension pointsComponents that plug into the framework lifecycle

How Abstraction Supports Reuse

An abstract data type specifies operations independently of representation. A list, for example, may be backed by an array or a linked structure while exposing comparable operations. A design pattern works at a broader level: it abstracts a collaboration among responsibilities.

Consider Strategy. A context delegates a variable behavior to a strategy interface. The concrete strategies can change without forcing the context to contain every algorithm in one conditional block. The value comes from the dependency boundary, not merely from creating more classes.

A Pattern Includes Consequences

A pattern is not automatically the best choice. Applying one may add indirection, objects, configuration, or lifecycle concerns. A responsible pattern decision therefore answers four questions:

  1. Context: What part of the system is changing or difficult to couple safely?
  2. Forces: Which requirements compete with one another?
  3. Structure: Which objects own the responsibilities and how do they collaborate?
  4. Consequences: What flexibility is gained, and what complexity or runtime cost is introduced?

If a problem is simple and unlikely to vary, a direct implementation can be clearer than a pattern. Patterns earn their keep when their structure makes an important source of change easier to control.

Example: Traffic-Signal Messages

In the course project, different signal types display different messages for the same logical state. A common TrafficSignal abstraction can define the legal state transition, while TrafficLight and WalkSign supply their own messages. This is a small example of separating stable policy from behavior that varies.

The example does not prove that inheritance is always preferable. A later design might inject a message strategy instead. Comparing those alternatives is exactly the kind of trade-off analysis that pattern study is meant to develop.

SEMrush Software 3 SEMrush Banner 3