Designing with Groovy Traits - Gr8Conf India

Naresha K
Naresha KDeveloper | Technical Excellence Coach | Consultant | Trainer at Independent
Designing with
Groovy Traits
Naresha K
Chief Technologist
Channel Bridge Software Labs
naresha.k@gmail.com
@naresha_k
About Me
OOP
Object Oriented Maturity Model
0
1
2
Designing with Groovy Traits - Gr8Conf India
Designing with Groovy Traits - Gr8Conf India
Designing with Groovy Traits - Gr8Conf India
I made up the term
‘object-oriented',
and I can tell you
I didn't have that in mind
Alan Kay
iskov Substitution Principle
"Favour 'object composition'
over
'class inheritance'."
Designing with Groovy Traits - Gr8Conf India
A case for
Traits
The Problem
Bird charlie = new Bird()
charlie.fly()
Butterfly aButterFly = new Butterfly()
aButterFly.fly()
Recall Interfaces
interface Flyable {
def fly()
}
class Bird implements Flyable {
def fly() {
println "Flying..."
}
}
class Butterfly implements Flyable {
def fly() {
println "Flying..."
}
}
The Smell
class Bird implements Flyable {
def fly() {
println "Flying..."
}
}
class Butterfly implements Flyable {
def fly() {
println "Flying..."
}
}
DRY
class FlyableImpl implements Flyable {
def fly() {
println 'Flying...'
}
}
class Bird implements Flyable {
def fly() {
new FlyableImpl().fly()
}
}
Making it Groovier
class DefaultFlyable implements Flyable {
def fly() {
println 'Flying...'
}
}
class Bird {
@Delegate
Flyable flyable = new DefaultFlyable()
}
Summarizing
interface Flyable {
def fly()
}
class DefaultFlyable implements Flyable {
def fly() {
println 'Flying...'
}
}
class Bird {
@Delegate
Flyable flyable = new DefaultFlyable()
}
Introducing Trait
trait Flyable {
def fly() {
println "Flying.."
}
}
class Bird implements Flyable {}
Multiple Capabilities
trait CanSing {
def sing() {
println "Singing"
}
}
trait CanDance {
def dance() {
println "Dancing"
}
}
class Person implements CanSing, CanDance {}
Person reema = new Person()
reema.sing()
reema.dance()
The Mechanics
Overriding Methods from a Trait
trait Speaker {
def speak() {
println "speaking"
}
}
class Gr8ConfSpeaker implements Speaker {
def speak() {
println "Groovy is Groovy!"
}
}
new Gr8ConfSpeaker().speak()
Traits can implement interfaces
interface Programmer {
def code()
}
trait GroovyProgrammer implements Programmer {
def code() {
println 'coding Groovy'
}
}
class Engineer implements GroovyProgrammer {}
new Engineer().code()
Traits can declare abstract methods
trait Programmer {
abstract String getLanguage()
def code() {
println "Coding ${getLanguage()}"
}
}
class GroovyProgrammer implements Programmer {
String getLanguage() { "Groovy"}
}
new GroovyProgrammer().code()
Traits can have a state
trait Programmer {
String language
def code() {
println "Coding ${language}"
}
}
class GroovyProgrammer implements Programmer {}
new GroovyProgrammer(language: 'Groovy').code()
A trait can extend another trait
trait JavaProgrammer {
def codeObjectOriented() {
println 'Coding OOP'
}
}
trait GroovyProgrammer extends JavaProgrammer {
def codeFunctional() {
println 'Coding FP'
}
}
class Engineer implements GroovyProgrammer {}
Engineer raj = new Engineer()
raj.codeFunctional()
raj.codeObjectOriented()
A trait can extend from multiple traits
trait Reader {
def read() { println 'Reading'}
}
trait Evaluator {
def eval() { println 'Evaluating'}
}
trait Printer {
def printer() { println 'Printing'}
}
trait Repl implements Reader, Evaluator, Printer {
}
The diamond problem
trait GroovyProgrammer {
def learn() { println 'Learning Traits'}
}
trait JavaProgrammer {
def learn() { println 'Busy coding'}
}
class Dev implements JavaProgrammer,
GroovyProgrammer {}
new Dev().learn()
Finer control on the diamond problem
class Dev implements JavaProgrammer,
GroovyProgrammer {
def learn() {
JavaProgrammer.super.learn()
}
}
Applying traits at run time
trait Flyable{
def fly(){
println "Flying.."
}
}
class Person {}
new Person().fly()
Applying traits at run time
def boardAPlane(Person person) {
person.withTraits Flyable
}
def passenger = boardAPlane(new Person())
passenger.fly()
More examples
Composing Behaviours
trait UserContextAware {
UserContext getUserContext(){
// implementation
}
}
class ProductApi implements UserContextAware {}
class PriceApi implements UserContextAware {}
common fields
trait Auditable {
String createdBy
String modifiedBy
Date dateCreated
Date lastUpdated
}
class Price implements Auditable {
String productCode
BigDecimal mrp
BigDecimal sellingPrice
}
common fields - a trait approach
Price groovyInActionToday = new Price(
productCode: '9789351198260',
mrp: 899,
sellingPrice: 751,
createdBy: 'admin',
modifiedBy: 'rk'
)
println groovyInActionToday.createdBy
println groovyInActionToday.modifiedBy
Chaining
interface Manager {
def approve(BigDecimal amount)
}
Chaining
trait JuniorManager implements Manager {
def approve(BigDecimal amount){
if(amount < 10000G){ println "Approved by JM” }
else{
println "Sending to SM"
super.approve()
}
}
}
trait SeniorManager implements Manager {
def approve(BigDecimal amount){
println "Approved by SM"
}
}
Chaining
class FinanceDepartment implements SeniorManager,
JuniorManager {}
FinanceDepartment finance = new FinanceDepartment()
finance.approve(3000)
finance.approve(30000)
Groovy Coding!
1 of 40

Recommended

Discovering functional treasure in idiomatic Groovy by
Discovering functional treasure in idiomatic GroovyDiscovering functional treasure in idiomatic Groovy
Discovering functional treasure in idiomatic GroovyNaresha K
1.7K views42 slides
Polyglot JVM by
Polyglot JVMPolyglot JVM
Polyglot JVMArturo Herrero
10.4K views70 slides
Functional Programming with Groovy by
Functional Programming with GroovyFunctional Programming with Groovy
Functional Programming with GroovyArturo Herrero
13.4K views71 slides
Polyglot Programming in the JVM by
Polyglot Programming in the JVMPolyglot Programming in the JVM
Polyglot Programming in the JVMAndres Almiray
1.5K views44 slides
Ruby 程式語言簡介 by
Ruby 程式語言簡介Ruby 程式語言簡介
Ruby 程式語言簡介Wen-Tien Chang
2.7K views58 slides
Groovy! by
Groovy!Groovy!
Groovy!Petr Giecek
471 views112 slides

More Related Content

What's hot

AST Transformations at JFokus by
AST Transformations at JFokusAST Transformations at JFokus
AST Transformations at JFokusHamletDRC
945 views101 slides
Groovy for Java Developers by
Groovy for Java DevelopersGroovy for Java Developers
Groovy for Java DevelopersAndres Almiray
2.1K views60 slides
Groovy for java developers by
Groovy for java developersGroovy for java developers
Groovy for java developersPuneet Behl
377 views80 slides
groovy & grails - lecture 3 by
groovy & grails - lecture 3groovy & grails - lecture 3
groovy & grails - lecture 3Alexandre Masselot
748 views80 slides
Groovy DSLs, from Beginner to Expert - Guillaume Laforge and Paul King - Spri... by
Groovy DSLs, from Beginner to Expert - Guillaume Laforge and Paul King - Spri...Groovy DSLs, from Beginner to Expert - Guillaume Laforge and Paul King - Spri...
Groovy DSLs, from Beginner to Expert - Guillaume Laforge and Paul King - Spri...Guillaume Laforge
18K views154 slides
GraphQL API in Clojure by
GraphQL API in ClojureGraphQL API in Clojure
GraphQL API in ClojureKent Ohashi
1.9K views44 slides

What's hot(20)

AST Transformations at JFokus by HamletDRC
AST Transformations at JFokusAST Transformations at JFokus
AST Transformations at JFokus
HamletDRC945 views
Groovy for Java Developers by Andres Almiray
Groovy for Java DevelopersGroovy for Java Developers
Groovy for Java Developers
Andres Almiray2.1K views
Groovy for java developers by Puneet Behl
Groovy for java developersGroovy for java developers
Groovy for java developers
Puneet Behl377 views
Groovy DSLs, from Beginner to Expert - Guillaume Laforge and Paul King - Spri... by Guillaume Laforge
Groovy DSLs, from Beginner to Expert - Guillaume Laforge and Paul King - Spri...Groovy DSLs, from Beginner to Expert - Guillaume Laforge and Paul King - Spri...
Groovy DSLs, from Beginner to Expert - Guillaume Laforge and Paul King - Spri...
Guillaume Laforge18K views
GraphQL API in Clojure by Kent Ohashi
GraphQL API in ClojureGraphQL API in Clojure
GraphQL API in Clojure
Kent Ohashi1.9K views
re-frame à la spec by Kent Ohashi
re-frame à la specre-frame à la spec
re-frame à la spec
Kent Ohashi1.6K views
Groovy Ast Transformations (greach) by HamletDRC
Groovy Ast Transformations (greach)Groovy Ast Transformations (greach)
Groovy Ast Transformations (greach)
HamletDRC1.8K views
GR8Conf 2009: What's New in Groovy 1.6? by Guillaume Laforge by GR8Conf
GR8Conf 2009: What's New in Groovy 1.6? by Guillaume LaforgeGR8Conf 2009: What's New in Groovy 1.6? by Guillaume Laforge
GR8Conf 2009: What's New in Groovy 1.6? by Guillaume Laforge
GR8Conf694 views
C++ for Java Developers (JavaZone Academy 2018) by Patricia Aas
C++ for Java Developers (JavaZone Academy 2018)C++ for Java Developers (JavaZone Academy 2018)
C++ for Java Developers (JavaZone Academy 2018)
Patricia Aas1.4K views
Mirah Talk for Boulder Ruby Group by baroquebobcat
Mirah Talk for Boulder Ruby GroupMirah Talk for Boulder Ruby Group
Mirah Talk for Boulder Ruby Group
baroquebobcat694 views
Diving into HHVM Extensions (PHPNW Conference 2015) by James Titcumb
Diving into HHVM Extensions (PHPNW Conference 2015)Diving into HHVM Extensions (PHPNW Conference 2015)
Diving into HHVM Extensions (PHPNW Conference 2015)
James Titcumb1K views
Grooscript gr8conf by GR8Conf
Grooscript gr8confGrooscript gr8conf
Grooscript gr8conf
GR8Conf696 views
Natural Language Toolkit (NLTK), Basics by Prakash Pimpale
Natural Language Toolkit (NLTK), Basics Natural Language Toolkit (NLTK), Basics
Natural Language Toolkit (NLTK), Basics
Prakash Pimpale16.1K views
Lift off with Groovy 2 at JavaOne 2013 by Guillaume Laforge
Lift off with Groovy 2 at JavaOne 2013Lift off with Groovy 2 at JavaOne 2013
Lift off with Groovy 2 at JavaOne 2013
Guillaume Laforge3.8K views
C# 7.0 Hacks and Features by Abhishek Sur
C# 7.0 Hacks and FeaturesC# 7.0 Hacks and Features
C# 7.0 Hacks and Features
Abhishek Sur1.2K views
"Simple Made Easy" Made Easy by Kent Ohashi
"Simple Made Easy" Made Easy"Simple Made Easy" Made Easy
"Simple Made Easy" Made Easy
Kent Ohashi2.6K views
Building High Performance Web Applications and Sites by goodfriday
Building High Performance Web Applications and SitesBuilding High Performance Web Applications and Sites
Building High Performance Web Applications and Sites
goodfriday451 views

Viewers also liked

science 8 by
science 8science 8
science 8Jennifer Guidi
53 views1 slide
G30022_Karen Devine_DL_Print by
G30022_Karen Devine_DL_PrintG30022_Karen Devine_DL_Print
G30022_Karen Devine_DL_PrintDr. Karen Devine
121 views2 slides
Paper by
PaperPaper
PaperSaumya Dhup
264 views4 slides
Araba by
ArabaAraba
Arabajakintzaikastola5c
672 views15 slides
Trabajo final by
Trabajo finalTrabajo final
Trabajo finalmonicamaria villada
144 views18 slides
ARC FLASH MITIGATION USING ACTIVE HIGH-SPEED SWITCHING by
ARC FLASH MITIGATION USING ACTIVE HIGH-SPEED SWITCHINGARC FLASH MITIGATION USING ACTIVE HIGH-SPEED SWITCHING
ARC FLASH MITIGATION USING ACTIVE HIGH-SPEED SWITCHINGAmit Chakraborty
406 views18 slides

Viewers also liked(13)

ARC FLASH MITIGATION USING ACTIVE HIGH-SPEED SWITCHING by Amit Chakraborty
ARC FLASH MITIGATION USING ACTIVE HIGH-SPEED SWITCHINGARC FLASH MITIGATION USING ACTIVE HIGH-SPEED SWITCHING
ARC FLASH MITIGATION USING ACTIVE HIGH-SPEED SWITCHING
Amit Chakraborty406 views
Biostatistics Workshop: Regression by HopkinsCFAR
Biostatistics Workshop: RegressionBiostatistics Workshop: Regression
Biostatistics Workshop: Regression
HopkinsCFAR308 views
Comandos usados en kali linux by Jhon TRUJILLO
Comandos usados en kali linuxComandos usados en kali linux
Comandos usados en kali linux
Jhon TRUJILLO730 views
Overview of JSI & USAID | DELIVER PROJECT Reproductive Health Supplies Monito... by JSI
Overview of JSI & USAID | DELIVER PROJECT Reproductive Health Supplies Monito...Overview of JSI & USAID | DELIVER PROJECT Reproductive Health Supplies Monito...
Overview of JSI & USAID | DELIVER PROJECT Reproductive Health Supplies Monito...
JSI 829 views
Kamal final presentation eee reb- comilla by siam hossain
Kamal final presentation eee  reb- comillaKamal final presentation eee  reb- comilla
Kamal final presentation eee reb- comilla
siam hossain1.1K views
Karen Devine Ind NUI Seanad Proposals by Dr. Karen Devine
Karen Devine Ind NUI Seanad ProposalsKaren Devine Ind NUI Seanad Proposals
Karen Devine Ind NUI Seanad Proposals
Dr. Karen Devine231 views

Similar to Designing with Groovy Traits - Gr8Conf India

Oop2010 Scala Presentation Stal by
Oop2010 Scala Presentation StalOop2010 Scala Presentation Stal
Oop2010 Scala Presentation StalMichael Stal
2.2K views60 slides
Polyglot Programming @ Jax.de 2010 by
Polyglot Programming @ Jax.de 2010Polyglot Programming @ Jax.de 2010
Polyglot Programming @ Jax.de 2010Andres Almiray
682 views38 slides
Polyglot Programming in the JVM - 33rd Degree by
Polyglot Programming in the JVM - 33rd DegreePolyglot Programming in the JVM - 33rd Degree
Polyglot Programming in the JVM - 33rd DegreeAndres Almiray
1.4K views46 slides
A (too) Short Introduction to Scala by
A (too) Short Introduction to ScalaA (too) Short Introduction to Scala
A (too) Short Introduction to ScalaRiccardo Cardin
3.3K views35 slides
2007 09 10 Fzi Training Groovy Grails V Ws by
2007 09 10 Fzi Training Groovy Grails V Ws2007 09 10 Fzi Training Groovy Grails V Ws
2007 09 10 Fzi Training Groovy Grails V Wsloffenauer
1.3K views58 slides
A Sceptical Guide to Functional Programming by
A Sceptical Guide to Functional ProgrammingA Sceptical Guide to Functional Programming
A Sceptical Guide to Functional ProgrammingGarth Gilmour
913 views112 slides

Similar to Designing with Groovy Traits - Gr8Conf India(20)

Oop2010 Scala Presentation Stal by Michael Stal
Oop2010 Scala Presentation StalOop2010 Scala Presentation Stal
Oop2010 Scala Presentation Stal
Michael Stal2.2K views
Polyglot Programming @ Jax.de 2010 by Andres Almiray
Polyglot Programming @ Jax.de 2010Polyglot Programming @ Jax.de 2010
Polyglot Programming @ Jax.de 2010
Andres Almiray682 views
Polyglot Programming in the JVM - 33rd Degree by Andres Almiray
Polyglot Programming in the JVM - 33rd DegreePolyglot Programming in the JVM - 33rd Degree
Polyglot Programming in the JVM - 33rd Degree
Andres Almiray1.4K views
A (too) Short Introduction to Scala by Riccardo Cardin
A (too) Short Introduction to ScalaA (too) Short Introduction to Scala
A (too) Short Introduction to Scala
Riccardo Cardin3.3K views
2007 09 10 Fzi Training Groovy Grails V Ws by loffenauer
2007 09 10 Fzi Training Groovy Grails V Ws2007 09 10 Fzi Training Groovy Grails V Ws
2007 09 10 Fzi Training Groovy Grails V Ws
loffenauer1.3K views
A Sceptical Guide to Functional Programming by Garth Gilmour
A Sceptical Guide to Functional ProgrammingA Sceptical Guide to Functional Programming
A Sceptical Guide to Functional Programming
Garth Gilmour913 views
Game Design and Development Workshop Day 1 by Troy Miles
Game Design and Development Workshop Day 1Game Design and Development Workshop Day 1
Game Design and Development Workshop Day 1
Troy Miles494 views
Polyglot Programming in the JVM - Øredev by Andres Almiray
Polyglot Programming in the JVM - ØredevPolyglot Programming in the JVM - Øredev
Polyglot Programming in the JVM - Øredev
Andres Almiray974 views
Dart structured web apps by chrisbuckett
Dart   structured web appsDart   structured web apps
Dart structured web apps
chrisbuckett1.3K views
Dart, unicorns and rainbows by chrisbuckett
Dart, unicorns and rainbowsDart, unicorns and rainbows
Dart, unicorns and rainbows
chrisbuckett997 views
Exploring Ceylon with Gavin King - JUG BB Talk - Belrin 2014 by hwilming
Exploring Ceylon with Gavin King - JUG BB Talk - Belrin 2014Exploring Ceylon with Gavin King - JUG BB Talk - Belrin 2014
Exploring Ceylon with Gavin King - JUG BB Talk - Belrin 2014
hwilming1.2K views
Advanced Python, Part 1 by Zaar Hai
Advanced Python, Part 1Advanced Python, Part 1
Advanced Python, Part 1
Zaar Hai776 views
What’s new in Google Dart - Seth Ladd by jaxconf
What’s new in Google Dart - Seth LaddWhat’s new in Google Dart - Seth Ladd
What’s new in Google Dart - Seth Ladd
jaxconf972 views

More from Naresha K

The Groovy Way of Testing with Spock by
The Groovy Way of Testing with SpockThe Groovy Way of Testing with Spock
The Groovy Way of Testing with SpockNaresha K
240 views40 slides
Evolving with Java - How to Remain Effective by
Evolving with Java - How to Remain EffectiveEvolving with Java - How to Remain Effective
Evolving with Java - How to Remain EffectiveNaresha K
243 views74 slides
Take Control of your Integration Testing with TestContainers by
Take Control of your Integration Testing with TestContainersTake Control of your Integration Testing with TestContainers
Take Control of your Integration Testing with TestContainersNaresha K
268 views50 slides
Implementing Resilience with Micronaut by
Implementing Resilience with MicronautImplementing Resilience with Micronaut
Implementing Resilience with MicronautNaresha K
343 views20 slides
Take Control of your Integration Testing with TestContainers by
Take Control of your Integration Testing with TestContainersTake Control of your Integration Testing with TestContainers
Take Control of your Integration Testing with TestContainersNaresha K
281 views21 slides
Favouring Composition - The Groovy Way by
Favouring Composition - The Groovy WayFavouring Composition - The Groovy Way
Favouring Composition - The Groovy WayNaresha K
229 views48 slides

More from Naresha K(20)

The Groovy Way of Testing with Spock by Naresha K
The Groovy Way of Testing with SpockThe Groovy Way of Testing with Spock
The Groovy Way of Testing with Spock
Naresha K240 views
Evolving with Java - How to Remain Effective by Naresha K
Evolving with Java - How to Remain EffectiveEvolving with Java - How to Remain Effective
Evolving with Java - How to Remain Effective
Naresha K243 views
Take Control of your Integration Testing with TestContainers by Naresha K
Take Control of your Integration Testing with TestContainersTake Control of your Integration Testing with TestContainers
Take Control of your Integration Testing with TestContainers
Naresha K268 views
Implementing Resilience with Micronaut by Naresha K
Implementing Resilience with MicronautImplementing Resilience with Micronaut
Implementing Resilience with Micronaut
Naresha K343 views
Take Control of your Integration Testing with TestContainers by Naresha K
Take Control of your Integration Testing with TestContainersTake Control of your Integration Testing with TestContainers
Take Control of your Integration Testing with TestContainers
Naresha K281 views
Favouring Composition - The Groovy Way by Naresha K
Favouring Composition - The Groovy WayFavouring Composition - The Groovy Way
Favouring Composition - The Groovy Way
Naresha K229 views
Effective Java with Groovy - How Language Influences Adoption of Good Practices by Naresha K
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
Naresha K294 views
What's in Groovy for Functional Programming by Naresha K
What's in Groovy for Functional ProgrammingWhat's in Groovy for Functional Programming
What's in Groovy for Functional Programming
Naresha K275 views
Effective Java with Groovy & Kotlin - How Languages Influence Adoption of Goo... by Naresha K
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 K208 views
Effective Java with Groovy & Kotlin How Languages Influence Adoption of Good ... by Naresha 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 ...
Naresha K162 views
Eclipse Collections, Java Streams & Vavr - What's in them for Functional Pro... by 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...
Naresha K499 views
Implementing Cloud-Native Architectural Patterns with Micronaut by Naresha K
Implementing Cloud-Native Architectural Patterns with MicronautImplementing Cloud-Native Architectural Patterns with Micronaut
Implementing Cloud-Native Architectural Patterns with Micronaut
Naresha K402 views
Groovy - Why and Where? by Naresha K
Groovy  - Why and Where?Groovy  - Why and Where?
Groovy - Why and Where?
Naresha K79 views
Leveraging Micronaut on AWS Lambda by Naresha K
Leveraging Micronaut on AWS LambdaLeveraging Micronaut on AWS Lambda
Leveraging Micronaut on AWS Lambda
Naresha K310 views
Groovy Refactoring Patterns by Naresha K
Groovy Refactoring PatternsGroovy Refactoring Patterns
Groovy Refactoring Patterns
Naresha K514 views
Implementing Cloud-native Architectural Patterns with Micronaut by Naresha K
Implementing Cloud-native Architectural Patterns with MicronautImplementing Cloud-native Architectural Patterns with Micronaut
Implementing Cloud-native Architectural Patterns with Micronaut
Naresha K667 views
Effective Java with Groovy by Naresha K
Effective Java with GroovyEffective Java with Groovy
Effective Java with Groovy
Naresha K501 views
Evolving with Java - How to remain Relevant and Effective by Naresha K
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
Naresha K294 views
Effective Java with Groovy - How Language can Influence Good Practices by Naresha K
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
Naresha K311 views
Beyond Lambdas & Streams - Functional Fluency in Java by Naresha K
Beyond Lambdas & Streams - Functional Fluency in JavaBeyond Lambdas & Streams - Functional Fluency in Java
Beyond Lambdas & Streams - Functional Fluency in Java
Naresha K186 views

Recently uploaded

Microsoft Power Platform.pptx by
Microsoft Power Platform.pptxMicrosoft Power Platform.pptx
Microsoft Power Platform.pptxUni Systems S.M.S.A.
53 views38 slides
HTTP headers that make your website go faster - devs.gent November 2023 by
HTTP headers that make your website go faster - devs.gent November 2023HTTP headers that make your website go faster - devs.gent November 2023
HTTP headers that make your website go faster - devs.gent November 2023Thijs Feryn
22 views151 slides
handbook for web 3 adoption.pdf by
handbook for web 3 adoption.pdfhandbook for web 3 adoption.pdf
handbook for web 3 adoption.pdfLiveplex
22 views16 slides
TouchLog: Finger Micro Gesture Recognition Using Photo-Reflective Sensors by
TouchLog: Finger Micro Gesture Recognition  Using Photo-Reflective SensorsTouchLog: Finger Micro Gesture Recognition  Using Photo-Reflective Sensors
TouchLog: Finger Micro Gesture Recognition Using Photo-Reflective Sensorssugiuralab
19 views15 slides
Ransomware is Knocking your Door_Final.pdf by
Ransomware is Knocking your Door_Final.pdfRansomware is Knocking your Door_Final.pdf
Ransomware is Knocking your Door_Final.pdfSecurity Bootcamp
55 views46 slides
Voice Logger - Telephony Integration Solution at Aegis by
Voice Logger - Telephony Integration Solution at AegisVoice Logger - Telephony Integration Solution at Aegis
Voice Logger - Telephony Integration Solution at AegisNirmal Sharma
39 views1 slide

Recently uploaded(20)

HTTP headers that make your website go faster - devs.gent November 2023 by Thijs Feryn
HTTP headers that make your website go faster - devs.gent November 2023HTTP headers that make your website go faster - devs.gent November 2023
HTTP headers that make your website go faster - devs.gent November 2023
Thijs Feryn22 views
handbook for web 3 adoption.pdf by Liveplex
handbook for web 3 adoption.pdfhandbook for web 3 adoption.pdf
handbook for web 3 adoption.pdf
Liveplex22 views
TouchLog: Finger Micro Gesture Recognition Using Photo-Reflective Sensors by sugiuralab
TouchLog: Finger Micro Gesture Recognition  Using Photo-Reflective SensorsTouchLog: Finger Micro Gesture Recognition  Using Photo-Reflective Sensors
TouchLog: Finger Micro Gesture Recognition Using Photo-Reflective Sensors
sugiuralab19 views
Voice Logger - Telephony Integration Solution at Aegis by Nirmal Sharma
Voice Logger - Telephony Integration Solution at AegisVoice Logger - Telephony Integration Solution at Aegis
Voice Logger - Telephony Integration Solution at Aegis
Nirmal Sharma39 views
The details of description: Techniques, tips, and tangents on alternative tex... by BookNet Canada
The details of description: Techniques, tips, and tangents on alternative tex...The details of description: Techniques, tips, and tangents on alternative tex...
The details of description: Techniques, tips, and tangents on alternative tex...
BookNet Canada127 views
Transcript: The Details of Description Techniques tips and tangents on altern... by BookNet Canada
Transcript: The Details of Description Techniques tips and tangents on altern...Transcript: The Details of Description Techniques tips and tangents on altern...
Transcript: The Details of Description Techniques tips and tangents on altern...
BookNet Canada136 views
Attacking IoT Devices from a Web Perspective - Linux Day by Simone Onofri
Attacking IoT Devices from a Web Perspective - Linux Day Attacking IoT Devices from a Web Perspective - Linux Day
Attacking IoT Devices from a Web Perspective - Linux Day
Simone Onofri16 views
Serverless computing with Google Cloud (2023-24) by wesley chun
Serverless computing with Google Cloud (2023-24)Serverless computing with Google Cloud (2023-24)
Serverless computing with Google Cloud (2023-24)
wesley chun11 views
STKI Israeli Market Study 2023 corrected forecast 2023_24 v3.pdf by Dr. Jimmy Schwarzkopf
STKI Israeli Market Study 2023   corrected forecast 2023_24 v3.pdfSTKI Israeli Market Study 2023   corrected forecast 2023_24 v3.pdf
STKI Israeli Market Study 2023 corrected forecast 2023_24 v3.pdf

Designing with Groovy Traits - Gr8Conf India

  • 1. Designing with Groovy Traits Naresha K Chief Technologist Channel Bridge Software Labs naresha.k@gmail.com @naresha_k
  • 3. OOP
  • 8. I made up the term ‘object-oriented', and I can tell you I didn't have that in mind Alan Kay
  • 13. The Problem Bird charlie = new Bird() charlie.fly() Butterfly aButterFly = new Butterfly() aButterFly.fly()
  • 15. class Bird implements Flyable { def fly() { println "Flying..." } } class Butterfly implements Flyable { def fly() { println "Flying..." } }
  • 16. The Smell class Bird implements Flyable { def fly() { println "Flying..." } } class Butterfly implements Flyable { def fly() { println "Flying..." } }
  • 17. DRY class FlyableImpl implements Flyable { def fly() { println 'Flying...' } } class Bird implements Flyable { def fly() { new FlyableImpl().fly() } }
  • 18. Making it Groovier class DefaultFlyable implements Flyable { def fly() { println 'Flying...' } } class Bird { @Delegate Flyable flyable = new DefaultFlyable() }
  • 19. Summarizing interface Flyable { def fly() } class DefaultFlyable implements Flyable { def fly() { println 'Flying...' } } class Bird { @Delegate Flyable flyable = new DefaultFlyable() }
  • 20. Introducing Trait trait Flyable { def fly() { println "Flying.." } } class Bird implements Flyable {}
  • 21. Multiple Capabilities trait CanSing { def sing() { println "Singing" } } trait CanDance { def dance() { println "Dancing" } } class Person implements CanSing, CanDance {} Person reema = new Person() reema.sing() reema.dance()
  • 23. Overriding Methods from a Trait trait Speaker { def speak() { println "speaking" } } class Gr8ConfSpeaker implements Speaker { def speak() { println "Groovy is Groovy!" } } new Gr8ConfSpeaker().speak()
  • 24. Traits can implement interfaces interface Programmer { def code() } trait GroovyProgrammer implements Programmer { def code() { println 'coding Groovy' } } class Engineer implements GroovyProgrammer {} new Engineer().code()
  • 25. Traits can declare abstract methods trait Programmer { abstract String getLanguage() def code() { println "Coding ${getLanguage()}" } } class GroovyProgrammer implements Programmer { String getLanguage() { "Groovy"} } new GroovyProgrammer().code()
  • 26. Traits can have a state trait Programmer { String language def code() { println "Coding ${language}" } } class GroovyProgrammer implements Programmer {} new GroovyProgrammer(language: 'Groovy').code()
  • 27. A trait can extend another trait trait JavaProgrammer { def codeObjectOriented() { println 'Coding OOP' } } trait GroovyProgrammer extends JavaProgrammer { def codeFunctional() { println 'Coding FP' } } class Engineer implements GroovyProgrammer {} Engineer raj = new Engineer() raj.codeFunctional() raj.codeObjectOriented()
  • 28. A trait can extend from multiple traits trait Reader { def read() { println 'Reading'} } trait Evaluator { def eval() { println 'Evaluating'} } trait Printer { def printer() { println 'Printing'} } trait Repl implements Reader, Evaluator, Printer { }
  • 29. The diamond problem trait GroovyProgrammer { def learn() { println 'Learning Traits'} } trait JavaProgrammer { def learn() { println 'Busy coding'} } class Dev implements JavaProgrammer, GroovyProgrammer {} new Dev().learn()
  • 30. Finer control on the diamond problem class Dev implements JavaProgrammer, GroovyProgrammer { def learn() { JavaProgrammer.super.learn() } }
  • 31. Applying traits at run time trait Flyable{ def fly(){ println "Flying.." } } class Person {} new Person().fly()
  • 32. Applying traits at run time def boardAPlane(Person person) { person.withTraits Flyable } def passenger = boardAPlane(new Person()) passenger.fly()
  • 34. Composing Behaviours trait UserContextAware { UserContext getUserContext(){ // implementation } } class ProductApi implements UserContextAware {} class PriceApi implements UserContextAware {}
  • 35. common fields trait Auditable { String createdBy String modifiedBy Date dateCreated Date lastUpdated } class Price implements Auditable { String productCode BigDecimal mrp BigDecimal sellingPrice }
  • 36. common fields - a trait approach Price groovyInActionToday = new Price( productCode: '9789351198260', mrp: 899, sellingPrice: 751, createdBy: 'admin', modifiedBy: 'rk' ) println groovyInActionToday.createdBy println groovyInActionToday.modifiedBy
  • 37. Chaining interface Manager { def approve(BigDecimal amount) }
  • 38. Chaining trait JuniorManager implements Manager { def approve(BigDecimal amount){ if(amount < 10000G){ println "Approved by JM” } else{ println "Sending to SM" super.approve() } } } trait SeniorManager implements Manager { def approve(BigDecimal amount){ println "Approved by SM" } }
  • 39. Chaining class FinanceDepartment implements SeniorManager, JuniorManager {} FinanceDepartment finance = new FinanceDepartment() finance.approve(3000) finance.approve(30000)