Join Jungwoo Ryoo for an in-depth discussion in this video Adapter example, part of Python: Design Patterns.
- Let's start by defining these classes whose methods will be used by our adapter class. So, we have this class called Korean, and it has an attribute name, which is set to Korean, and it has a method which simply returns a string object that says An-meyong. We also have the British class, which has the same attribute. However, in this case, the method name is different. So, the method name is speak_english, instead of speak_korean.
So, let's complete the definition of this class. So, here we will say self.name, which is set to British. And then, in the method, we'll be simply returning a string object. Because this is an English speaker, we'll be returning "Hello." Now, let's define the adapter class. This adapter class changes the generic method name to individualized method names.
Let's start by defining the init method. In the init method all we're doing is we're setting this attribute_object to the value of the argument we're receiving, called object. So, whatever instance of object we're receiving, it's going to be initializing _object. Now, the next part is probably the most important part, and this is the part where we add a new dictionary item that establishes this mapping between the generic method name, such as Speak, and the concrete method name, such as Speak Korean, or Speak English.
So, all you have to do here is adding this one line. self.__ dict,__.update, and then we'll be using this adapted method as our argument. So I'll just do copy and paste right here. Adapted method. So what this does is, it will be accepting a dictionary item which consists of a key and the value, and the key will be the name of a generic method, and the value will be actually the individualized method name.
Other than that, we're not changing anything in the attributes or the methods of the received object. So, that's why we're simply returning the rest of attributes and methods in the getattr statement here. So, we simply say return getattr (self._object) which holds an object instance.
And then ,attr. Now we're ready to test our adapter, so let's move on to the rest of the code. Here, we've declared our list called objects, and we'll be creating these new objects, and then adding those objects to the list called Objects, here. So, we'll first create the Korean object. Next, we'll be creating the British object.
And then we'll be appending these objects to the objects list. So, we'll cull objects.append method. However, when we are appending these objects to the objects list, we want to change the mapping between the generic method cull Speak to the individualized method names. So, that's why we are using adapter here.
So, the first argument of the adapter was the object itself, so in this case we'll be using the Korean object, and then we'll be providing that mapping between the generic and the individualized method names, speak=korean. - speak_korean So, the generic method name, Korean, when it gets invoked, it gets translated into speak_korean. And we'll do the same thing with the British object.
objects_append(Adapter (british, speak=)). Now, it's the British object, so that's why we say british.speak_english. Note that the equal sign here simply means it's a dictionary item which consists of the key, in this case speak, and the value, which comes next, like this.
Now we're finally ready to test our code. So, in this case, we'll be using forloop to invoke the individualized speak methods. So we're simply saying speak, using the generic method. However, the end result is the individualized methods getting invoked, so let's go to Tools, click on Build, and, as you can see, it works. Once you get the hang of how to use the adapter class, it could become really handy in certain situations.
- Understanding design patterns
- Best design practices: consistency, completeness, and correctness
- Working with creational patterns
- Working with structural patterns
- Working with behavioral patterns
Skill Level Intermediate
Q: In the strategy pattern example, why does the code keep executing the default function rather than the alternate?
A: The programming demonstration skips the step of defining what the strategy pattern should be when an alternate function name is provided
as an argument as shown below.