Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Returning User-Defined Objects With Factory Patterns

00:00 In this lesson, we’ll look at how you can use the factory pattern to return user-determined objects. The factory pattern defines an interface for creating objects during program execution in response to conditions you can’t predict when writing your program.

00:17 You write a factory of user-defined objects with a function that takes some initialization arguments and returns different objects determined by input during the program execution. For example, in a painting program, you need to create different types of shapes in response to the user’s selection.

00:37 They might select to make a square one time, then a circle, then a rectangle, and so on and so forth. So the function you write for them to create something will have to return a different type of object each time they use it. So, what’s involved?

00:54 Well, first we have to define classes for all the shapes our program will use. Here, we’re just providing skeleton classes for Circle and Square.

01:03 We don’t care about their implementations beyond their initializers for this particular example. And, of course, we would have other classes—Triangles, Rectangles, and so on—defined as well.

01:15 Then you write a function that takes as its first positional argument a string to provide the name of the shape to be created. Then it would take any additional arguments that might be needed to create that shape.

01:29 It uses a dictionary to match each possible shape name string with the actual class name it relates to. Then the proper class name is found by looking up its shape name key and the remaining arguments given to this function are provided to that shape class’s initializer. That new shape object is then returned to the calling environment. Here’s how it would all fit together.

01:58 First are the simple Circle and Square classes. Both of these initializers just happen to take a single argument, but you can imagine other shape classes with initializers that need more than one. Here’s our shape_factory() function.

02:14 It takes a first positional argument for the type of shape to create, and then any additional arguments that would be needed to create that shape with that shape’s initializer.

02:27 Since we’ve only defined two shape classes, our dictionary only needs two key-value pairs, each of the form string—key, class name—value. As we create additional shapes that the user can select, we would extend this dictionary to include them.

02:48 When I call this function to create a shape, we look up the shape name string in the dictionary, then use the class name it maps to to create type of object in our return statement, passing the additional arguments to its initializer and then returning that newly created shape. For example, if we specify a shape_name of "circle", it’s as if this whole phrase is replaced with the name Circle, and then its initializer using the remaining parameters.

03:28 Now you can use this shape_factory() function to create shape objects as your users request them. Let’s import our shapes file. Here we can create a circle.

03:46 We can verify that it is a Circle

03:50 and examine its radius. Likewise, we can create a square,

04:07 see that it is a Square object, and find out the length of its sides.

04:14 We don’t need to use keyword arguments. We could supply the radius positionally as well.

04:34 So, there’s a bigger circle. There’s how you can return different types of objects from a single function. Next, we’ll look at how return statements work when used in try/finally blocks.

Become a Member to join the conversation.