Builder Design Pattern
bhaskarakster@gmail.com
What ?
• It Is a object creation software design pattern
bhaskarakster@gmail.com
Why?
• Introduced to solve problems with Factory and Abstract-Factory
patterns.
• if object creation is with ‘n’ number of arguments, then It is difficult maintain
order of arguments.
• Some times few arguments are optional, but need to send as null.
• Creation of complex object creation will fall in Factory classes.
bhaskarakster@gmail.com
Why?
• Issues with Factory patterns can be solved by creating constructors
with large number of required arguments and by having setter
methods for optional parameters.
• Problem with this is, object state would be not consistent until and unless all
optional parameters are set explicitly.
bhaskarakster@gmail.com
How?
• Builder pattern solves the issue in the following way
• Create static nested builder class by copying all the argument from outer
class.
• Create public constructor with required attributes and setter methods for
optional parameters.( Setter methods should return the builder class).
• Provide build method which returns desired class object, for this need to
create private constructor in desired class.
bhaskarakster@gmail.com
Example
bhaskarakster@gmail.com

Creational builder design_pattern

  • 1.
  • 2.
    What ? • ItIs a object creation software design pattern bhaskarakster@gmail.com
  • 3.
    Why? • Introduced tosolve problems with Factory and Abstract-Factory patterns. • if object creation is with ‘n’ number of arguments, then It is difficult maintain order of arguments. • Some times few arguments are optional, but need to send as null. • Creation of complex object creation will fall in Factory classes. bhaskarakster@gmail.com
  • 4.
    Why? • Issues withFactory patterns can be solved by creating constructors with large number of required arguments and by having setter methods for optional parameters. • Problem with this is, object state would be not consistent until and unless all optional parameters are set explicitly. bhaskarakster@gmail.com
  • 5.
    How? • Builder patternsolves the issue in the following way • Create static nested builder class by copying all the argument from outer class. • Create public constructor with required attributes and setter methods for optional parameters.( Setter methods should return the builder class). • Provide build method which returns desired class object, for this need to create private constructor in desired class. bhaskarakster@gmail.com
  • 6.