课程: Python: Design Patterns (2021)

Factory example

- [Instructor] Here I just defined my dog class. The dog class, whenever created, calls the init method, setting the name of the dog. It also has a method called speak that returns a string Woof! Now let's create a new method called get_pet. Type def, space get_pet, open parentheses. We're passing a key to indicate the pet we want. The default key is dog. So type pet, equal sign, double quotation marks, dog. And don't forget the colon. Let's define this function by adding some internal documentation first. Since this is our vector method we'll be using to create our object, we'll say The Factory Method. In The Factory Method, the mission is to create objects and return them to their user. We'll be storing those objects being created into a dictionary data structure called pets. That's why we use the keyword dict. So type pets, equal sign, dict, D-I-C-T, open parentheses. For now, we'll store one object instantiated from the dog class. So type dog, which is the key for the dictionary. Then a dog object instantiated by calling the dog class constructor, like this. Equal sign, Dog class, open parentheses. Every time we instantiate a dog object, we have to provide its name. In this case, the name is Hope. We'll be returning whatever object needed out of the dictionary object. Therefore, return pets but not the actual dictionary object but an item stored in the dictionary object indicated by the pet key value, pet. We've just provided the key, which is stored in the pet variable. Therefore, this will return an object that was stored in the dictionary object and also identified by the key provided. Now 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 already have this get_pet method, adding a new class is not a problem. All you have to do is to create a new class. Select the whole dog class definition and we'll just copy and paste. We'll call it Cat this time. So it's a simple cat class. Then in addition to changing the name of the class, we'll also return a different value when it comes to the speak method. We'll use meow because it's a cat. Now all you have to do is add a new instance of the cat in the dictionary object. By the way, the indentation of our get_pet method needs to be corrected because this is not part of the class. The key is cat. Cat, equal sign. We're instantiating a cat object from this cat class. Cat, open parentheses. We have to provide the name of the cat, which is Peace. Don't forget the double quotation marks. As you can see, adding a new class, cat, is not a problem at all because all you really have to do is add in this new object, instantiate it from the cat class into the dictionary object. Now let's see if this works. Let's invoke the get_pet method. First, I'll try to retrieve a dog object, which is why I'm using the dog key. I'm storing the dog object in this variable d, equal sign, get_pet, open parentheses, and then the key is dog. Just to test it, we'll print what the dog says. Type print, open parentheses, and then type d.speak. This will be invoking the speak method of the dog object. Let's see if this works. Go to Tools, click on Build. It does work. Now, let's see if the cat object can be instantiated from the cat class. Type c, equal sign, get_pet. Now instead of getting a dog, we're getting a cat. That's why I'm using the key cat. Now let's test it. All you have to do is print what the cat says. Just type print c.speak. Let's run the program. Go to Tools and Build. The script works. One thing I'd like to mention here is that since we have this get_pet method, which is our factory method, the addition of new pet 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 all these new types of objects being added. That's what is nice about the use of the factory pattern. The way the factory pattern is implemented here is slightly different from the factory method in a typical object-oriented programming language because we're trying to fully take advantage of all the built-in features of Python in this course.

内容