SlideShare ist ein Scribd-Unternehmen logo
1 von 48
Downloaden Sie, um offline zu lesen
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

Weitere ähnliche Inhalte

Was ist angesagt?

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
 

Was ist angesagt? (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
 

Ähnlich wie 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
 

Ähnlich wie 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
 

Mehr von 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
 

Mehr von 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
 

Kürzlich hochgeladen

5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdfWave PLM
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsAlberto González Trastoy
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerThousandEyes
 
+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
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...panagenda
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...ICS
 
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...OnePlan Solutions
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsArshad QA
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfkalichargn70th171
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfkalichargn70th171
 
Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionSolGuruz
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...kellynguyen01
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...MyIntelliSource, Inc.
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...harshavardhanraghave
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Modelsaagamshah0812
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxbodapatigopi8531
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsJhone kinadey
 

Kürzlich hochgeladen (20)

5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
 
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
 
+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...
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
 
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS LiveVip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
 
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview Questions
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 
Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with Precision
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Models
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptx
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 

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