English subtitles

← Groovy Closures and Objects

Get Embed Code
4 Languages

Showing Revision 3 created 05/25/2016 by Udacity Robot.

  1. Now that we've got
    the fundamentals under our belt,
  2. let's check out some
    advanced Groovy features.
  3. The first one is called the closure.
  4. The closure is essentially a different
    way of declaring a function.
  5. But a function that can be packaged up,
    passed around and assigned to variables.
  6. You declare one just using
    curly braces like this.
  7. Closures are so-called because they
    also capture variables from their
  8. enclosing environment.
  9. So, this value of foo will
    always be one million dollars.
  10. Let's go ahead and try calling,
    since myClosure is a function,
  11. we can call it just by putting parenths
    after it, let's give it a try.
  12. Awesome, there we go.
  13. We're getting print statements
    from inside the closure and
  14. the value of foo is respected
    inside the closure.
  15. Unlike normal functions, closures can be
    passed around to different variables.
  16. So if we declare bar and set it equal
    to myClosure and then declare yet
  17. another variable baz and
  18. set it equal t0 bar, we can then invoke
    baz because baz is now a function.
  19. And there we go.
  20. By calling baz, we got the exact
    same result as calling myClosure.
  21. All right, let's do some really
    tricky stuff, hold on to your hats.
  22. So, closures have a different syntax for
  23. You put a comma separated list of your
    arguments on the left side of an arrow
  24. and then do whatever you
    want with the arguments.
  25. So I hope it's clear that this is
    actually a function that doubles
  26. it inputs.
  27. The tricky bit is that Gradle allows for
    higher order functions.
  28. That is, functions that take as one
    of their inputs another function.
  29. For example, here we have applyTwice.
  30. Its first argument is a function and
    its second argument is just some
  31. argument to be passed
    through that function.
  32. And what it returns is function
    applied twice to that argument.
  33. Let's check out an example.
  34. If we set foo equal to 5 and
    then we apply doubleIt twice to foo.
  35. Well, let's see what we get.
  36. Before we run it, see if you can figure
    out what it's going to be ahead of time.
  37. Well, let's see, five doubled is ten and
    then ten doubled is 20.
  38. Awesome.
  39. Let's talk about lists in Groovy.
  40. Groovy allows for a super simple list
    literal syntax and also allows for
  41. some very sophisticated list processing.
  42. For example, we can execute
    a closure for each item in a list.
  43. We simply define our closure.
  44. In this case,
    it's just printing out each item.
  45. And then we call myList.each and
    then our closure.
  46. Let's see what this does.
  47. And there we go.
  48. Each item in our list gets printed out.
  49. There's a couple things we can do
    here to make our syntax a little bit
  50. more compact.
  51. The first thing is we can
    define our closure in line.
  52. Note we don't have any
    parentheses around here because
  53. Groovy syntax is awesome.
  54. Also, if a closure is
    only taking one argument,
  55. then by default that
    argument is just called it.
  56. So let's try running this.
  57. And there we go,
    does exactly what we expect.
  58. Let's talk about classes in Groovy.
  59. Groovy has a simplified syntax for
    creating classes, but
  60. the classes themselves
    are essentially Java under the hood.
  61. So, here we've got a class.
  62. It's got a member variable,
    it's got a method.
  63. We can create a new one,
    just how you're used to from Java,
  64. and then we can ask it
    to print its greeting.
  65. We can also change the greeting and
    ask it to print that greeting again.
  66. Groovy automatically creates getters and
    setters for
  67. all of your member variables.
  68. Let's give this a try.
  69. And our outputs are just as we expect.
  70. First printing the default greeting
    then after we've customized it,
  71. it prints the custom greeting.
  72. Closures can have a delegate object.
  73. Let's check out this
    closure we've created here.
  74. It's trying to assign to
    a variable called greeting.
  75. But greetingClosure doesn't have
    a variable called greeting.
  76. Actually, the only place the greeting is
    defined is as a member variable of this
  77. class.
  78. What we can do is set the delegate
    of greetingClosure to be an instance
  79. of GroovyGreeter.
  80. And now, greeting closure can
    access the member variables and
  81. methods of that instance
    of GroovyGreeter.
  82. So if all goes well,
    when we invoke greetingClosure,
  83. after setting its delegate
    to myGroovyGreeter,
  84. it should be able to set the greeting
    and print the greeting just fine.
  85. Let's give it a try.
  86. There we go.
  87. We have successfully changed
    the greeting inside the instance of
  88. GroovyGreeter from a closure.
  89. Well congrats on completing
    our whirlwind tour of Groovy.
  90. I know that was a lot to cover.
  91. Fortunately there are a ton of
    resources out there to help you.
  92. We've linked a bunch in the instructor
    notes and I strongly recommend
  93. checking out this demo again in
    the sample code provided with the class.