Standard Builder design, why should I use it?

Asked

Viewed 528 times

2

This example of design pattern can be found mainly in the book Design Patterns Elements of Reusable Object-Oriented Software, a widely known book both in the academic field and also its content is widely used in the labor market.

What I want to know is this:

  1. How would be the best way to apply this pattern?
  2. Why should I use this pattern? When should I use it? Why is it good?
  3. How would be a wrong way to make this pattern?
  4. There are flaws in this pattern?
  5. Something else to add?
  • 1

    Could add the Wikipedia link on this pattern to provide more context?

  • 4

    The wikipedia article cited in the question is not a good example on the Builder standard.

  • 3

    For me, this code of Converter looks more like the strategy standard. To wikipedia in English has a better example of Builder

  • 1

    Did the answer resolve what was in doubt? Do you need something else to be improved? Do you think it is possible to accept it now?

  • @Could Leonardolima show why it is not a good example and what would be a good example for it? This would answer question 1 and 3.

2 answers

9


How would be the best way to apply this pattern?

In general such questions are open and often directed by opinions. But I would say that it applies better by following a reliable source (already demonstrated in the comments that Wikipedia in Portuguese is not).

Do you want an opinion? I’ve never seen a need for it, at least in this form, but it’s just my experience. But there is one my question that gives an example where it might be necessary. There are more explained examples.

Possible reliable sources:

It is very easy to confuse it with other patterns (incidentally almost all patterns are very similar among some groups).

Why should I use this pattern? When should I use it? Why is it good?

Use when you have very complex objects and they need to be built in different ways before using it, usually built in steps. But not only that, it only makes sense if you have several objects that have the same basic construction mechanism, but with different implementation details. I don’t think it’s good, it’s really bad, but if you have that need then you have to use it and this shape was what they thought was best, I don’t know if it really is.

Something similar to it may be more useful in some cases where you just create the construction separately in a simple way just to separate the responsibility and allow the construction in steps without conforming to a pattern. Even in such cases it is only useful if the construction is very complex.

How would be a wrong way to make this pattern?

Hard to answer that, there are literally millions or billions of different ways to do it wrong. And I don’t even know if it would be wrong. I always say you should do what you do best, even if it violates some pattern.

Not to be confused with Method Chaining which is a very different pattern but which is used for a construction, and can even be used together with the Builder. This I see being much more used (I don’t even know if I should, but for language failures it has been common).

There are flaws in this pattern?

Certainly, unfortunately, I’m no expert on him to say, I hope someone can. What I can say is that it always adds a lot of complexity, needs to compensate a lot to use it, for me is the biggest flaw. And it certainly doesn’t solve every situation.

Something else to add?

Always has, but nothing that I think important. remembering that this is the very little used pattern and by very few dominated.

  • I found the answer interesting, but I thought I could give a few more details, for example "I never saw the need for it, at least in this form". or why the X form would be more used and the Y you see no use for. In the case the way it is in Wikipedia would not be a determining factor, so I will remove the question link.

  • 1

    I’m gonna go check this stuff out. Although it seems to me that the amendment in your question misstates it and my answer is outdated because of that, I will have to delete parts of the answer because it erased parts of the question, and then people will not have that information. Or I will leave those parts and refer to the old version of the question. I mean I can’t remember the context any more than I was thinking when I answered.

  • Well, I just deleted the link from Wikipedia, actually the idea was never to evaluate what was there, but rather a complete view of the Builder design pattern. So much so that the content there can be updated at any time and can make you lose sense anyway. So I thought it best to remove the link from Wikipedia.

  • The whole beginning of my answer was based on that. And then part of what you ask now makes no sense. Including because the right way to do it is in the links I’ve gone through. I don’t even know what more details would be needed here.

  • Got it, well I think that without Wikipedia it will get much better. Why then you could speak in a general way, and not just the example from there. But if you want you can use the example from there.

0

A complement to Maniero’s response, using a metaphor.

First the definition, removed from the site Medium:

The Builder pattern is part of the creative patterns, where it has the same the separation of the construction of a complex object from its representation, so that the same construction process can create different representations, based on rules and parameters that are informed to the object responsible for the construction.

Imagine you need to make breads. For this you will need to create the object Pao. To make a simple bread you will need flour, water, yeast and salt, but if you want to make a bread with filling you will need more ingredients.

The solution is to extend the class Pao and create subclass sets to represent the parameters. However, any new filling will need to increase the amount of parameters more and more. To avoid the passage of various parameters that will not be used, because to make a simple bread you do not need to put filling, The Builder pattern suggests that you extract the building code of the object out of its own class and move it to separate objects called Builders.

The pattern organizes the construction of objects in a series of steps, for example, inserirCamadaPresunto(), inserirCamadaMussarela()..To create the objects you perform these steps in a Builder object. The important part is that you do not need to call all the steps. Just call the ones you need for the recipe you’re making.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.