Join Jungwoo Ryoo for an in-depth discussion in this video Abstract factory example, part of Python: Design Patterns.
- We'll start with the object that will be returned by our concrete factory, in this case, the dog object. We need to define the dog class to be used to instantiate the dog object. So the usual method where we return woof as a string. Next is the dog factory, and this is our concrete factory. And the concrete factory returns two objects.
In this case, the first object we're returning is the dog object, and the second object we're returning is the dog food object, which is simply, a string object. So let's complete this code by typing return, the name of the class, dog, and then we'll be returning the dog food string object, so return, dog food. The next class is the pet store class, which will be housing our abstract factory, actually, an instance of our abstract factory.
So we'll have to have this attribute of the pet store which is called _pet_factory, which will be also storing an instance of this concrete factory class. The concrete factory class we're talking about in this case, is dog factory, so let's initialize our attribute by first creating the variable or the attribute of the pet store class.
So we'll simple name it as self._pet_factory. We'll give it a space, and then we'll assign this argument we're getting from the init method, which is pet factory, as you can see, so we'll simply say pet_factory, and that's all we need to store the instance of the concrete factory class, in this case, dog factory.
And this _pet factory attribute is our abstract factory in this case. Next is the method that simply shows all the details of the objects we're receiving from the concrete factory, so this is simply a utility method that will display all the details of the objects returned by the dog factory, which is our concrete factory. So we'll need a variable, pet, and we'll be storing, for example, the first object we're receiving, so self_pet factory, and we'll be getting our pet, which is dog.
Next, we'll also get the pet food object, so we'll say pet food assignment self_pet factory. Now we're getting pet food. So now our utility method is complete because we have all the necessary print statements here, which will be printing all the details. Now let's move on to the part where we're actually using all these classes we just defined.
First, we'll be instantiating this concrete factory which would be used by the abstract factory eventually. So that's why we say factory, which is our variable, assignment, and we are simply instantiating the concrete factory. Next, we'll be creating a pet store, which is housing our abstract factory.
So we just say shop, which is out pet store housing the abstract factory in its attribute, and we instantiate the pet store. However, in the process, we're getting the factory, which is the instance of the concrete factory, as an argument here. Then, once we have our shop object, we can invoke this utility method we just created by saying show pet, and that's all there is to it.
So let's try to run the code and see what happens. Go to Tools, click on Build, and as you can see, the code worked beautifully. In addition to this concrete factory called dog factory, if you want to add another concrete factory, such as cat factory, adding that new concrete factory is not a big problem. That's one of the reasons why we use this pattern, abstract factory.
- 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.