Copyright© 2008-2022 Sitevision AB, all rights reserved.
public interface Builder<T>
A Builder
is used to set up the state necessary to build an instance of a specific type.
A Builder
has zero or more optional attributes and zero or more mandatory attributes.
The builder will typically throw an IllegalStateException
if a mandatory attribute hasn't been
set when the build()
method is invoked.
A Builder
is not thread safe, but the it can be re-used by the same thread to build multiple
instances.
Using a Builder
is pretty straightforward, if you remember that it is stateful. Conceptually you
would typically use it like this:
When you have built an instance, you can re-use the builder to build more instances. Typically like:
All methods (except the build()
method) of a Builder
will typically
return the builder itself.
This makes it easy to "chain" all method invocations, i.e. do all setup on a single line of code.
In Java, you would typically use the MailBuilder
like this:
MailBuilder mailBuilder = siteVisionUtils.getMailUtil().getMailBuilder();
// Create a mail
Mail mail = mailBuilder.setSubject("Hello")
.setTextMessage("How are you Magnus?")
.addRecipient("magnus.lovgren@sitevision.se")
.build();
The code would typically look similar when using server-side JavaScript:
var mailBuilder = require('MailBuilder');
// Create a mail
var mail = mailBuilder.setSubject('Hello')
.setTextMessage('How are you Magnus?')
.addRecipient('magnus.lovgren@sitevision.se')
.build();
To use the same multi-line approach in Velocity, you would actually have to write it like this:
#set ($mailBuilder = $sitevisionUtils.mailUtil.mailBuilder)
## Create a mail
#set ($mail = $mailBuilder.setSubject('Hello'
).setTextMessage('How are you Magnus?'
).addRecipient('magnus.lovgren@sitevision.se'
).build())
As exemplified above, working with a builder in Velocity has some caveats since the Velocity parser doesn't allow whitespace before the dot (i.e. you must put the method closing parenthesis and the dot tight together). If you are uncomfortable handling multi-line nested method invocations in Velocity as above, you could instead:
## Create a mail
#set ($mail = $mailBuilder.setSubject('Hello').setTextMessage('How are you Magnus?').addRecipient('magnus.lovgren@sitevision.se').build())
ScriptUtil.swallow(Object)
method to prohibit unwanted output:
## Get script util
#set ($scriptUtil = $sitevisionUtils.scriptUtil)
...
## Create a mail
$scriptUtil.swallow($mailBuilder.setSubject('Hello'))
$scriptUtil.swallow($mailBuilder.setTextMessage('How are you Magnus?'))
$scriptUtil.swallow($mailBuilder.addRecipient('magnus.lovgren@sitevision.se'))
#set ($mail = $mailBuilder.build())
Modifier and Type | Method and Description |
---|---|
T |
build()
Creates an instance of type T using current state of this builder.
|
T build()
Sitevision - Portal and Content Management Made Easy
Sitevision is an advanced Java enterprise portal product and a portlet container (JSR 286) that implements Java Content Repository (JSR 283).
Copyright© 2008-2022 Sitevision AB, all rights reserved.