• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Builder patern

 
Ranch Hand
Posts: 918
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, I use the builder pattern(see "the Gang of Four") to build some complex objects ,each of this classes has a complex constructor(with many parameters).
In my case the director needs a lot of paramters(from the client) and I pass them using the director constructor, but my director construcot gets too complex(to many paramters).
How i can solve this problem ?
P.S. :I try to group all parameters(in to container) pass it like this.
Note : The builder pattern use the director to contruct a object ussing a builder.When I use "director" I mean the builder pattern director.
ThanX!
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My first guess would be: encapsulate groups of related parameters into parameter objects.
Can you show us an example of such a constructor?
 
Mihai Radulescu
Ranch Hand
Posts: 918
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok.here is my builder :
class ToggleBuilder implements Builder{

private boolean label;
private boolean border;
private String [] labels;
private String representation = "toggle";
private JComponent [] components;
private int layout;
private Decorator decorator;
private ResourceBundle resourceBundle;
private ButtonGroup butGrp;
/**
* It sets the labels array but keep the component array.Each component is
* stored using a label.
*
* @param labelsArray <code>[] String</code> the label array.
*/
public void setLabels(String[] labelsArray) {
labels = labelsArray;
}
/**
* It sets the components array but keep the labels array.Each component is
* stored using a label.
*
* @param compArray <code>[] JComponent</code> the component array.
*/
public void setComponents(JComponent[] compArray) {
components = compArray;
}
/**
* It sets the components array and the labels array.Each component is
* stored using a label.
* @param labelsArray <code>[] String</code> the component array.
* @param compArray <code>[] JComponent</code> the component array.
*/
public void setComponents(String[] labelsArray, JComponent[] compArray) {
labels = labelsArray;
components = compArray;
}
/**
* It sets the border flag.If is true all your contoller will have a border
*
* @param withBorder boolean the border flag
*/
public void setBorder(boolean withBorder) {
border = withBorder;
}
/**
* It sets the label flag.If is true all your contollers components will
* have a label.
* @param withLabel boolean the border flag
*/
public void setLabel(boolean withLabel) {
label = withLabel;
}
/**
* It sets the title for this controller.The title is visible only if the
* border flag is set on true.
* @param title <code>String<code>the component title
*/
public void setTitle(String title) {
representation = title;
}
public void setResourceBundle(ResourceBundle inRb) {
resourceBundle = inRb;
}
/**
* It sets the layout.The layout constants are deffined in
* <code>om.oce.prismapro.guiengine.util.GuiEngineConstants</code>
* @param inLayout <code>int</code> the layout
* @see <code>com.oce.prismapro.guiengine.util.GuiEngineConstants</code>
*/
public void setLayout(int inLayout) {
layout = inLayout;
}
/**
* It sets the decorator for the model.
* @param inDecoarator
* @see <code>com.oce.prismapro.guiengine.util.Decorator</code>
*/
public void setModelDecoarator(Decorator inDecoarator) {
decorator = inDecoarator;
}
/**
* It sets a custom property.By ex if the controller has a button and this
* button must be in to button group(select policies).
* @param prop <code>Object</code>the new Prop
*/
public void setOtherPropertiy(Object prop) {
butGrp = (ButtonGroup)prop;
}
/**
* After all the settings (fianlly) it gets the controller.
*
* @return <code>AbstractGUICController</code>the conroller
*/
public AbstractGUICController getResoult() {
return new ToggleController();
}
private class ToggleController extends AbstractGUICController {
private GUIModel mainModel;
public ToggleController() {
init();
}
public GUIModel getModel() {
return mainModel;
}
public JComponent getView() {
return getRepresentation(representation);
}
private void init() {
mainModel = new ToggleModel(resourceBundle);
if(decorator != null) {
decorator.decorate(mainModel);
}
JToggleButton tgButt = null;
if(components != null) {
tgButt = (JToggleButton)components[0];
} else {
tgButt = new JCheckBox();
}
if(butGrp != null) {
butGrp.add(tgButt);
}
add(labels != null ? labels[0] : "toggle" , tgButt);
add(labels != null ? labels[1] : "toggleExample", new JLabel());
setBorderFlag(border);
setLabelFlag(label);
setLayout(layout);
}
}
}

and here is the director :

public class ToggleDirector implements Director{
public static final int RADIO_BUTTON_X = 0;
public static final int RADIO_BUTTON_Y = 1;
public static final int CHECK_BOX_X = 2;
public static final int CHECK_BOX_Y = 3;
private int buildMode = -1;
private String compName = "toggle";
//@todo: in the future the "Delegator" must be on the "Director",The same with otherProp
private Decorator decorator;
private Object otherProp;
private Logger log = Logger.getLogger(getClass().getName());
public ToggleDirector(int mode, String toggleName, Decorator inDecorator,Object propt) {
buildMode = mode;
compName = toggleName;
decorator = inDecorator;
otherProp = propt;
}
/**
* It constructs an object(conroller) using the Builder interface.
*
* @return <code>AbstractGUICController</code> the product.
*/
public AbstractGUICController construct() {
//unimplemeted for the moment
Builder tmpBuilder = new ToggleBuilder();
return construct(tmpBuilder);
}
/**
* It constructs an object(conroller) using a specific Builder interface.
*
* @param inBuilder <code>Builder<code> the builder for ussing
* @return <code>AbstractGUICController</code> the product.
*/
public AbstractGUICController construct(Builder inBuilder) {
//general settings
inBuilder.setBorder(false);
inBuilder.setLabel(false);
inBuilder.setModelDecoarator(decorator);
inBuilder.setOtherPropertiy(otherProp);
String [] labels = new String[2];
JComponent [] comps = new JComponent[2];
labels[0] = compName;
labels[1] = "toggleExample";
comps[1] = new JLabel();
String msg = "Buldind mode is : " + buildMode;
log.log(Level.CONFIG, msg);
switch (buildMode) {
case RADIO_BUTTON_X :
comps[0] = new JRadioButton();
inBuilder.setLayout(GuiEngineConstants.BOX_X);
break;
case RADIO_BUTTON_Y :
comps[0] = new JRadioButton();
inBuilder.setLayout(GuiEngineConstants.BOX_Y);
break;
case CHECK_BOX_X :
comps[0] = new JCheckBox();
inBuilder.setLayout(GuiEngineConstants.BOX_X);
break;
case CHECK_BOX_Y :
comps[0] = new JCheckBox();
inBuilder.setLayout(GuiEngineConstants.BOX_Y);
}
inBuilder.setLabels(labels);
inBuilder.setComponents(comps);
return inBuilder.getResoult();
}
}
On the direcotr side I try to group together some properties(the layout and the (J)component type) but with all of this my constructor (director constructor) is still to complex.
I try also to encapsulate the parameter in to a "paramter object"(thanx for advice) and to reuse this to create new instances (via director).
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic