2006-06-08

Setter returns this

I've spied in new Eclipse a new feature or even pattern of beautiful code.

Void is useless. When every expression has a value it's cool.

/**
* This class provides a convienient shorthand for creating and initializing
* GridData. This offers several benefits over creating GridData normal way:
* The same factory can be used many times to create several GridData instances
* The setters on GridDataFactory all return "this", allowing them to be chained
*/


For example:
// Example : Typical grid data for a wrapping label
// GridDataFactory version
GridDataFactory.fillDefaults()
.align(SWT.FILL, SWT.CENTER)
.hint(150, SWT.DEFAULT)
.grab(true, false)
.applyTo(wrappingLabel);

// Equivalent SWT version
GridData wrappingLabelData = new GridData(GridData.FILL_HORIZONTAL | GridData.VERTICAL_ALIGN_CENTER);
wrappingLabelData.minimumWidth = 1;
wrappingLabelData.widthHint = 150;
wrappingLabel.setLayoutData(wrappingLabelData);

No comments:

Post a Comment