DEV Community

Cover image for Advance Builder Design Pattern — Java

Advance Builder Design Pattern — Java

Kareem Radwan on February 28, 2020

Introduction: Each design pattern comes to solve a well known problem. Builder Design Patter the Problem is : There is a class that has ...
Collapse
 
luccabiagi profile image
Lucca Biagi de Paula Prado

As an Study topic is really, really good! I really wish that when I was studying, I would have been able to read this...

Well, as @frmaglia sugested, on work you can use the Immutables lib or the polemic Project Lombok, we use it at the company that I work and it really saves development time.

Anyway, good article! Congratulations!

Collapse
 
kareemradwan profile image
Kareem Radwan

thank you very much for this reply.

I am thinking of writing a detailed article for every design pattern in advanced

Collapse
 
siy profile image
Sergiy Yevtushenko

Good article. Sad that Builder itself is anti-pattern in most cases, including one used by you in examples. dev.to/siy/when-builder-is-anti-pa...

Collapse
 
kareemradwan profile image
Kareem Radwan

Hi Sergiy, I am very happy for your reply.

But in the Builder Design Pattern:
1- The constructor can't must be private . so you can still constructor public and any developer use it and fill all require parameters.

2- I agree with you to the extent that error in compile time is better than an error in Run Time.
But in the case of my article, look at the last example. The developer cannot see the setMajor method except if it calls the setName method for example.
I think we can overcome the problem of forgetting some data without entering.

Collapse
 
siy profile image
Sergiy Yevtushenko
  1. This will result to inconsistent instance creation and increase coupling in places where 'new' is used to create instance.
  2. This is another pattern called "fluent interface". It works fine and solves all Builder issues. Unfortunately implementation involves a lot of boilerplate and does not make sense as general approach for POJO creation.
Thread Thread
 
kareemradwan profile image
Kareem Radwan

1- Yes, but this is written in Design patterns constructor can be optional private as I remember.

2- for point two you can use Immutables lib (immutables.github.io/immutable.html) and have it for free as some readers mention above. :).

Thank you very much, Mr Sergiy

Thread Thread
 
siy profile image
Sergiy Yevtushenko
  1. Design patterns are not a Holy Bible and always should be taken with grain of salt. Exposing constructor while having Builder or Factory/Factory method is a bad practice, as I've mentioned above.
  2. Yeah, tooling may help a lot to solve such problems.
Thread Thread
 
kareemradwan profile image
Kareem Radwan

Yes, I agree with you,
But I explain how Builder and Step Builder work.

thank you.

Collapse
 
frmaglia profile image
Francesco Maglia

You can avoid the hassle of doing it manually by using Immutables lib (immutables.github.io/immutable.html) and have it for free.

Collapse
 
orenovadia profile image
orenovadia

Does Intellij work well with this library?

Autocompletes, go to definition, etc...

Collapse
 
kareemradwan profile image
Kareem Radwan

oh, thank you very very much.

But in this article, I explain it to other students because I'm a student and I get this topic in university.

Collapse
 
proclus profile image
Ogla Sungutay

Hi Kareem, thanks for the article! It gave me inspiration for my Typescript project which uses inner classes.