Listen to this post
Learn design patterns for fun and profit! Design patterns are a useful way to solve a problem once with many languages. A design pattern looks at languages and frameworks as components of a design pattern.
Each design pattern is a three-part rule that expresses a relationship between a certain context, a problem, and a solution. It is a solution to a recurring problem within a certain context.
The more versatile and valuable design patterns can be applied to many diverse languages and frameworks and frame the consequences of using the software design pattern.
Quality software design patterns are named for the concrete problem they are attempting to solve with a template or algorithm.
Software design patterns are a map and not a prescription. Patterns help with defining a why and how within your context, but not when to apply them.
Patterns used in the wrong context and at the wrong time are described as anti-patterns. Identifying and eliminating anti-patterns is a primary responsibility of software developers.
Primary Use Cases
- If you like saving time, you’ll like design patterns
- Quickly communicate ideas with your colleagues, and speak the same language despite knowing different underlying tools
- Solve recurring problems
When creating a design pattern understanding the purpose is where to start. The purpose reflects exactly what the pattern does.
Types of Design Patterns
There are three types of design patterns. Behavioral, creational, and structural.
In software engineering, creational design patterns are design patterns that deal with object creation mechanisms, trying to create objects suitable to the situation. The basic form of object creation could result in design problems or added complexity to the design. Creational design patterns solve this problem by somehow controlling this object creation.
An example is the builder pattern that is used to separate 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. Using the builder pattern allows you to acquire a car, a hotel, and a flight with different workflows.
In software engineering, structural design patterns are design patterns that ease the design by identifying a simple way to realize relationships among entities.
An example is the module pattern. It is the most common structural pattern you will see in everyday software implementations. The module pattern is often employed as a fundamental concept of a language. In Python, any .py file is a module, along with Terraform where each .tf file is a module.
A module is a self-contained set of code that can run independently of other code.
In software engineering, behavioral design patterns are design patterns that identify common communication patterns among objects. By doing so, these patterns increase flexibility in carrying out communication.
An example of a behavioral pattern is the strategy pattern which is used to adjust how you deal with different classes of data.
Imagine your program accepts multiple payment types. When someone pays with Paypal, your strategy class is different than paying with American Express. Luckily your payment strategy can be achieved with different payment types, and adding a new one is as simple as adding another class.
How To Choose
Choosing the appropriate design pattern to use within your given context and at the right time is a key decision point. Applying the following methodology will help you choose the right software design pattern at the right time.
- Read the software design pattern completely from beginning to end to overview the pattern’s usage.
- Consider how candidate software design patterns solve your design problem.
- Identify what the intent of the design problem is and what it actually does.
- Learn what principles the design pattern is based upon
- Study how your candidate software design patterns relate to other patterns
- Consider what parts of code you want to be able to change without having to redesign your solution
Developing enough software will make you a pattern recognition master. Pattern recognition isn’t limited to software design; we are built at a psychological level to identify patterns to predict the future and stay alive. Identifying, and using software design patterns, might not save your life, but it could save your project.
While software design patterns seem like a surefire way to make the right design choices, be judicious with their use.
Follow this checklist to ensure you’re using software design patterns in the right way.
- Is the design pattern appropriate for your context?
- Does the problem you’re facing repeat across space and time?
- Does the design pattern solve your problem, or does it introduce numerous new problems?
Software design patterns pack a serious amount of power. With that power comes a great responsibility to understand the pattern you are using before you employ it.
I wish you the best of luck with your endless journey with software design patterns!
Learn Software Design Patterns – Beyond the Basics
- Mastering Python Design Patterns: A guide to creating smart, efficient, and reusable software, 2nd Edition (Amazon)
- Design Patterns: Elements of Reusable Object-Oriented Software (Amazon)
- Microservices Patterns: With examples in Java (Amazon)
- Head First DesignPatterns, 2nd Edition O’Reilly
- Software Architecture DesignPatterns in Java (O’Reilly)