Implementation
There are many ways of implementing the Adapter design pattern in Python [Eckel08, page 207]. All the techniques demonstrated by Bruce Eckel use inheritance, but Python provides an alternative, and in my opinion, a more idiomatic way of implementing an Adapter. The alternative technique should be familiar to you, since it uses the internal dictionary of a class, and we have seen how to do that in Chapter 3, The Prototype Pattern.
Let's begin with the what we have part. Our application has a Computer
class that shows basic information about a computer. All the classes of this example, including the Computer
class are very primitive, because we want to focus on the Adapter pattern and not on how to make a class as complete as possible.
class Computer: def __init__(self, name): self.name = name def __str__(self): return 'the {} computer'.format(self.name) def execute(self): return 'executes a program'
In this case,...