Design problem while using Builder pattern

Discussion in 'Java' started by qazmlp, Jan 31, 2004.

  1. qazmlp

    qazmlp Guest

    Builder pattern seem to suit my requirements perfectly except in one
    case
    as described below.
    I have used the same example diagram that is used in GOF Design
    pattern book, to explain the problem that I am facing.


    aClient aDirector
    aConcreteBuilder
    =============================================================================
    - new ConcreteBuilder-------------------------------------------->

    - new Director(aConcreteBuilder)-------->

    - Construct()--------------------------->BuildPartA()------------>
    BuildPartB()------------>
    BuildPartC()------------>

    - GetResult()---------------------------------------------------->


    There is no problem in calling BuildPartA(), BuildPartB(),
    BuildPartC() as a part of Construct(), for all the ConcreteBuilders.
    But, apart from that, one additional function(it is different for each
    ConcreteBuilder...different in type and number of arguments) needs to
    be called for which aClient has to pass different number&type of
    arguments depending on the ConcreteBuilder type.

    How do you overcome such a scenario?
    Does Builder pattern fit here? Or, do you suggest some other pattern
    for this?
    Do you find some flaw in the design? And, what are the improvements
    that you suggest here?
    qazmlp, Jan 31, 2004
    #1
    1. Advertising

  2. qazmlp

    BarryNL Guest

    qazmlp wrote:
    > Builder pattern seem to suit my requirements perfectly except in one
    > case
    > as described below.
    > I have used the same example diagram that is used in GOF Design
    > pattern book, to explain the problem that I am facing.
    >
    >
    > aClient aDirector
    > aConcreteBuilder
    > =============================================================================
    > - new ConcreteBuilder-------------------------------------------->
    >
    > - new Director(aConcreteBuilder)-------->
    >
    > - Construct()--------------------------->BuildPartA()------------>
    > BuildPartB()------------>
    > BuildPartC()------------>
    >
    > - GetResult()---------------------------------------------------->
    >
    >
    > There is no problem in calling BuildPartA(), BuildPartB(),
    > BuildPartC() as a part of Construct(), for all the ConcreteBuilders.
    > But, apart from that, one additional function(it is different for each
    > ConcreteBuilder...different in type and number of arguments) needs to
    > be called for which aClient has to pass different number&type of
    > arguments depending on the ConcreteBuilder type.
    >
    > How do you overcome such a scenario?


    You could always pass a HashMap containing the arguments.

    > Does Builder pattern fit here? Or, do you suggest some other pattern
    > for this?


    If you need to know which concrete builder you have then Builder
    probably doesn't fit. I'd need more concrete details to decide which
    pattern is really best - do you have concrete details, or is this just a
    homework assignment?

    > Do you find some flaw in the design? And, what are the improvements


    More details... what is actually being built here?

    > that you suggest here?
    BarryNL, Jan 31, 2004
    #2
    1. Advertising

  3. qazmlp

    iksrazal Guest

    (qazmlp) wrote in message
    <snip>
    > There is no problem in calling BuildPartA(), BuildPartB(),
    > BuildPartC() as a part of Construct(), for all the ConcreteBuilders.
    > But, apart from that, one additional function(it is different for each
    > ConcreteBuilder...different in type and number of arguments) needs to
    > be called for which aClient has to pass different number&type of
    > arguments depending on the ConcreteBuilder type.
    >
    > How do you overcome such a scenario?
    > Does Builder pattern fit here? Or, do you suggest some other pattern
    > for this?
    > Do you find some flaw in the design? And, what are the improvements
    > that you suggest here?


    Step back a second and try to makes things simpler.

    Typically you use the builder pattern when you are gathering the data
    for an object gradually before instatiating it. Explain why your
    scenerio fits that description. There are also factory method,
    abstract factory, memento and protype construction patterns defined in
    GOF, as well as delegation. Use what is the closest fit.

    As for different types and number of arguments, that's how OO helps
    you - do it dynamically and cast. the STL/Java vector type and
    countless others are exactly for that - defering type and how many of
    them until run-time. Hashmap containsKey() and containsValue() can be
    used in the builder constructor, for example.

    Hope that helps,
    iksrazal
    iksrazal, Jan 31, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. qazmlp
    Replies:
    2
    Views:
    512
    iksrazal
    Jan 31, 2004
  2. Phlip
    Replies:
    5
    Views:
    548
    Stefan Behnel
    Jan 13, 2010
  3. Pallav singh
    Replies:
    0
    Views:
    341
    Pallav singh
    Jan 22, 2012
  4. Pallav singh
    Replies:
    0
    Views:
    387
    Pallav singh
    Jan 22, 2012
  5. Pallav singh
    Replies:
    1
    Views:
    442
    Peter Remmers
    Jan 22, 2012
Loading...

Share This Page