# Delegation Pattern

version 2.5.15

The Delegation Pattern is a technique where an object’s behavior (public methods) is implemented by delegating responsibility to one or more associated objects.

Groovy allows the traditional style of applying the delegation pattern, e.g. see Replace Inheritance with Delegation.

## 1. Implement Delegation Pattern using ExpandoMetaClass

The groovy.lang.ExpandoMetaClass allows usage of this pattern to be encapsulated in a library. This allows Groovy to emulate similar libraries available for the Ruby language.

Consider the following library class:

    class Delegator {
        private targetClass
        private delegate
        Delegator(targetClass, delegate) {
            this.targetClass = targetClass
            this.delegate = delegate
        }
        def delegate(String methodName) {
            delegate(methodName, methodName)
        }
        def delegate(String methodName, String asMethodName) {
            targetClass.metaClass."$asMethodName" = delegate.&"$methodName"
        }
        def delegateAll(String[] names) {
            names.each { delegate(it) }
        }
        def delegateAll(Map names) {
            names.each { k, v -> delegate(k, v) }
        }
        def delegateAll() {
            delegate.class.methods*.name.each { delegate(it) }
        }
    }

With this in your classpath, you can now apply the delegation pattern dynamically as shown in the following examples. First, consider we have the following classes:

    class Person {
        String name
    }

    class MortgageLender {
        def borrowAmount(amount) {
           "borrow \\$$amount"
        }
        def borrowFor(thing) {
           "buy \\$thing"
        }
    }

    def lender = new MortgageLender()

    def delegator = new Delegator(Person, lender)

We can now use the delegator to automatically borrow methods from the lender object to extend the Person class. We can borrow the methods as is or with a rename:

    delegator.delegate 'borrowFor'
    delegator.delegate 'borrowAmount', 'getMoney'

    def p = new Person()

    println p.borrowFor('present')   // => buy present
    println p.getMoney(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:

    delegator.delegateAll 'borrowFor', 'borrowAmount'

Which adds these two methods to the Person class.

Or if we want all the methods, like this:

    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:

    delegator.delegateAll borrowAmount:'getMoney', borrowFor:'getThing'

## 2. Implement Delegation Pattern using @Delegate annotation

Since version 1.6 you can use the built-in delegation mechanism which is based on AST transformation.

This make delegation even easier:

    class Person {
        def name
        @Delegate MortgageLender mortgageLender = new MortgageLender()
    }

    class MortgageLender {
        def borrowAmount(amount) {
           "borrow \\$$amount"
        }
        def borrowFor(thing) {
           "buy $thing"
        }
    }

    def p = new Person()

    assert "buy present" == p.borrowFor('present')
    assert "borrow \\$50" == p.borrowAmount(50)