SlideShare a Scribd company logo
1 of 48
Download to read offline
Favoring Composition
- The Groovy Way
Naresha K
@naresha_k
https://blog.nareshak.com/
APACHECON @HOME
Spt, 29th – Oct. 1st 2020
About me
Developer, Architect &
Tech Excellence Coach
Founder & Organiser
Bangalore Groovy User
Group
2
Object-Oriented
Maturity Model!
3
2 No inheritance. Mostly
Procedural code
1 Inheritance Everywhere
0
No inheritance. Mostly
Procedural code
Evolution of OO Programmer 4
INHERITANCE
5
6
2 No inheritance. Mostly
Procedural code
1 Inheritance Everywhere
0
No inheritance. Mostly
Procedural code
Evolution of OO Programmer 7
Liskov Substitution
Principle
8
2 Favour composition
1 Inheritance Everywhere
0
No inheritance. Mostly
Procedural code
Evolution of OO Programmer 9
Favour
Composition to Inheritance
10
The difference between
theory and practice is
in theory
somewhat smaller
than in practice
11
Inheritance is

Easy
12
List<String> phoneNumbers = new ArrayList<>()
phoneNumbers += ['19876512345', '19876512346',
‘919876512347', '49876512348']
println phoneNumbers
13
List<String> phoneNumbers = new ArrayList<>()
phoneNumbers += ['19876512345', '19876512346',
‘919876512347', '49876512348']
println phoneNumbers
println phoneNumbers.collect { "+" + it }
14
List<String> phoneNumbers = new ArrayList<>()
phoneNumbers += ['19876512345', '19876512346',
‘919876512347', '49876512348']
println phoneNumbers
println phoneNumbers.collect { "+" + it }
println phoneNumbers.find { it == '919876512347' }
15
List<String> phoneNumbers = new ArrayList<>()
phoneNumbers += ['19876512345', '19876512346',
‘919876512347', '49876512348']
println phoneNumbers
println phoneNumbers.collect { "+" + it }
println phoneNumbers.find { it == '919876512347' }
println phoneNumbers.usPhoneNumbers();
Method usPhoneNumbers() not
available in ArrayList
16
class PhoneNumbers extends ArrayList<String> {
List<String> usPhoneNumbers() {
iterator().findAll { number -> number.startsWith("1") }
}
}
17
PhoneNumbers phoneNumbers = new PhoneNumbers()
phoneNumbers += ['19876512345', '19876512346', '919876512347',
'49876512348']
println phoneNumbers
println phoneNumbers.collect { "+" + it }
println phoneNumbers.find { it == '919876512347' }
println phoneNumbers.usPhoneNumbers();
18
@ToString(includePackage = false, includes = "data")
class PhoneNumbers {
List<String> data = []
String find(Closure closure) {
data.find { closure }
}
public <T> List<T> collect(Closure<T> closure) {
data.collect(closure)
}
List<String> usPhoneNumbers() {
data.findAll { number -> number.startsWith("1") }
}
PhoneNumbers plus(List list) {
new PhoneNumbers(data: data + list)
}
}
19
@ToString(includePackage = false, includes = "data")
class PhoneNumbers {
List<String> data = []
String find(Closure closure) {
data.find { closure }
}
public <T> List<T> collect(Closure<T> closure) {
data.collect(closure)
}
List<String> usPhoneNumbers() {
data.findAll { number -> number.startsWith("1") }
}
PhoneNumbers plus(List list) {
new PhoneNumbers(data: data + list)
}
}
class PhoneNumbers extends ArrayList<String> {
List<String> usPhoneNumbers() {
iterator().findAll { number -> number.startsWith("1") }
}
}
20
@ToString(includePackage = false, includes = "data")
class PhoneNumbers {
@Delegate
List<String> data = []
List<String> usPhoneNumbers() {
data.findAll { number -> number.startsWith("1") }
}
}
21
@ToString(includePackage = false, includes = "data")
class PhoneNumbers {
@Delegate
List<String> data = []
List<String> usPhoneNumbers() {
data.findAll { number -> number.startsWith("1") }
}
}
PhoneNumbers phoneNumbers
phoneNumbers = ['19876512345', '19876512346', '919876512347',
'49876512348'] as PhoneNumbers
println phoneNumbers
println phoneNumbers.collect { "+" + it }
println phoneNumbers.find { it == '919876512347' }
println phoneNumbers.usPhoneNumbers();
22
23
Bird sunny = new Bird()
sunny.fly()
24
Bird sunny = new Bird()
sunny.fly()
ButterFly aButterFly = new ButterFly()
aButterFly.fly()
25
Bird sunny = new Bird()
sunny.fly()
ButterFly aButterFly = new ButterFly()
aButterFly.fly()
interface Flyable {
void fly()
}
26
Bird sunny = new Bird()
sunny.fly()
ButterFly aButterFly = new ButterFly()
aButterFly.fly()
interface Flyable {
void fly()
}
List<Flyable> fliers = [sunny, aButterFly]
fliers.each { flier -> flier.fly() }
27
class Bird implements Flyable {
@Override
void fly() {
println "Flying..."
}
}
class ButterFly implements Flyable {
@Override
void fly() {
println "Flying..."
}
}
28
class Bird implements Flyable {
@Override
void fly() {
println "Flying..."
}
}
class ButterFly implements Flyable {
@Override
void fly() {
println "Flying..."
}
}
Potential violation of
DRY
principle
29
30
class DefaultFlyable implements Flyable {
@Override
void fly() {
println "Flying..."
}
}
class Bird implements Flyable {
private Flyable defaultFlyable = new DefaultFlyable()
@Override
void fly() {
defaultFlyable.fly()
}
}
31
class DefaultFlyable implements Flyable {
@Override
void fly() {
println "Flying..."
}
}
class Bird implements Flyable {
@Delegate
private Flyable defaultFlyable = new DefaultFlyable()
}
interface Flyable {
void fly()
}
32
trait Flyable {
void fly() {
println "Flying..."
}
}
class Bird implements Flyable {}
class ButterFly implements Flyable {}
33
public interface Flyable {
public abstract void fly()
}
public static class Flyable$Trait$Helper {
public static void fly(Flyable $self) {
$self.println('Flying...')
}
}
public class Bird implements Flyable {
public void fly() {
Flyable$Trait$Helper.fly(this)
}
}
trait Flyable
34
interface Flyable {
void fly()
}
trait FlyableTrait implements Flyable {
@Override
void fly() {
println "Flying..."
}
}
35
class Actor implements Singer, Dancer {}
Actor actor = new Actor()
actor.sing()
actor.dance()
trait Singer {
void sing() {
println "Singing"
}
}
trait Dancer {
void dance() {
println "Dancing"
}
}
Composing multiple behaviours
36
trait BusinessObject {
UUID uuid = UUID.randomUUID()
}
class Product implements BusinessObject {
}
Product product = new Product()
println product.uuid
Composing state
37
public interface BusinessObject {
public abstract java.util.UUID getUuid()
public abstract void setUuid(java.util.UUID value)
}
public static interface BusinessObject$Trait$FieldHelper {}
public abstract static class BusinessObject$Trait$Helper {
public static java.util.UUID getUuid(BusinessObject $self) {
(( $self ) as
BusinessObject$Trait$FieldHelper).BusinessObject__uuid$get()
}
public static void setUuid(BusinessObject $self, java.util.UUID
value) {
(( $self ) as
BusinessObject$Trait$FieldHelper).BusinessObject__uuid$set(value)
}
}
38
public class Product implements BusinessObject,
BusinessObject$Trait$FieldHelper {
private java.util.UUID BusinessObject__uuid
}
39
class Person {
}
Person person = new Person()
person.fly()
Composing at Runtime
40
class Person {
}
Person person = new Person()
Flyable passanger = boardPlane(person)
passanger.fly()
Flyable boardPlane(Person person) {
person.withTraits FlyableTrait
}
41
trait JavaProgrammer {
def codeObjectOriented() {
println 'Coding OOP'
}
}
trait GroovyProgrammer extends JavaProgrammer {
def codeFunctional() {
println 'Coding FP'
}
}
class Developer implements GroovyProgrammer {}
Developer raj = new Developer()
raj.codeFunctional()
raj.codeObjectOriented()
Trait extending another Trait
42
trait Reader {
def read() { println 'Reading'}
}
trait Evaluator {
def eval() { println 'Evaluating'}
}
trait Printer {
def print() { println 'Printing'}
}
trait Repl implements Reader, Evaluator, Printer {
}
class GroovyRepl implements Repl{}
Repl groovyRepl = new GroovyRepl()
groovyRepl.read()
groovyRepl.eval()
groovyRepl.print()
Trait extending multiple Traits
43
interface MyInterface {
default void doSomething() {
println "Doing"
}
}
// is equivalent to
trait MyInterface {
void doSomething() {
println "Doing"
}
}
default methods in Groovy Interfaces
44
Function Composition
45
List<String> firstNamesOfDevs(List<Developer> devs, Closure
devSelector) {
List<Developer> selectedDevs = devSelector(devs)
selectedDevs.collect { it.name }
}
Closure groovyDevSelector = …
Closure javaDevSelector = …
Closure javaAndGroovyDevSelector = ?
println firstNamesOfDevs(developers, javaAndGroovyDevSelector)
46
List<String> firstNamesOfDevs(List<Developer> devs, Closure
devSelector) {
List<Developer> selectedDevs = devSelector(devs)
selectedDevs.collect { it.name }
}
Closure groovyDevSelector = …
Closure javaDevSelector = …
Closure javaAndGroovyDevSelector =
groovyDevSelector << javaDevSelector
println firstNamesOfDevs(developers, javaAndGroovyDevSelector)
47
Happy Composing
Thank You
APACHECON @HOME
Spt, 29th – Oct. 1st 2020

More Related Content

What's hot

What Have The Properties Ever Done For Us
What Have The Properties Ever Done For UsWhat Have The Properties Ever Done For Us
What Have The Properties Ever Done For UsMiklós Martin
 
Rのスコープとフレームと環境と
Rのスコープとフレームと環境とRのスコープとフレームと環境と
Rのスコープとフレームと環境とTakeshi Arabiki
 
令和から本気出す
令和から本気出す令和から本気出す
令和から本気出すTakashi Kitano
 
Rデバッグあれこれ
RデバッグあれこれRデバッグあれこれ
RデバッグあれこれTakeshi Arabiki
 
Error Management: Future vs ZIO
Error Management: Future vs ZIOError Management: Future vs ZIO
Error Management: Future vs ZIOJohn De Goes
 
{tidygraph}と{ggraph}による モダンなネットワーク分析(未公開ver)
{tidygraph}と{ggraph}による モダンなネットワーク分析(未公開ver){tidygraph}と{ggraph}による モダンなネットワーク分析(未公開ver)
{tidygraph}と{ggraph}による モダンなネットワーク分析(未公開ver)Takashi Kitano
 
Haskellで学ぶ関数型言語
Haskellで学ぶ関数型言語Haskellで学ぶ関数型言語
Haskellで学ぶ関数型言語ikdysfm
 
Generics and Inference
Generics and InferenceGenerics and Inference
Generics and InferenceRichard Fox
 
Transaction is a monad
Transaction is a  monadTransaction is a  monad
Transaction is a monadJarek Ratajski
 
Closure, Higher-order function in Swift
Closure, Higher-order function in SwiftClosure, Higher-order function in Swift
Closure, Higher-order function in SwiftSeongGyu Jo
 
Introduction to Gremlin
Introduction to GremlinIntroduction to Gremlin
Introduction to GremlinMax De Marzi
 
From java to kotlin beyond alt+shift+cmd+k - Droidcon italy
From java to kotlin beyond alt+shift+cmd+k - Droidcon italyFrom java to kotlin beyond alt+shift+cmd+k - Droidcon italy
From java to kotlin beyond alt+shift+cmd+k - Droidcon italyFabio Collini
 
{tidytext}と{RMeCab}によるモダンな日本語テキスト分析
{tidytext}と{RMeCab}によるモダンな日本語テキスト分析{tidytext}と{RMeCab}によるモダンな日本語テキスト分析
{tidytext}と{RMeCab}によるモダンな日本語テキスト分析Takashi Kitano
 
Groovy ネタ NGK 忘年会2009 ライトニングトーク
Groovy ネタ NGK 忘年会2009 ライトニングトークGroovy ネタ NGK 忘年会2009 ライトニングトーク
Groovy ネタ NGK 忘年会2009 ライトニングトークTsuyoshi Yamamoto
 
{shiny}と{leaflet}による地図アプリ開発Tips
{shiny}と{leaflet}による地図アプリ開発Tips{shiny}と{leaflet}による地図アプリ開発Tips
{shiny}と{leaflet}による地図アプリ開発TipsTakashi Kitano
 
Pre-Bootcamp introduction to Elixir
Pre-Bootcamp introduction to ElixirPre-Bootcamp introduction to Elixir
Pre-Bootcamp introduction to ElixirPaweł Dawczak
 
Fog City Ruby - Triple Equals Black Magic
Fog City Ruby - Triple Equals Black MagicFog City Ruby - Triple Equals Black Magic
Fog City Ruby - Triple Equals Black MagicBrandon Weaver
 
Intro to OTP in Elixir
Intro to OTP in ElixirIntro to OTP in Elixir
Intro to OTP in ElixirJesse Anderson
 

What's hot (20)

What Have The Properties Ever Done For Us
What Have The Properties Ever Done For UsWhat Have The Properties Ever Done For Us
What Have The Properties Ever Done For Us
 
Rのスコープとフレームと環境と
Rのスコープとフレームと環境とRのスコープとフレームと環境と
Rのスコープとフレームと環境と
 
令和から本気出す
令和から本気出す令和から本気出す
令和から本気出す
 
Rデバッグあれこれ
RデバッグあれこれRデバッグあれこれ
Rデバッグあれこれ
 
Error Management: Future vs ZIO
Error Management: Future vs ZIOError Management: Future vs ZIO
Error Management: Future vs ZIO
 
{tidygraph}と{ggraph}による モダンなネットワーク分析(未公開ver)
{tidygraph}と{ggraph}による モダンなネットワーク分析(未公開ver){tidygraph}と{ggraph}による モダンなネットワーク分析(未公開ver)
{tidygraph}と{ggraph}による モダンなネットワーク分析(未公開ver)
 
Haskellで学ぶ関数型言語
Haskellで学ぶ関数型言語Haskellで学ぶ関数型言語
Haskellで学ぶ関数型言語
 
Generics and Inference
Generics and InferenceGenerics and Inference
Generics and Inference
 
Pure kotlin
Pure kotlinPure kotlin
Pure kotlin
 
Transaction is a monad
Transaction is a  monadTransaction is a  monad
Transaction is a monad
 
Closure, Higher-order function in Swift
Closure, Higher-order function in SwiftClosure, Higher-order function in Swift
Closure, Higher-order function in Swift
 
Introduction to Gremlin
Introduction to GremlinIntroduction to Gremlin
Introduction to Gremlin
 
From java to kotlin beyond alt+shift+cmd+k - Droidcon italy
From java to kotlin beyond alt+shift+cmd+k - Droidcon italyFrom java to kotlin beyond alt+shift+cmd+k - Droidcon italy
From java to kotlin beyond alt+shift+cmd+k - Droidcon italy
 
{tidytext}と{RMeCab}によるモダンな日本語テキスト分析
{tidytext}と{RMeCab}によるモダンな日本語テキスト分析{tidytext}と{RMeCab}によるモダンな日本語テキスト分析
{tidytext}と{RMeCab}によるモダンな日本語テキスト分析
 
Groovy ネタ NGK 忘年会2009 ライトニングトーク
Groovy ネタ NGK 忘年会2009 ライトニングトークGroovy ネタ NGK 忘年会2009 ライトニングトーク
Groovy ネタ NGK 忘年会2009 ライトニングトーク
 
{shiny}と{leaflet}による地図アプリ開発Tips
{shiny}と{leaflet}による地図アプリ開発Tips{shiny}と{leaflet}による地図アプリ開発Tips
{shiny}と{leaflet}による地図アプリ開発Tips
 
Pre-Bootcamp introduction to Elixir
Pre-Bootcamp introduction to ElixirPre-Bootcamp introduction to Elixir
Pre-Bootcamp introduction to Elixir
 
はじめてのGroovy
はじめてのGroovyはじめてのGroovy
はじめてのGroovy
 
Fog City Ruby - Triple Equals Black Magic
Fog City Ruby - Triple Equals Black MagicFog City Ruby - Triple Equals Black Magic
Fog City Ruby - Triple Equals Black Magic
 
Intro to OTP in Elixir
Intro to OTP in ElixirIntro to OTP in Elixir
Intro to OTP in Elixir
 

Similar to Favouring Composition - The Groovy Way

Mixing functional and object oriented approaches to programming in C#
Mixing functional and object oriented approaches to programming in C#Mixing functional and object oriented approaches to programming in C#
Mixing functional and object oriented approaches to programming in C#Mark Needham
 
Mixing Functional and Object Oriented Approaches to Programming in C#
Mixing Functional and Object Oriented Approaches to Programming in C#Mixing Functional and Object Oriented Approaches to Programming in C#
Mixing Functional and Object Oriented Approaches to Programming in C#Skills Matter
 
From java to kotlin beyond alt+shift+cmd+k
From java to kotlin beyond alt+shift+cmd+kFrom java to kotlin beyond alt+shift+cmd+k
From java to kotlin beyond alt+shift+cmd+kFabio Collini
 
Mixing functional and object oriented approaches to programming in C#
Mixing functional and object oriented approaches to programming in C#Mixing functional and object oriented approaches to programming in C#
Mixing functional and object oriented approaches to programming in C#Mark Needham
 
Effective Java with Groovy
Effective Java with GroovyEffective Java with Groovy
Effective Java with GroovyNaresha K
 
Что нам готовит грядущий C#7?
Что нам готовит грядущий C#7?Что нам готовит грядущий C#7?
Что нам готовит грядущий C#7?Andrey Akinshin
 
A swift introduction to Swift
A swift introduction to SwiftA swift introduction to Swift
A swift introduction to SwiftGiordano Scalzo
 
Functional Programming with Groovy
Functional Programming with GroovyFunctional Programming with Groovy
Functional Programming with GroovyArturo Herrero
 
Effective Java with Groovy & Kotlin - How Languages Influence Adoption of Goo...
Effective Java with Groovy & Kotlin - How Languages Influence Adoption of Goo...Effective Java with Groovy & Kotlin - How Languages Influence Adoption of Goo...
Effective Java with Groovy & Kotlin - How Languages Influence Adoption of Goo...Naresha K
 
So I have this code(StackInAllSocks) and I implemented the method but.pdf
So I have this code(StackInAllSocks) and I implemented the method but.pdfSo I have this code(StackInAllSocks) and I implemented the method but.pdf
So I have this code(StackInAllSocks) and I implemented the method but.pdfaksahnan
 
Refactoring to Macros with Clojure
Refactoring to Macros with ClojureRefactoring to Macros with Clojure
Refactoring to Macros with ClojureDmitry Buzdin
 
JBUG 11 - Scala For Java Programmers
JBUG 11 - Scala For Java ProgrammersJBUG 11 - Scala For Java Programmers
JBUG 11 - Scala For Java ProgrammersTikal Knowledge
 
Elements of Object-oriented Design
Elements of Object-oriented DesignElements of Object-oriented Design
Elements of Object-oriented DesignHenry Osborne
 

Similar to Favouring Composition - The Groovy Way (20)

Scala introduction
Scala introductionScala introduction
Scala introduction
 
Mixing functional and object oriented approaches to programming in C#
Mixing functional and object oriented approaches to programming in C#Mixing functional and object oriented approaches to programming in C#
Mixing functional and object oriented approaches to programming in C#
 
Mixing Functional and Object Oriented Approaches to Programming in C#
Mixing Functional and Object Oriented Approaches to Programming in C#Mixing Functional and Object Oriented Approaches to Programming in C#
Mixing Functional and Object Oriented Approaches to Programming in C#
 
Arrays
ArraysArrays
Arrays
 
From java to kotlin beyond alt+shift+cmd+k
From java to kotlin beyond alt+shift+cmd+kFrom java to kotlin beyond alt+shift+cmd+k
From java to kotlin beyond alt+shift+cmd+k
 
mobl
moblmobl
mobl
 
Mixing functional and object oriented approaches to programming in C#
Mixing functional and object oriented approaches to programming in C#Mixing functional and object oriented approaches to programming in C#
Mixing functional and object oriented approaches to programming in C#
 
Effective Java with Groovy
Effective Java with GroovyEffective Java with Groovy
Effective Java with Groovy
 
Что нам готовит грядущий C#7?
Что нам готовит грядущий C#7?Что нам готовит грядущий C#7?
Что нам готовит грядущий C#7?
 
Pooya Khaloo Presentation on IWMC 2015
Pooya Khaloo Presentation on IWMC 2015Pooya Khaloo Presentation on IWMC 2015
Pooya Khaloo Presentation on IWMC 2015
 
A swift introduction to Swift
A swift introduction to SwiftA swift introduction to Swift
A swift introduction to Swift
 
Functional Programming with Groovy
Functional Programming with GroovyFunctional Programming with Groovy
Functional Programming with Groovy
 
Scala 2 + 2 > 4
Scala 2 + 2 > 4Scala 2 + 2 > 4
Scala 2 + 2 > 4
 
Effective Java with Groovy & Kotlin - How Languages Influence Adoption of Goo...
Effective Java with Groovy & Kotlin - How Languages Influence Adoption of Goo...Effective Java with Groovy & Kotlin - How Languages Influence Adoption of Goo...
Effective Java with Groovy & Kotlin - How Languages Influence Adoption of Goo...
 
So I have this code(StackInAllSocks) and I implemented the method but.pdf
So I have this code(StackInAllSocks) and I implemented the method but.pdfSo I have this code(StackInAllSocks) and I implemented the method but.pdf
So I have this code(StackInAllSocks) and I implemented the method but.pdf
 
Refactoring to Macros with Clojure
Refactoring to Macros with ClojureRefactoring to Macros with Clojure
Refactoring to Macros with Clojure
 
Java Generics
Java GenericsJava Generics
Java Generics
 
JBUG 11 - Scala For Java Programmers
JBUG 11 - Scala For Java ProgrammersJBUG 11 - Scala For Java Programmers
JBUG 11 - Scala For Java Programmers
 
Functional DDD
Functional DDDFunctional DDD
Functional DDD
 
Elements of Object-oriented Design
Elements of Object-oriented DesignElements of Object-oriented Design
Elements of Object-oriented Design
 

More from Naresha K

The Groovy Way of Testing with Spock
The Groovy Way of Testing with SpockThe Groovy Way of Testing with Spock
The Groovy Way of Testing with SpockNaresha K
 
Evolving with Java - How to Remain Effective
Evolving with Java - How to Remain EffectiveEvolving with Java - How to Remain Effective
Evolving with Java - How to Remain EffectiveNaresha K
 
Take Control of your Integration Testing with TestContainers
Take Control of your Integration Testing with TestContainersTake Control of your Integration Testing with TestContainers
Take Control of your Integration Testing with TestContainersNaresha K
 
Implementing Resilience with Micronaut
Implementing Resilience with MicronautImplementing Resilience with Micronaut
Implementing Resilience with MicronautNaresha K
 
Take Control of your Integration Testing with TestContainers
Take Control of your Integration Testing with TestContainersTake Control of your Integration Testing with TestContainers
Take Control of your Integration Testing with TestContainersNaresha K
 
Effective Java with Groovy - How Language Influences Adoption of Good Practices
Effective Java with Groovy - How Language Influences Adoption of Good PracticesEffective Java with Groovy - How Language Influences Adoption of Good Practices
Effective Java with Groovy - How Language Influences Adoption of Good PracticesNaresha K
 
What's in Groovy for Functional Programming
What's in Groovy for Functional ProgrammingWhat's in Groovy for Functional Programming
What's in Groovy for Functional ProgrammingNaresha K
 
Effective Java with Groovy & Kotlin How Languages Influence Adoption of Good ...
Effective Java with Groovy & Kotlin How Languages Influence Adoption of Good ...Effective Java with Groovy & Kotlin How Languages Influence Adoption of Good ...
Effective Java with Groovy & Kotlin How Languages Influence Adoption of Good ...Naresha K
 
Eclipse Collections, Java Streams & Vavr - What's in them for Functional Pro...
Eclipse Collections, Java Streams & Vavr - What's in them for  Functional Pro...Eclipse Collections, Java Streams & Vavr - What's in them for  Functional Pro...
Eclipse Collections, Java Streams & Vavr - What's in them for Functional Pro...Naresha K
 
Implementing Cloud-Native Architectural Patterns with Micronaut
Implementing Cloud-Native Architectural Patterns with MicronautImplementing Cloud-Native Architectural Patterns with Micronaut
Implementing Cloud-Native Architectural Patterns with MicronautNaresha K
 
Groovy - Why and Where?
Groovy  - Why and Where?Groovy  - Why and Where?
Groovy - Why and Where?Naresha K
 
Leveraging Micronaut on AWS Lambda
Leveraging Micronaut on AWS LambdaLeveraging Micronaut on AWS Lambda
Leveraging Micronaut on AWS LambdaNaresha K
 
Groovy Refactoring Patterns
Groovy Refactoring PatternsGroovy Refactoring Patterns
Groovy Refactoring PatternsNaresha K
 
Implementing Cloud-native Architectural Patterns with Micronaut
Implementing Cloud-native Architectural Patterns with MicronautImplementing Cloud-native Architectural Patterns with Micronaut
Implementing Cloud-native Architectural Patterns with MicronautNaresha K
 
Evolving with Java - How to remain Relevant and Effective
Evolving with Java - How to remain Relevant and EffectiveEvolving with Java - How to remain Relevant and Effective
Evolving with Java - How to remain Relevant and EffectiveNaresha K
 
Effective Java with Groovy - How Language can Influence Good Practices
Effective Java with Groovy - How Language can Influence Good PracticesEffective Java with Groovy - How Language can Influence Good Practices
Effective Java with Groovy - How Language can Influence Good PracticesNaresha K
 
Beyond Lambdas & Streams - Functional Fluency in Java
Beyond Lambdas & Streams - Functional Fluency in JavaBeyond Lambdas & Streams - Functional Fluency in Java
Beyond Lambdas & Streams - Functional Fluency in JavaNaresha K
 
GORM - The polyglot data access toolkit
GORM - The polyglot data access toolkitGORM - The polyglot data access toolkit
GORM - The polyglot data access toolkitNaresha K
 
Rethinking HTTP Apps using Ratpack
Rethinking HTTP Apps using RatpackRethinking HTTP Apps using Ratpack
Rethinking HTTP Apps using RatpackNaresha K
 
Design Patterns from 10K feet
Design Patterns from 10K feetDesign Patterns from 10K feet
Design Patterns from 10K feetNaresha K
 

More from Naresha K (20)

The Groovy Way of Testing with Spock
The Groovy Way of Testing with SpockThe Groovy Way of Testing with Spock
The Groovy Way of Testing with Spock
 
Evolving with Java - How to Remain Effective
Evolving with Java - How to Remain EffectiveEvolving with Java - How to Remain Effective
Evolving with Java - How to Remain Effective
 
Take Control of your Integration Testing with TestContainers
Take Control of your Integration Testing with TestContainersTake Control of your Integration Testing with TestContainers
Take Control of your Integration Testing with TestContainers
 
Implementing Resilience with Micronaut
Implementing Resilience with MicronautImplementing Resilience with Micronaut
Implementing Resilience with Micronaut
 
Take Control of your Integration Testing with TestContainers
Take Control of your Integration Testing with TestContainersTake Control of your Integration Testing with TestContainers
Take Control of your Integration Testing with TestContainers
 
Effective Java with Groovy - How Language Influences Adoption of Good Practices
Effective Java with Groovy - How Language Influences Adoption of Good PracticesEffective Java with Groovy - How Language Influences Adoption of Good Practices
Effective Java with Groovy - How Language Influences Adoption of Good Practices
 
What's in Groovy for Functional Programming
What's in Groovy for Functional ProgrammingWhat's in Groovy for Functional Programming
What's in Groovy for Functional Programming
 
Effective Java with Groovy & Kotlin How Languages Influence Adoption of Good ...
Effective Java with Groovy & Kotlin How Languages Influence Adoption of Good ...Effective Java with Groovy & Kotlin How Languages Influence Adoption of Good ...
Effective Java with Groovy & Kotlin How Languages Influence Adoption of Good ...
 
Eclipse Collections, Java Streams & Vavr - What's in them for Functional Pro...
Eclipse Collections, Java Streams & Vavr - What's in them for  Functional Pro...Eclipse Collections, Java Streams & Vavr - What's in them for  Functional Pro...
Eclipse Collections, Java Streams & Vavr - What's in them for Functional Pro...
 
Implementing Cloud-Native Architectural Patterns with Micronaut
Implementing Cloud-Native Architectural Patterns with MicronautImplementing Cloud-Native Architectural Patterns with Micronaut
Implementing Cloud-Native Architectural Patterns with Micronaut
 
Groovy - Why and Where?
Groovy  - Why and Where?Groovy  - Why and Where?
Groovy - Why and Where?
 
Leveraging Micronaut on AWS Lambda
Leveraging Micronaut on AWS LambdaLeveraging Micronaut on AWS Lambda
Leveraging Micronaut on AWS Lambda
 
Groovy Refactoring Patterns
Groovy Refactoring PatternsGroovy Refactoring Patterns
Groovy Refactoring Patterns
 
Implementing Cloud-native Architectural Patterns with Micronaut
Implementing Cloud-native Architectural Patterns with MicronautImplementing Cloud-native Architectural Patterns with Micronaut
Implementing Cloud-native Architectural Patterns with Micronaut
 
Evolving with Java - How to remain Relevant and Effective
Evolving with Java - How to remain Relevant and EffectiveEvolving with Java - How to remain Relevant and Effective
Evolving with Java - How to remain Relevant and Effective
 
Effective Java with Groovy - How Language can Influence Good Practices
Effective Java with Groovy - How Language can Influence Good PracticesEffective Java with Groovy - How Language can Influence Good Practices
Effective Java with Groovy - How Language can Influence Good Practices
 
Beyond Lambdas & Streams - Functional Fluency in Java
Beyond Lambdas & Streams - Functional Fluency in JavaBeyond Lambdas & Streams - Functional Fluency in Java
Beyond Lambdas & Streams - Functional Fluency in Java
 
GORM - The polyglot data access toolkit
GORM - The polyglot data access toolkitGORM - The polyglot data access toolkit
GORM - The polyglot data access toolkit
 
Rethinking HTTP Apps using Ratpack
Rethinking HTTP Apps using RatpackRethinking HTTP Apps using Ratpack
Rethinking HTTP Apps using Ratpack
 
Design Patterns from 10K feet
Design Patterns from 10K feetDesign Patterns from 10K feet
Design Patterns from 10K feet
 

Recently uploaded

%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...masabamasaba
 
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...Jittipong Loespradit
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplatePresentation.STUDIO
 
Define the academic and professional writing..pdf
Define the academic and professional writing..pdfDefine the academic and professional writing..pdf
Define the academic and professional writing..pdfPearlKirahMaeRagusta1
 
WSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go PlatformlessWSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go PlatformlessWSO2
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Steffen Staab
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...masabamasaba
 
The title is not connected to what is inside
The title is not connected to what is insideThe title is not connected to what is inside
The title is not connected to what is insideshinachiaurasa2
 
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024VictoriaMetrics
 
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension AidPhilip Schwarz
 
%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Harare%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Hararemasabamasaba
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsArshad QA
 
%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in sowetomasabamasaba
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...Health
 
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfonteinmasabamasaba
 
tonesoftg
tonesoftgtonesoftg
tonesoftglanshi9
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisamasabamasaba
 
%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrandmasabamasaba
 
WSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
WSO2Con2024 - Enabling Transactional System's Exponential Growth With SimplicityWSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
WSO2Con2024 - Enabling Transactional System's Exponential Growth With SimplicityWSO2
 

Recently uploaded (20)

%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
 
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation Template
 
Define the academic and professional writing..pdf
Define the academic and professional writing..pdfDefine the academic and professional writing..pdf
Define the academic and professional writing..pdf
 
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
 
WSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go PlatformlessWSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go Platformless
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
 
The title is not connected to what is inside
The title is not connected to what is insideThe title is not connected to what is inside
The title is not connected to what is inside
 
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
 
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
 
%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Harare%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Harare
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview Questions
 
%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
 
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
 
tonesoftg
tonesoftgtonesoftg
tonesoftg
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand
 
WSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
WSO2Con2024 - Enabling Transactional System's Exponential Growth With SimplicityWSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
WSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
 

Favouring Composition - The Groovy Way

  • 1. Favoring Composition - The Groovy Way Naresha K @naresha_k https://blog.nareshak.com/ APACHECON @HOME Spt, 29th – Oct. 1st 2020
  • 2. About me Developer, Architect & Tech Excellence Coach Founder & Organiser Bangalore Groovy User Group 2
  • 4. 2 No inheritance. Mostly Procedural code 1 Inheritance Everywhere 0 No inheritance. Mostly Procedural code Evolution of OO Programmer 4
  • 6. 6
  • 7. 2 No inheritance. Mostly Procedural code 1 Inheritance Everywhere 0 No inheritance. Mostly Procedural code Evolution of OO Programmer 7
  • 9. 2 Favour composition 1 Inheritance Everywhere 0 No inheritance. Mostly Procedural code Evolution of OO Programmer 9
  • 11. The difference between theory and practice is in theory somewhat smaller than in practice 11
  • 13. List<String> phoneNumbers = new ArrayList<>() phoneNumbers += ['19876512345', '19876512346', ‘919876512347', '49876512348'] println phoneNumbers 13
  • 14. List<String> phoneNumbers = new ArrayList<>() phoneNumbers += ['19876512345', '19876512346', ‘919876512347', '49876512348'] println phoneNumbers println phoneNumbers.collect { "+" + it } 14
  • 15. List<String> phoneNumbers = new ArrayList<>() phoneNumbers += ['19876512345', '19876512346', ‘919876512347', '49876512348'] println phoneNumbers println phoneNumbers.collect { "+" + it } println phoneNumbers.find { it == '919876512347' } 15
  • 16. List<String> phoneNumbers = new ArrayList<>() phoneNumbers += ['19876512345', '19876512346', ‘919876512347', '49876512348'] println phoneNumbers println phoneNumbers.collect { "+" + it } println phoneNumbers.find { it == '919876512347' } println phoneNumbers.usPhoneNumbers(); Method usPhoneNumbers() not available in ArrayList 16
  • 17. class PhoneNumbers extends ArrayList<String> { List<String> usPhoneNumbers() { iterator().findAll { number -> number.startsWith("1") } } } 17
  • 18. PhoneNumbers phoneNumbers = new PhoneNumbers() phoneNumbers += ['19876512345', '19876512346', '919876512347', '49876512348'] println phoneNumbers println phoneNumbers.collect { "+" + it } println phoneNumbers.find { it == '919876512347' } println phoneNumbers.usPhoneNumbers(); 18
  • 19. @ToString(includePackage = false, includes = "data") class PhoneNumbers { List<String> data = [] String find(Closure closure) { data.find { closure } } public <T> List<T> collect(Closure<T> closure) { data.collect(closure) } List<String> usPhoneNumbers() { data.findAll { number -> number.startsWith("1") } } PhoneNumbers plus(List list) { new PhoneNumbers(data: data + list) } } 19
  • 20. @ToString(includePackage = false, includes = "data") class PhoneNumbers { List<String> data = [] String find(Closure closure) { data.find { closure } } public <T> List<T> collect(Closure<T> closure) { data.collect(closure) } List<String> usPhoneNumbers() { data.findAll { number -> number.startsWith("1") } } PhoneNumbers plus(List list) { new PhoneNumbers(data: data + list) } } class PhoneNumbers extends ArrayList<String> { List<String> usPhoneNumbers() { iterator().findAll { number -> number.startsWith("1") } } } 20
  • 21. @ToString(includePackage = false, includes = "data") class PhoneNumbers { @Delegate List<String> data = [] List<String> usPhoneNumbers() { data.findAll { number -> number.startsWith("1") } } } 21
  • 22. @ToString(includePackage = false, includes = "data") class PhoneNumbers { @Delegate List<String> data = [] List<String> usPhoneNumbers() { data.findAll { number -> number.startsWith("1") } } } PhoneNumbers phoneNumbers phoneNumbers = ['19876512345', '19876512346', '919876512347', '49876512348'] as PhoneNumbers println phoneNumbers println phoneNumbers.collect { "+" + it } println phoneNumbers.find { it == '919876512347' } println phoneNumbers.usPhoneNumbers(); 22
  • 23. 23
  • 24. Bird sunny = new Bird() sunny.fly() 24
  • 25. Bird sunny = new Bird() sunny.fly() ButterFly aButterFly = new ButterFly() aButterFly.fly() 25
  • 26. Bird sunny = new Bird() sunny.fly() ButterFly aButterFly = new ButterFly() aButterFly.fly() interface Flyable { void fly() } 26
  • 27. Bird sunny = new Bird() sunny.fly() ButterFly aButterFly = new ButterFly() aButterFly.fly() interface Flyable { void fly() } List<Flyable> fliers = [sunny, aButterFly] fliers.each { flier -> flier.fly() } 27
  • 28. class Bird implements Flyable { @Override void fly() { println "Flying..." } } class ButterFly implements Flyable { @Override void fly() { println "Flying..." } } 28
  • 29. class Bird implements Flyable { @Override void fly() { println "Flying..." } } class ButterFly implements Flyable { @Override void fly() { println "Flying..." } } Potential violation of DRY principle 29
  • 30. 30
  • 31. class DefaultFlyable implements Flyable { @Override void fly() { println "Flying..." } } class Bird implements Flyable { private Flyable defaultFlyable = new DefaultFlyable() @Override void fly() { defaultFlyable.fly() } } 31
  • 32. class DefaultFlyable implements Flyable { @Override void fly() { println "Flying..." } } class Bird implements Flyable { @Delegate private Flyable defaultFlyable = new DefaultFlyable() } interface Flyable { void fly() } 32
  • 33. trait Flyable { void fly() { println "Flying..." } } class Bird implements Flyable {} class ButterFly implements Flyable {} 33
  • 34. public interface Flyable { public abstract void fly() } public static class Flyable$Trait$Helper { public static void fly(Flyable $self) { $self.println('Flying...') } } public class Bird implements Flyable { public void fly() { Flyable$Trait$Helper.fly(this) } } trait Flyable 34
  • 35. interface Flyable { void fly() } trait FlyableTrait implements Flyable { @Override void fly() { println "Flying..." } } 35
  • 36. class Actor implements Singer, Dancer {} Actor actor = new Actor() actor.sing() actor.dance() trait Singer { void sing() { println "Singing" } } trait Dancer { void dance() { println "Dancing" } } Composing multiple behaviours 36
  • 37. trait BusinessObject { UUID uuid = UUID.randomUUID() } class Product implements BusinessObject { } Product product = new Product() println product.uuid Composing state 37
  • 38. public interface BusinessObject { public abstract java.util.UUID getUuid() public abstract void setUuid(java.util.UUID value) } public static interface BusinessObject$Trait$FieldHelper {} public abstract static class BusinessObject$Trait$Helper { public static java.util.UUID getUuid(BusinessObject $self) { (( $self ) as BusinessObject$Trait$FieldHelper).BusinessObject__uuid$get() } public static void setUuid(BusinessObject $self, java.util.UUID value) { (( $self ) as BusinessObject$Trait$FieldHelper).BusinessObject__uuid$set(value) } } 38
  • 39. public class Product implements BusinessObject, BusinessObject$Trait$FieldHelper { private java.util.UUID BusinessObject__uuid } 39
  • 40. class Person { } Person person = new Person() person.fly() Composing at Runtime 40
  • 41. class Person { } Person person = new Person() Flyable passanger = boardPlane(person) passanger.fly() Flyable boardPlane(Person person) { person.withTraits FlyableTrait } 41
  • 42. trait JavaProgrammer { def codeObjectOriented() { println 'Coding OOP' } } trait GroovyProgrammer extends JavaProgrammer { def codeFunctional() { println 'Coding FP' } } class Developer implements GroovyProgrammer {} Developer raj = new Developer() raj.codeFunctional() raj.codeObjectOriented() Trait extending another Trait 42
  • 43. trait Reader { def read() { println 'Reading'} } trait Evaluator { def eval() { println 'Evaluating'} } trait Printer { def print() { println 'Printing'} } trait Repl implements Reader, Evaluator, Printer { } class GroovyRepl implements Repl{} Repl groovyRepl = new GroovyRepl() groovyRepl.read() groovyRepl.eval() groovyRepl.print() Trait extending multiple Traits 43
  • 44. interface MyInterface { default void doSomething() { println "Doing" } } // is equivalent to trait MyInterface { void doSomething() { println "Doing" } } default methods in Groovy Interfaces 44
  • 46. List<String> firstNamesOfDevs(List<Developer> devs, Closure devSelector) { List<Developer> selectedDevs = devSelector(devs) selectedDevs.collect { it.name } } Closure groovyDevSelector = … Closure javaDevSelector = … Closure javaAndGroovyDevSelector = ? println firstNamesOfDevs(developers, javaAndGroovyDevSelector) 46
  • 47. List<String> firstNamesOfDevs(List<Developer> devs, Closure devSelector) { List<Developer> selectedDevs = devSelector(devs) selectedDevs.collect { it.name } } Closure groovyDevSelector = … Closure javaDevSelector = … Closure javaAndGroovyDevSelector = groovyDevSelector << javaDevSelector println firstNamesOfDevs(developers, javaAndGroovyDevSelector) 47
  • 48. Happy Composing Thank You APACHECON @HOME Spt, 29th – Oct. 1st 2020