Builder desgin pattern

Helo everybody,

today I want to say few words about another design pattern: Builder.

And that probably one of design patterns which I describe in words without using UML schema.

As name implies, Builder is intended for building something. And what exactly it builds? It builds classes. Think about your life, when you build castle from the send on the seashore or oceanshore, do you pay for proffesional castle sand builder? Well you can do it, but as usually people play with it by themselves and don't pay for some kind of proffesional castle sand builder.

The same is situation with classes instantiantion. If you have some kind of class, which has one, two, three paramethers, as usually you will not care about is it easy or hard to create it. You'll just craete it and use it. But in case if you class has 10 paramethers, and should be used in five or ten places in your code, then just typing those 10 names will be in the best case boring, in worst case proned to errors. So what is logical solution of this puzzle? To create class, with mehtod which will create for you instanceo of the class with complicated constructor, and then return you created class. 

Class creator as usually is named builder. 

But as often happens, not everything in life is trivial. What if your builder needed for creating some kind of hierarchy of objects? You can define abstract class for builder and needed implementations for creating instances of complicated hierarchy. 

Then later in code you can create your class with abstract builder, which you can link to needed implementation and make your life simpler.

No Comments

Add a Comment