So I have to create a lot of big objects for the project I am working on.
Looking for the most appropriate design pattern for my case, I searched the design pattern Builder and found many differents point of view, some contracting others.
Could someone explain the pattern, and what are the pro and cons of the pattern? maybe when to use it and when not? And maybe do you have other patterns that could work for manipulating big classes with more than ten properties?
Thanks a lot, it could really help me in my current work.
Hey, let me give this a shot.
There are two main use cases for the Builder Design Pattern
1.) The first use case is if you want to build a class object from a separate representation. For instance, you have an xml file or a json file that specifies some properties for your object. In this sort of scenario, you could use a Builder class to take the information from your xml file and turn it into an object of your class.
A practical example of this is say you have a CarShop class and a CarShop.xml file.
In your CarShop.xml file you have something like this that specifies details of the carshop and all the cars contained in that shop:
As shown above, the information in Carshop.xml specifies the properties of your Carshop class. In this scenario you would use a CarshopBuilder class to take the information from Carshop.xml and turn it into a Carshop object.
This introduces some level of flexibility into your program. Because say you have both Carshop.xml and Carshop.json representations of a Carshop, all you'd need to do is create two builders XMLCarshopBuilder and JSONCarshopBuilder. Both builders will work with their respective carshop representations and turn those representations into a Carshop object.
2.) The second use case for the Builder pattern is when you see that there are a lot of things and options required for the construction of your class. So say the constructor for your Carshop class looked something like this:
This is PHP code because that's what I'm more familiar with but I hope it translates nicely. Just imagine that the constructor for that class has a very long list of item and option parameters required to create the object.
In this scenario each time you wanted to create a new object of the class, you'd have to pass in all those items and options to the constructor. In this instance You could use a Builder class to help you make objects instead of directly calling the constructor.
So keeping with our Carshop Example. You could have Builder classes that create different types of Carshop objects. So from the example above we see that we are passing a Location object to the constructor. We could have an AthensCarshopBuilder that creates Carshops based in Athens. That way all Carshops built by the AthensCarshopBuilder will have their location set to Athens by the builder. Generally you'd use a builder class in this scenario to create objects of a class with specific configuration options.
Personally, I think this second use case of the Builder pattern might be an indicator that your class is doing too much and you should consider breaking it down.
You could also use the Builder pattern for classes with small amount of constructor arguments... it all just depends on what you're trying accomplish at the end of the day. You have to decide if it's easier for you to have a builder class that builds objects with specified configurations or if you'd rather create your objects and pass in the configurations by yourself every time.