O'Reilly logo

Learning Groovy by Adam L. Davis

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

© Adam L. Davis 2016

Adam L. Davis, Learning Groovy, 10.1007/978-1-4842-2117-4_6

6. Groovy Design Patterns

Adam L. Davis

(1)New York, USA

Design patterns are a great way to make your code functional, readable, and extensible. There are some patterns that are easier and require less code in Groovy compared to Java.

Strategy Pattern

Imagine you have three different methods for finding totals:

 1   def totalPricesLessThan10(prices) { 2           int total = 0 3           for (int price : prices) 4                   if (price < 10) total += price 5           total 6   } 7    def totalPricesMoreThan10(prices) { 8               int total = 0 9               for (int price : prices)10                       if (price > 10) total += price11               total ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required