Summary

Summarize the concept explored in this chapter regarding common design patterns in Python.

We'll cover the following

Recall

The world of software design is full of good ideas. The really good ideas get repeated and form repeatable patterns. Knowing—and using—these patterns of software design can save the developer from burning a lot of brain calories trying to reinvent something that’s been developed already. In this chapter, we looked at a few of the most common patterns:

  • The Decorator pattern is used in the Python language to add features to functions or classes. We can define decorator functions and apply them directly, or use the @ syntax to apply a decorator to another function.

  • The Observer pattern can simplify writing GUI applications. It can also be used in non-GUI applications to formalize relationships between objects that change state, and objects that display or summarize or otherwise use the state information.

  • The Strategy pattern is central to a lot of object-oriented programming. We can decompose large problems into containers with the data and strategy objects that help with processing the data. The Strategy object is a kind of plug-in to another object. This gives us ways to adapt, extend, and improve processing without breaking all the code we wrote when we make a change.

  • The Command pattern is a handy way to summarize a collection of changes that are applied to other objects. It’s really helpful in a web services context where external commands arrive from web clients.

  • The State pattern is a way to define processing where there’s a change in state and a change in behavior. We can often push unique or special-case processing into state-specific objects, leveraging the Strategy pattern to plug in state-specific behavior.

  • The Singleton pattern is used in the rare cases where we need to be sure there is one and only one of a specific kind of object. It’s common, for example, to limit an application to exactly one connection to a central database.

These design patterns help us organize complex collections of objects. Knowing a number of patterns can help the developer visualize a collection of cooperating classes, and allocate their responsibilities. It can also help developers talk about a design: when they’ve both read the same books on design patterns, they can refer to the patterns by name and skip over long descriptions.

Synopsis

This chapter discussed several common design patterns in detail, with examples, UML diagrams, and a discussion of the differences between Python and statically typed object-oriented languages. The Decorator pattern is often implemented using Python’s more generic decorator syntax. The Observer pattern is a useful way to decouple events from actions taken on those events. The Strategy pattern allows different algorithms to be chosen to accomplish the same task. The Command pattern helps us design active classes that share a common interface but carry out distinct actions. The State pattern looks similar to the Strategy pattern but is used instead to represent systems that can move between different states using well-defined actions. The Singleton pattern, popular in some statically typed languages, is almost always an anti-pattern in Python.

Get hands-on with 1200+ tech skills courses.