Builder Design Pattern in Python with Fluent Interface
Tags: designpatterns, python, builderdesignpattern, programming
Published: 2023-05-09 14:48:37

In the article [Strategy Design Pattern in Java with Enum](https://dev.to/pablocavalcanteh/strategy-design-pattern-in-java-with-enum-nc7), it was introduced what Design Patterns are and also discussed one of them with an implementation in Java with Enum. Now, in this article, a very useful design pattern implemented with Fluent API or Fluent Interface will be covered.

## Builder

Builder is a design pattern from the creation category whose intention is to separate the construction of a complex object from its representation so that the same construction process can create different representations of the object. This pattern is applicable when:

- algorithm for creating a complex object must be independent of the parts that make up the object and how they are assembled.

- construction process must allow for different representations for the object that is constructed.

## Generic structure

That's Strategy’s generic structure:

![Strategy’s generic structure](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dcacwzg67hrav2huq42l.png)

Let’s talk about each of the components (or participants):

- **Builder**: specifies an abstract interface for creating parts of a product object.
- **ConcreteBuilder**: builds and assembles parts of the product by implementing the Builder interface. It also defines and keeps up the representation it creates and provides an interface for product retrieval.
- **Director**: constructs an object using the Builder interface.
- **Product**: represents the complex object under construction.

## Builder with Fluent Interface

As mentioned in the article [Strategy Design Pattern in Java with Enum](https://dev.to/pablocavalcanteh/strategy-design-pattern-in-java-with-enum-nc7), one of the characteristics of design patterns is customization. There are some ways  which it is possible to implement the Builder, and one of them and maybe the coolest is with Fluent Interface or Fluent API. 

This time, the Python programming language will be used. An important detail is that a Director class won’t be created, in other words, the pattern will be applied without this component.

The figure below is the abstract Builder class. To make it abstract and therefore non-instantiable, **metaclass=ABCMeta** is used. The **@abstractmethod** annotation is complementary to the ABCMeta class and defines that the method is abstract. From the Builder class, it is noticeable that to build a sandwich, we can compose it in many ways. For example, you can make a sandwich with just bread, lettuce and tomato. Or we can do it with all the components that the Builder class allows us to add.

![Builder class](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/416bam8hlkes3x6xedzs.png)

This is our Product class, that is, the complex object that will be built.

![Product class](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xuv3uquh1zm85hhrhb40.png)

And this is ConcreteBuilder. Several Builders could be created for different ways of composing the sandwich, but here we opted for only one class in order to explain the Fluent Interface later.

![ConcreteBuilder](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mf6vhvh55o02618yzy8z.png)

Finally, here is the execution of the design pattern with Fluent Interface. Notice that using the same builder, it was possible to create two different sandwiches.

![Execution](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yacndhyir6rx9lxqrm6e.png)

Uuuhmm, made me hungry! Well, that's it! See you guys! 👋👨‍💻