Join Jungwoo Ryoo for an in-depth discussion in this video Factory example, part of Python: Design Patterns.
- We just defined this Dog class. The Dog class, whenever it is created, calling this init method which is a setting, the name of the dog, and then it has a method called speak and it simply returns a string, "Woof!" Now, let's create a new function called get_pet. So type def get_pet. Now, we've passing this key value combination because whatever argument we're passing will be stored in a dictionary object.
We've passing this argument which is going to be used as a key in the dictionary object. The name of the variable is pet, we'll initialize that variable with dog, just a string, initial string. So the default value of pet is dog. Then we'll define this function by adding some internal documentation first. Since this is the factory method we'll be using to create our object, we'll simply say it, ***The factory method***.
In the factory function, the mission is really to create these objects and to return these objects to the user of the function. We'll be storing those objects being created into a variable called pets. Then we'll store the objects we'll be returning in a dictionary object, so that why we used the keyword dict. Then for now we'll simply store one object instantiated from the Dog class so say "dog" which is the key for the dictionary.
Then the object itself is instantiated by calling the class D-o-g. Then every time we instantiate the dog object, we have to provide the name, in this case, the name is Hope. Then we'll be simply returning whatever object we're getting out of the dictionary object, so return pets, but not the actual dictionary object but the item stored in the dictionary object. In this case, we've just provide the key which is stored in pet variable.
This will simply return an object that was stored in the dictionary object. We're done with our factory method. Now let's imagine a situation in which you have to provide a new object instantiated from a new class called Cat. Since we have this function get_pet, adding a new class to this method is not a problem. All you have to do is create a new class.
Simply choose the whole thing and we'll just copy it and paste it and we'll just call it Cat this time. Then in addition to changing the name of the class, we'll also return a different value when it comes to the speak method, so we'll just say "Meow!" because it's a cat. Now, all you have to do is simply adding a new instance of the Cat in the dictionary object.
In this case, the key is cat and then we're instantiating a cat object from this Cat class. We have to provide the name of the cat which is Peace. As you can see, adding a new class Cat is not a problem because all you really have to do is adding this new object instantiated from the Cat class into the dictionary object. Now let's see if this works.
Let's invoke this function by calling the get_pet function. First time, I'll try to retrieve a dog object which is why I'm using the dog key and that's why I'm storing it in this variable d. Just to test it, we'll print whatever the dog speaks.
Simply type print() and then type d. and we'll be invoking the speak method of the dog object. Let's see if this works. Go to Tools, click on Build. As you can see, it works. Now, let's see if the cat object can be instantiated from the Cat class. Type c =, assignment get_pet. Now, instead of getting the dog, we're getting a cat so that's why I'm using the key cat.
Now let's test it. All we have to do is just to print whatever the cat speaks. Just type print(c.speak()) and let's run the program. Tools, Build. As you can see, the script works. One thing I would like to mention here is that since we have this get_pet method, which is our factory method, the addition of these new types such as cat is really, really easy and as the user of this get_pet method or the factory method, you don't really see what's going on in terms of adding all these additional new types of objects, that is classes.
That's really the nice thing about the use of the factory pattern. The factory pattern, the way it's implemented here, is slightly different from the factory method in a typical programming language or object-oriented programming language because we are trying to fully take advantage of all the features of Python. Let me emphasize that it's now so easy to add a new type such as cat. This is one of the advantages of using this pattern called factory pattern.
The way we implement the factory pattern is slightly different from the conventional object-oriented programming language because we're trying to fully take advantage of the features of Python.
- 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.