A factory is a class for building other objects, where it creates and returns an object based on the passed parameters. Here, the client provides materials to the Factory class, and the Factory class creates and returns the products based on the given material. A Factory is not a design pattern, but it serves as a basis for the Factory Method Design Pattern.
Before getting into the Factory Method design pattern, let’s create a Simple Factory. In the below example, the SimpleFactory class has a static method called build_connection() to create objects based on the received parameter.
Let’s look into the output
Here, you provide the type of protocol as an argument, and the factory class creates and returns the object based on the argument. The point to note is that object creation is not the responsibility of the client.
Factory Method Design Pattern
Factory Method Design Pattern is identical to a simple factory, but its structure is complicated compared to a simple factory structure. It has an abstract class that contains factory methods (to create objects) and operation methods (to work with created objects). And, the concrete classes that create objects are derived from the abstract class.
This pattern helps to define an interface to create an object. Here the classes that implement the interface decide which class to instantiate. Hence, the operation methods in the abstract class need not worry about object creation until the product interface is implemented.
Note: If you are a beginner, I strongly recommend you to go through the Factory Method – Python Design Patterns.
Advantages of the Factory Method Design Pattern
Let’s look into the advantages of the Factory Design Pattern. A few of them are:
- It makes code more universal
- It separates interfaces from implementation
- It reduces the complexity of code maintenance.
Accessing Web Resources Using Different Protocol
Let’s implement a program for accessing web resources using HTTP or FTP protocol. Here, we will use the site ftp.freebsd.org that allows both HTTP and FTP protocol.
From the above diagram, we can understand that the design has an abstract class called Connector, and two concrete classes – HTTPConnector and FTPConnector. The two concrete classes are derived from the Connector class, and the factory methods in the abstract class use these classes for product creation. Let’s look into the code below.
In this program, we connect to a web page using the HTTP or FTP protocol. The Connector abstract class is designed to establish a connection (scan method), and as well as for crawling the pages (crawl method). In addition to this, it provides two factory methods – factory_protocol and factory_port – to deal with protocol and port address.
Let’s look into the output.
A factory class is used to create other classes, It makes the code more universal. The factory methods in factory classes act as an interface for creating an object, thereby it separates interface from implementation. As a result, classes can implement these interface methods and can decide which class to instantiate.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course