Join Jungwoo Ryoo for an in-depth discussion in this video Proxy example, part of Python: Design Patterns.
- Let's first define this Producer class, which is supposed to be more resource intensive when we're trying to instantiate it. So the Producer class simply print some of these messages. There are two methods defined. The first one is printing the current status of the Producer stating ('Producer is working hard!') and then the second method is simply printing a statement saying that Producer has time to now meet a guest.
So the goal here is not to bother the Producer until the Producer is available because the Producer is very expensive to instantiate. Next, let's define the Proxy class. The Proxy class is supposed to be relatively less resource intensive, especially when we're trying to instantiate it. And that's why we try to use this Proxy object as a middle man, so that we simply handle the request for the Producer until the Producer becomes available, and we can afford to do it because the Proxy class, the instantiated version of the Proxy class, or the object of the Proxy class is much less to create.
So let's see what happens in the Proxy class. So we have two attributes. One is occupied. So at the moment, the default value for that attribute is 'No'. And then we have another attribute that's supposed to be storing an instance of the Producer class. So we have this if statement, where we'll be checking if the Producer is available. So let's add the logic we need here.
So we have this if statement that says self.occupied == 'No', so if the Producer is not occupied, that is available, then that's when we'll be instantiating this expensive Producer class. So we simply say, self.producer assignment and then we are creating an object from the Producer class. And then we let the program wait a little bit, by stating time.sleep for (2) seconds.
And then since we now have the Producer object created, the Producer can meet the guest. So we can add self.producer meet, so we invoke the meet method on the Producer object, otherwise we're not going to be doing anything, because the Producer is not available. So let's test the code to see if the code works. For us to use the code, we need to instantiate a Proxy first, because the Proxy is really the one handling all the requests from the guest to see if the Producer object is available or not.
So we'll give a variable name p for Proxy. And then, we'll make the Proxy work until Producer is available. So that's what this statement is. p.produce. So we're actually invoking this produce method on the Proxy, not the Producer. And the Producer object hasn't even been created yet. And remember that the current state of the Producer is not occupied, meaning not busy, so in this case, when I invoke this produce method on the Proxy object, the Proxy object will be able to instantiate a Producer, and then relay the message from the guest to the Producer object.
So the end result will be the Producer being able to spend some time with the guest. So let's run the code, see if it works. Go to Tools. Click on Build. And as you can see the Producer is now available. Moving on to the next steps. Now let's try to change the state of the Producer occupied. p.occupied. And we'll set it to 'Yes'. And then we'll make the Proxy produce again.
And let's run the program again. Build. And now you see this message. Producer is busy! Remember the point of having this Proxy, because the Producer object is so expensive to create that's why we have this Proxy object handling the requests from the guests until the Producer object becomes available. So here, what I mean by expensive, is really being resource intensive.
So in this case, Producers are much more resource intensive to instantiate.
- 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.