Reputation: 1
I am creating a Groovy project and I would like to pass a method as a parameter is this possible?
I have 2 methods, which basically do the same thing but have a few small differences, each have different properties.
I would like to send through whether to use the 'buildPayLoad' function or 'buildSecondPayLoad' into my sendEmail function.
private Operation sendEmail(Order order) {
def payload = buildPayload(order, templateId)
}
private firstEmailPayLoad buildPayload(Order order, String templateId) {
new firstEmailPayLoad(
templateId,
config.fromAddress,
order.firstPayLoadProperty,
buildEmailDataFirstPayLoad(order)
).validate()
}
private secondEmailPayLoad buildSecondPayLoad(Order order, String templateId) {
new secondEmailPayLoad(
templateId,
config.fromAddress,
config.bccAddress,
config.otherProperty,
order.secondPayLoadProperty
buildEmailData(order)
).validate()
}
Upvotes: 0
Views: 3019
Reputation: 171114
You can just do:
private Operation sendEmail(Order order, Closure builder) {
def payload = builder(order, templateId)
}
Then call it with:
sendEmail(order, this.&buildPayload)
Or
sendEmail(order, this.&buildSecondPayLoad)
BTW: You really should start class names with a capital letter, so instead of secondEmailPayLoad
, you should call it SecondEmailPayLoad
(and the same with firstEmailPayLoad
)
Upvotes: 4