Adapter Pattern
The adapter design pattern applies to any context where we effectively want to modify an existing class so that its methods match those a related, but different, class or interface.
One general way to apply the adapter pattern is to define a new class in such a way that it contains an instance of the existing class as a hidden field, and then to implement each method of the new class using methods of this hidden instance variable.
By applying the adapter pattern in this way, we have created a new class that performs some of the same functions as an existing class, but repacked in a more convenient way.
An example is to implement an Array based Stack using a Python list
- with a simple example shown below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
|