Builder design pattern in Ruby

Image for post
Image for post
Builder pattern in Ruby

According to GoF and their great book “Design Patterns: Elements of Reusable Object-Oriented Software”, builder pattern:

„Separate the construction of a complex object from its representation so that the same construction process can create different representations.”

and it’s a part of an object creational design patterns.

Builder pattern is useful when the algorithm how to build an object is something independent of the parts that makes the object (methods that are building the object). That also defines another applicability of builder pattern: it’s helpful when there can be many ways of building complex objects.

Let’s start with some example and see how we can improve/refactor it by using builder patter.

Let’s assume we have that way of creating users:

There are few problems with that approach:

  • it doesn’t look professional ;)
  • we have very long list of params which limits us with the ways we can instantiate a new user. To do this, you can sent for certain params but it can look very messy soon,
  • adding new params will only make the situation worse,
  • logic how the object is build is hidden in its instantiation.

To extract the logic how the user is build we can use builder pattern here.

Our class should be simple:

and we want to instantiate new user by doing this:

We are telling our builder () how to build new user by giving him exact plan (represented by the block in that example).

Two benefits that are obvious here: no long list of parameters and algorithm how the user is build is outside class.

Let’s implement class.

has the knowledge how to build parts of the . It gives us some more digestible way of building new object( , , …) than just instantiation of from the first example (we don’t have to know about some low level detail of what parts build a user object).

You can have different plans how to create a new user (blocks passed to method) and you can be always sure that your builder () will know how to use your plan to build new object.

Want to know first about new articles from that blog?

Subscribe to my newsletter now! —

If you like this article and consider it useful for you, please support it with 💚

Image for post
Image for post

kkempin’s dev blog

Dev and life blog.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store