...
| Code Block |
|---|
delegator.delegate 'borrowFor'
delegator.delegate 'borrowAmount', 'getMoney'
def p = new Person()
println p.borrowFor('present') // => buy present
println p.getMoney(50) // => borrow $50
|
The first line above, adds the borrowFor method to the Person class by delegating to the lender object. The second line adds a getMoney method to the Person class by delegating to the lender object's borrowAmount method.
Alternatively, we could borrow multiple methods like this:
| Code Block |
|---|
delegator.delegateAll 'borrowFor', 'borrowAmount' |
Which adds these two methods to the Person class.
Or if we want all the methods, like this:
| Code Block |
|---|
delegator.delegateAll() |
Which will make all the methods in the delegate object available in the Person class.
Alternatively, we can use a map notation to rename multiple methods:
...