What Will You Learn?

  • What are software design patterns?
  • What are the use cases for software design patterns?
  • What are common types of software design patterns?
  • How do I choose the right software design pattern?
  • Where can I learn more about software design patterns?

The Basics

Learn design patterns to accelerate software development. A design pattern considers languages and frameworks as its components. Design patterns help you solve a problem with many software languages and frameworks.

Each design pattern is a three-part rule expressing a relationship between a specific context, problem, and solution. It is a solution to a recurring problem within a particular context.

graph LR A[Three Part Rule] --> B[Context] A --> C[Problem] A --> D[Solution]

High-quality patterns define the consequences of using them and work with many languages and frameworks.

Software design patterns are a map and not a prescription. Patterns help define the why and how within your context, but not when to apply them.

Patterns used in the wrong context at the wrong time are anti-patterns. Identifying and eliminating them is a primary responsibility of software developers.

Primary Use Cases

  • Save time
  • Quickly communicate ideas
  • Solve recurring problems

Purpose

Understanding a design pattern’s purpose first is an effective approach. Its purpose should reflect what it does.

Types

There are three types of design patterns. Creational, structural, and behavioral.

graph LR A[Types of Design Patterns] --> B[Creational] A --> C[Structural] A --> D[Behavioral]

Creational

Creational design patterns manage object creation. A basic form of object creation can result in design problems. Creational design patterns address this by controlling object creation.

An example is the builder pattern which separates the construction of a complex object from its representation.

Imagine you are building an itinerary. When building the itinerary, you’ll need a car, a hotel, a flight, etc. Use the builder pattern to acquire a car, a hotel, and a flight with different workflows.

sequenceDiagram participant Itinerary participant Car participant Hotel participant Flight participant Itinerary Note over Itinerary: Builder Itinerary->>Car: Add Car Workflow Itinerary->>Hotel: Add Hotel Workflow Itinerary->>Flight: Add Flight Workflow Itinerary->>Itinerary: Build Itinerary

Structural

Structural design patterns ease a design by identifying a simple way to realize entity relationships.

An example is the module pattern. It is the most common structural pattern in everyday software implementations. The module pattern is a fundamental concept of a language. For example, in Python, any .py file is a module, along with Terraform, where each .tf file is a module.

A module is self-contained and runs independently of other code.

graph LR A[Parent Module] --> B[Child Module 1] A --> C[Child Module 2] A --> D[Child Module 3]

Behavioral

Behavioral design patterns identify common communication patterns. These patterns increase communication flexibility.

An example is the strategy pattern which adjusts data type behaviors.

Imagine your program accepts multiple payment types. When someone pays with PayPal, your strategy differs from paying with American Express.

classDiagram class Context { operation() } Context -- Strategy : Use payment Strategy class Strategy { execute() } class ConcreteStrategyA { execute() } class ConcreteStrategyB { execute() } class ConcreteStrategyC { execute() } Strategy <|-- ConcreteStrategyA : Use PayPal Strategy <|-- ConcreteStrategyB : Use American Express Strategy <|-- ConcreteStrategyC : Use Bitcoin

The strategy pattern simplifies adding new payment types with different concerns.

How To Choose

Choosing an appropriate design pattern is a crucial design decision.

Follow these steps to simplify design pattern selection.

  1. Thoroughly read its description to form an overview.
  2. Identify the design problem it solves.
  3. Learn what principles it follows.
  4. Study how it relates to other patterns.
  5. Identify code change needs and how it applies.

Conclusion

While software design patterns aid with design choices, be judicious with their use.

Follow this checklist to validate its usage.

  1. Is the pattern appropriate for your context?
  2. Does the problem it solves repeat across space and time?
  3. Does the pattern solve your problems, or does it introduce new problems?

Software design patterns pack a serious punch. With power comes great responsibility, so strive to understand a pattern before using it.

Best of luck on your endless journey with software design patterns!

Learn Software Design Patterns - Beyond the Basics


Other Learning