Join Jungwoo Ryoo for an in-depth discussion in this video Builder example, part of Python: Design Patterns.
- In this exercise, the goal is to build a car object and to print the details of the car object. We'll do so by using the various participants of the builder pattern including Director, Abstract Builder, Concrete Builder and object being built. The Abstract Builder class called Builder here creates a car object and keeps it as its attribute as you can see here.
The Concrete Builder class called SkyLarkBuilder that inherits from the Abstract Builder class but provides methods to be used by the Director class eventually. Let's add one more method in the Concrete Builder class which is the add_engine method. So d-e-f add_engine, we still need self and define it as self.car.engine and we set it as Turbo engine.
Now, the next part is to go back to the Director class and then complete the definition of construct_car method here. The Director is the one that's actually building the car. First of all, you need to create a new car object to get started. So type self._builder, so this is actually where the Concrete Builder object is stored, create_new_car.
Then we'll add the model, builder.add_model. The model name is Skylark, we'll be adding the tires text. Finally, we'll be adding the engine. So we're done with actually building the car in the Director class. Since we are now done with creating all the classes necessary to build the car object, it's time to move on and build a car using these classes.
To allow the Director to orchestrate the process of building a car, let's create a Concrete Builder first here. Type builder, the name of the Concrete Builder is SkyLarkBuilder. The Director then takes the Concrete Builder and uses it, therefore, type director which will be holding the Director object but Director object will be taking the builder as its argument.
Now we can construct the car by invoking the construct_car method so type director. construct _car. Finally, Director gets a reference to the car object by invoking the get_car method. The way we invoke it is director.get_car and whatever we're getting from this method we put it in the car variable.
Now, we are ready to print the car object. So let's print it. Let's test it, under Tools, Build. We are missing actually the method definition here so we need to actually return the object we're building, in this case the car object, so here you simply add one more method simply that returns the car object, return self., whatever is stored in the Concrete Builder, in this case builder.car, that's what you'll be returning.
Now, let's rerun the code. And it works. As you can see, the builder pattern simplifies the process of building complex objects. This is helpful especially because it makes the process of creating similar objects much, much easier and cleaner. All you have to do is simply add another Concrete Builder. For example, we could add another type of car.
- 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.