Design problem while using Builder pattern

Q

qazmlp

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?
 
B

BarryNL

qazmlp said:
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?
 
I

iksrazal

(e-mail address removed) (qazmlp) wrote in message
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top