Join Jungwoo Ryoo for an in-depth discussion in this video Prototype example, part of Python: Design Patterns.
- The prototype class has four different methods, and we'll start with the init method. In the init method, all we're doing is simply creating this dictionary object. The dictionary object will be containing the objects that will be cloned. So let's create the dictionary object by typing self._objects, and then the second method is register object.
So, in this method, we'll be registering the object to be cloned. So this method takes two arguments. The first one is name, and then the second one is the object to be cloned. The name, in particular, is going to be used as a key when it comes to storing the object in the dictionary object. So, let's simply store the object we're receiving as argument into the dictionary object.
So, type self_objects. This is our dictionary. And then the key will be name, and, using the key, we'll be storing the object to be cloned. Next is the unregister object method. In this method, we'll be deleting the objects from the dictionary object. So, simply delete by using dl, and then self_object, and we want to delete a particular object using the key, in this case name.
Let's move on to the final method, which is clone, which is probably the most important method. So, this method is used to clone the prototypical object. So, first copy the object, so type obj. This is where the copied object, or cloned object, will be stored, and we copy it by using the deep copy method. So, the prototypical object is already stored in our dictionary object, so we're simply using it by saying self._objects, and then we need to get the object by using the key, name.
So, this is perfectly fine, especially when you don't really have to change anything in the object being cloned, but when you want to change some attributes of the object you are cloning, then it's nice to have this way of updating those attributes, which is obj.dict, so this represents all the attributes of the object, and then we'll simply update the object based on the attribute dictionary object we are receiving as an argument, so that's why we just type attr here, and the we'll be returning whatever object we just replicated.
Just say return obj, so now the definition of your prototype class is done. So, then moving onto the prototypical object we'll be cloning, in this case we'll be cloning this Car object. So, we have to define the class for the Car objects we'll be cloning, and we initialize each Car object using these attributes. For example, the name will be Skylark, the color will be red, and then the option we're using is Ex.
And then this string method will be returning the attributes of the object when we print the object. So, now let's actually use the prototype class we just created. We'll first instantiate the car class and put it in a variable, C, create the Car object. So this is the prototypical object to be replicated, or cloned, and then we'll create an instance of the prototype class, so type prototype, all lowercase, that will be our variable, and then we'll create the prototype object out of the prototype class.
And then we'll have to register that prototypical class, which is housed, now, in the C variable. So we'll just say prototype.register_object, and the key for the object is Skylark, and then the prototypical object is passed in the second argument, which is c, and then let's see if you can clone that object.
So, we'll put it in the variable c1, and we will clone it. So type prototype, lowercase, and then cull the clone method, and, in this case, what we want to clone is the one associated with the Skylark key, so that's why I used Skylark here, so now we just cloned that prototypical object. Time to print it.
So, just say print(c1). Let's build the program and see what happens. Go to Tools, click on Build, and it worked. As you can see, it's printing all the attributes of the cloned object.
- 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.