title: What are delegate objects?
body: <p>I am currently working on writing gradle build scripts. What are delegate objects and when are they used?</p>
  
  <pre><code>class GroovyGreeter {
      String greeting = "Default greeting"
      def printGreeting(){println "Greeting: $greeting"}
  }
  
  def myGroovyGreeter = new GroovyGreeter()
  
  myGroovyGreeter.printGreeting()
  myGroovyGreeter.greeting = "My custom greeting"
  myGroovyGreeter.printGreeting()
  
  /*
  
  The last Groovy feature we'll cover is that closures can have a delegate
  object. Any variables or methods referenced in the closure that don't have a
  local definition are then evaluated against the closure's delegate. Let's make
  a closure that will access the property and method of our GroovyGreeter class.
  
  */
  
  def greetingClosure = {
      greeting = "Setting the greeting from a closure"
      printGreeting()
  }
  
  //greetingClosure() // This doesn't work, because `greeting` isn't defined
  greetingClosure.delegate = myGroovyGreeter
  greetingClosure() // This works as `greeting` is a property of the delegate
  </code></pre>
  
  <p>Please help me out.</p>