O'Reilly logo

Learning Python Design Patterns by Gennadiy Zlobin

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Implementation in Python

For this example, we will create an extremely simple implementation of several Unix commands: ls, touch, and rm. We are going to use them in our shell, which we will write sometime later. The killer feature of the shell will be the possibility to undo all the operations executed since the shell started. Let's use the Command design pattern to do it.

In this example, we will use the abc module that provides the abstractmethod decorator to make methods abstract, so that this method should be implemented in derived classes to instantiate them. Also, we need to assign abc.ABCMeta to the __metaclass__ class attribute to let the abstractmethod decorator work. More information can be found in Python's standard library documentation ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required