DEV Community

Pablo Cavalcante
Pablo Cavalcante

Posted on

Builder Design Pattern in Python with Fluent Interface

In the article Strategy Design Pattern in Java with Enum, 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

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, 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

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

Product class

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

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

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

Top comments (0)