A design pattern is a reusable, somewhat language-specific solution to a common problem in software design. The most popular book on this topic is Design Patterns: Elements of Reusable Object-Oriented Software, Addison-Wesley Professional, written by Erich Gamma, John Vlissides, Ralph Johnson, and Richard Helm, also known as the Gang of Four (GoF). It is considered a major writing in this area and provides a catalog of 23 design patterns with examples in Smalltalk and C++.
While designing an application's code, these patterns help solve common problems. They ring a bell to all developers, since they describe proven development paradigms. But they should be studied with the used language in mind, since some of them do not make sense in some languages or are already built in.
This chapter describes the most useful patterns in Python, and patterns that are interesting to discuss, with implementation examples. The following are the three sections that correspond to design pattern categories defined by the GoF:
- Creational patterns: These are patterns that are used to generate objects with specific behaviors.
- Structural patterns: These are patterns that help structure the code for specific use cases.
- Behavioral patterns: These are patterns that help assign responsibilities and encapsulate behaviors.
In this chapter, you will learn what the most common designs are and how to implement them in Python. You will also learn to recognize problems that can successfully be solved using these patterns in order to improve your application architecture and overall software maintainability.