Get startedGet started for free

Factory methods

1. Factory methods

In this final lesson, we'll learn how to create factory methods, with the help of interfaces. Let's get started!

2. Conditional logic in a method

Chances are, you've probably seen a method with complex conditional logic like this before. In the Student class, explore_topic takes two parameters; resource_type, and topic. Depending on the resource_type, a message may be printed, before calling either the reference_textbook, reference_blog, or reference_video method. This code is tricky to read, and some of the logic is repetitive. Using the factory method design pattern, we can make this code less complex, easier to read, and more modular.

3. Factory methods

The factory method design pattern uses factory methods to create objects that will be used in another method. Here's the catch; all objects returned by a factory method must implement the same interface. Factory methods help to reduce complexity in a method, such as heavy conditional logic. They also make code more reusable and modular. It's standard to use an underscore to denote a factory method. Here is a quick peek at a factory method. But before we can build this, we need to start from the beginning, with products and concrete products.

4. Creating a Resource interface

We know all factory methods must return objects which implement an interface. When working with factory methods, this interface is called a product. Concrete products are the implementations of the product interface. These are the classes that will be instantiated and returned by a factory method. Here, the Resource class is our product. It has a single concrete method "reference", which must be implemented by all concrete products. Next, we've built the Textbook concrete product with a reference method. To create concrete products for Blog and Video resources, we could do something similar.

5. Reworking explore_topic()

The next logical step might be to do something like this. Depending on the resource_type, explore_topic will now create an object of a concrete product, before calling the reference method on that object. This helps to reduce some of the issues we saw before, but the code is still quite repetitive. We can do one step better.

6. Creating a factory method

Finally, the moment we've been waiting for! We've rewritten explore_topic, to use the _get_resource factory method. get_resource takes a resource_type, and returns the corresponding object of a concrete product. Now, the explore_topic method calls get_resource to retrieve the appropriate resource and store it in a variable. Since each object returned by get_resource is a concrete product of Resource, it must have a reference method, which is then called by passing in the topic. Look how elegant and simple this is; it's easy to understand what's going on in each method, and all the complex conditional logic has been removed from explore_topic. If we wanted to add another resource, it would be easy; we'd only have to update the get_resource method to return a different concrete product.

7. Using factory methods

Once we create a Student object, we can try using the explore_topic method to learn more about object-oriented programming using a textbook. This outputs a message, along with the resulting content. Want to change the resource we're using? Easy - sub in video for textbook, and a new message is output, this time with a different list of results.

8. Let's practice!

Factory methods are awesome - let's practice building and using a few of your own!