SlideShare a Scribd company logo
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 elds
trait Auditable {
String createdBy
String modifiedBy
Date dateCreated
Date lastUpdated
}
class Price implements Auditable {
String productCode
BigDecimal mrp
BigDecimal sellingPrice
}
common elds - 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!

More Related Content

PDF
Discovering functional treasure in idiomatic Groovy
Naresha K
 
PDF
Polyglot JVM
Arturo Herrero
 
PDF
Functional Programming with Groovy
Arturo Herrero
 
PPT
Polyglot Programming in the JVM
Andres Almiray
 
PDF
Ruby 程式語言簡介
Wen-Tien Chang
 
PPTX
Groovy!
Petr Giecek
 
PDF
tictactoe groovy
Paul King
 
KEY
Polyglot Grails
Marcin Gryszko
 
Discovering functional treasure in idiomatic Groovy
Naresha K
 
Polyglot JVM
Arturo Herrero
 
Functional Programming with Groovy
Arturo Herrero
 
Polyglot Programming in the JVM
Andres Almiray
 
Ruby 程式語言簡介
Wen-Tien Chang
 
Groovy!
Petr Giecek
 
tictactoe groovy
Paul King
 
Polyglot Grails
Marcin Gryszko
 

What's hot (20)

ODP
AST Transformations at JFokus
HamletDRC
 
PPT
Groovy for Java Developers
Andres Almiray
 
PDF
Groovy for java developers
Puneet Behl
 
KEY
groovy & grails - lecture 3
Alexandre Masselot
 
KEY
Groovy DSLs, from Beginner to Expert - Guillaume Laforge and Paul King - Spri...
Guillaume Laforge
 
PDF
GraphQL API in Clojure
Kent Ohashi
 
PDF
re-frame Ă  la spec
Kent Ohashi
 
ODP
Groovy Ast Transformations (greach)
HamletDRC
 
KEY
PHPSpec BDD for PHP
Marcello Duarte
 
PDF
GR8Conf 2009: What's New in Groovy 1.6? by Guillaume Laforge
GR8Conf
 
PDF
C++ for Java Developers (JavaZone Academy 2018)
Patricia Aas
 
KEY
Mirah Talk for Boulder Ruby Group
baroquebobcat
 
PDF
Diving into HHVM Extensions (PHPNW Conference 2015)
James Titcumb
 
PDF
Grooscript gr8conf
GR8Conf
 
PDF
Natural Language Toolkit (NLTK), Basics
Prakash Pimpale
 
PDF
Lift off with Groovy 2 at JavaOne 2013
Guillaume Laforge
 
PPTX
C# 7.0 Hacks and Features
Abhishek Sur
 
PDF
Go 1.10 Release Party - PDX Go
Rodolfo Carvalho
 
PDF
"Simple Made Easy" Made Easy
Kent Ohashi
 
PPTX
Building High Performance Web Applications and Sites
goodfriday
 
AST Transformations at JFokus
HamletDRC
 
Groovy for Java Developers
Andres Almiray
 
Groovy for java developers
Puneet Behl
 
groovy & grails - lecture 3
Alexandre Masselot
 
Groovy DSLs, from Beginner to Expert - Guillaume Laforge and Paul King - Spri...
Guillaume Laforge
 
GraphQL API in Clojure
Kent Ohashi
 
re-frame Ă  la spec
Kent Ohashi
 
Groovy Ast Transformations (greach)
HamletDRC
 
PHPSpec BDD for PHP
Marcello Duarte
 
GR8Conf 2009: What's New in Groovy 1.6? by Guillaume Laforge
GR8Conf
 
C++ for Java Developers (JavaZone Academy 2018)
Patricia Aas
 
Mirah Talk for Boulder Ruby Group
baroquebobcat
 
Diving into HHVM Extensions (PHPNW Conference 2015)
James Titcumb
 
Grooscript gr8conf
GR8Conf
 
Natural Language Toolkit (NLTK), Basics
Prakash Pimpale
 
Lift off with Groovy 2 at JavaOne 2013
Guillaume Laforge
 
C# 7.0 Hacks and Features
Abhishek Sur
 
Go 1.10 Release Party - PDX Go
Rodolfo Carvalho
 
"Simple Made Easy" Made Easy
Kent Ohashi
 
Building High Performance Web Applications and Sites
goodfriday
 
Ad

Viewers also liked (13)

PDF
science 8
Jennifer Guidi
 
PDF
G30022_Karen Devine_DL_Print
Dr. Karen Devine
 
PDF
Paper
Saumya Dhup
 
ODP
Araba
jakintzaikastola5c
 
PPTX
Trabajo final
monicamaria villada
 
DOCX
ARC FLASH MITIGATION USING ACTIVE HIGH-SPEED SWITCHING
Amit Chakraborty
 
PDF
Biostatistics Workshop: Regression
HopkinsCFAR
 
PDF
Comandos usados en kali linux
Jhon TRUJILLO
 
PDF
Overview of JSI & USAID | DELIVER PROJECT Reproductive Health Supplies Monito...
JSI
 
PPTX
Georgia txostena
jakintzaikastola5c
 
PPTX
Kamal final presentation eee reb- comilla
siam hossain
 
PDF
Karen Devine Ind NUI Seanad Proposals
Dr. Karen Devine
 
DOC
ANKIT RASUMA
Ankit Sethi
 
science 8
Jennifer Guidi
 
G30022_Karen Devine_DL_Print
Dr. Karen Devine
 
Paper
Saumya Dhup
 
Trabajo final
monicamaria villada
 
ARC FLASH MITIGATION USING ACTIVE HIGH-SPEED SWITCHING
Amit Chakraborty
 
Biostatistics Workshop: Regression
HopkinsCFAR
 
Comandos usados en kali linux
Jhon TRUJILLO
 
Overview of JSI & USAID | DELIVER PROJECT Reproductive Health Supplies Monito...
JSI
 
Georgia txostena
jakintzaikastola5c
 
Kamal final presentation eee reb- comilla
siam hossain
 
Karen Devine Ind NUI Seanad Proposals
Dr. Karen Devine
 
ANKIT RASUMA
Ankit Sethi
 
Ad

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

PPTX
Oop2010 Scala Presentation Stal
Michael Stal
 
PDF
Polyglot Programming @ Jax.de 2010
Andres Almiray
 
PDF
Polyglot Programming in the JVM - 33rd Degree
Andres Almiray
 
PPTX
A (too) Short Introduction to Scala
Riccardo Cardin
 
PPT
2007 09 10 Fzi Training Groovy Grails V Ws
loffenauer
 
PDF
A Sceptical Guide to Functional Programming
Garth Gilmour
 
PDF
Game Design and Development Workshop Day 1
Troy Miles
 
PDF
Polyglot Programming in the JVM - Øredev
Andres Almiray
 
PPTX
Dart structured web apps
chrisbuckett
 
PPTX
Dart, unicorns and rainbows
chrisbuckett
 
PDF
JavaScript Core
François Sarradin
 
PDF
Exploring Ceylon with Gavin King - JUG BB Talk - Belrin 2014
hwilming
 
PPTX
JavaScript (without DOM)
Piyush Katariya
 
PDF
Advanced Python, Part 1
Zaar Hai
 
KEY
Language supports it
Niranjan Paranjape
 
PPT
Java script unleashed
Dibyendu Tiwary
 
PPT
Douglas Crockford Presentation Goodparts
Ajax Experience 2009
 
PDF
TDAD
Daniele Bottillo
 
PDF
OOPS JavaScript Interview Questions PDF By ScholarHat
Scholarhat
 
PDF
What’s new in Google Dart - Seth Ladd
jaxconf
 
Oop2010 Scala Presentation Stal
Michael Stal
 
Polyglot Programming @ Jax.de 2010
Andres Almiray
 
Polyglot Programming in the JVM - 33rd Degree
Andres Almiray
 
A (too) Short Introduction to Scala
Riccardo Cardin
 
2007 09 10 Fzi Training Groovy Grails V Ws
loffenauer
 
A Sceptical Guide to Functional Programming
Garth Gilmour
 
Game Design and Development Workshop Day 1
Troy Miles
 
Polyglot Programming in the JVM - Øredev
Andres Almiray
 
Dart structured web apps
chrisbuckett
 
Dart, unicorns and rainbows
chrisbuckett
 
JavaScript Core
François Sarradin
 
Exploring Ceylon with Gavin King - JUG BB Talk - Belrin 2014
hwilming
 
JavaScript (without DOM)
Piyush Katariya
 
Advanced Python, Part 1
Zaar Hai
 
Language supports it
Niranjan Paranjape
 
Java script unleashed
Dibyendu Tiwary
 
Douglas Crockford Presentation Goodparts
Ajax Experience 2009
 
OOPS JavaScript Interview Questions PDF By ScholarHat
Scholarhat
 
What’s new in Google Dart - Seth Ladd
jaxconf
 

More from Naresha K (20)

PDF
The Groovy Way of Testing with Spock
Naresha K
 
PDF
Evolving with Java - How to Remain Effective
Naresha K
 
PDF
Take Control of your Integration Testing with TestContainers
Naresha K
 
PDF
Implementing Resilience with Micronaut
Naresha K
 
PDF
Take Control of your Integration Testing with TestContainers
Naresha K
 
PDF
Favouring Composition - The Groovy Way
Naresha K
 
PDF
Effective Java with Groovy - How Language Influences Adoption of Good Practices
Naresha K
 
PDF
What's in Groovy for Functional Programming
Naresha K
 
PDF
Effective Java with Groovy & Kotlin - How Languages Influence Adoption of Goo...
Naresha K
 
PDF
Effective Java with Groovy & Kotlin How Languages Influence Adoption of Good ...
Naresha K
 
PDF
Eclipse Collections, Java Streams & Vavr - What's in them for Functional Pro...
Naresha K
 
PDF
Implementing Cloud-Native Architectural Patterns with Micronaut
Naresha K
 
PDF
Groovy - Why and Where?
Naresha K
 
PDF
Leveraging Micronaut on AWS Lambda
Naresha K
 
PDF
Groovy Refactoring Patterns
Naresha K
 
PDF
Implementing Cloud-native Architectural Patterns with Micronaut
Naresha K
 
PDF
Effective Java with Groovy
Naresha K
 
PDF
Evolving with Java - How to remain Relevant and Effective
Naresha K
 
PDF
Effective Java with Groovy - How Language can Influence Good Practices
Naresha K
 
PDF
Beyond Lambdas & Streams - Functional Fluency in Java
Naresha K
 
The Groovy Way of Testing with Spock
Naresha K
 
Evolving with Java - How to Remain Effective
Naresha K
 
Take Control of your Integration Testing with TestContainers
Naresha K
 
Implementing Resilience with Micronaut
Naresha K
 
Take Control of your Integration Testing with TestContainers
Naresha K
 
Favouring Composition - The Groovy Way
Naresha K
 
Effective Java with Groovy - How Language Influences Adoption of Good Practices
Naresha K
 
What's in Groovy for Functional Programming
Naresha K
 
Effective Java with Groovy & Kotlin - How Languages Influence Adoption of Goo...
Naresha K
 
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...
Naresha K
 
Implementing Cloud-Native Architectural Patterns with Micronaut
Naresha K
 
Groovy - Why and Where?
Naresha K
 
Leveraging Micronaut on AWS Lambda
Naresha K
 
Groovy Refactoring Patterns
Naresha K
 
Implementing Cloud-native Architectural Patterns with Micronaut
Naresha K
 
Effective Java with Groovy
Naresha K
 
Evolving with Java - How to remain Relevant and Effective
Naresha K
 
Effective Java with Groovy - How Language can Influence Good Practices
Naresha K
 
Beyond Lambdas & Streams - Functional Fluency in Java
Naresha K
 

Recently uploaded (20)

PDF
Brief History of Internet - Early Days of Internet
sutharharshit158
 
PDF
Software Development Methodologies in 2025
KodekX
 
PPTX
Simple and concise overview about Quantum computing..pptx
mughal641
 
PPTX
Dev Dives: Automate, test, and deploy in one place—with Unified Developer Exp...
AndreeaTom
 
PDF
Make GenAI investments go further with the Dell AI Factory
Principled Technologies
 
PDF
Oracle AI Vector Search- Getting Started and what's new in 2025- AIOUG Yatra ...
Sandesh Rao
 
PDF
Structs to JSON: How Go Powers REST APIs
Emily Achieng
 
PDF
CIFDAQ's Market Wrap : Bears Back in Control?
CIFDAQ
 
PDF
How ETL Control Logic Keeps Your Pipelines Safe and Reliable.pdf
Stryv Solutions Pvt. Ltd.
 
PDF
Trying to figure out MCP by actually building an app from scratch with open s...
Julien SIMON
 
PDF
OFFOFFBOX™ – A New Era for African Film | Startup Presentation
ambaicciwalkerbrian
 
PPTX
What-is-the-World-Wide-Web -- Introduction
tonifi9488
 
PPTX
cloud computing vai.pptx for the project
vaibhavdobariyal79
 
PPTX
OA presentation.pptx OA presentation.pptx
pateldhruv002338
 
PDF
Using Anchore and DefectDojo to Stand Up Your DevSecOps Function
Anchore
 
PPTX
Agile Chennai 18-19 July 2025 Ideathon | AI Powered Microfinance Literacy Gui...
AgileNetwork
 
PDF
Orbitly Pitch Deck|A Mission-Driven Platform for Side Project Collaboration (...
zz41354899
 
PPTX
The-Ethical-Hackers-Imperative-Safeguarding-the-Digital-Frontier.pptx
sujalchauhan1305
 
PPTX
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
PPTX
AI in Daily Life: How Artificial Intelligence Helps Us Every Day
vanshrpatil7
 
Brief History of Internet - Early Days of Internet
sutharharshit158
 
Software Development Methodologies in 2025
KodekX
 
Simple and concise overview about Quantum computing..pptx
mughal641
 
Dev Dives: Automate, test, and deploy in one place—with Unified Developer Exp...
AndreeaTom
 
Make GenAI investments go further with the Dell AI Factory
Principled Technologies
 
Oracle AI Vector Search- Getting Started and what's new in 2025- AIOUG Yatra ...
Sandesh Rao
 
Structs to JSON: How Go Powers REST APIs
Emily Achieng
 
CIFDAQ's Market Wrap : Bears Back in Control?
CIFDAQ
 
How ETL Control Logic Keeps Your Pipelines Safe and Reliable.pdf
Stryv Solutions Pvt. Ltd.
 
Trying to figure out MCP by actually building an app from scratch with open s...
Julien SIMON
 
OFFOFFBOX™ – A New Era for African Film | Startup Presentation
ambaicciwalkerbrian
 
What-is-the-World-Wide-Web -- Introduction
tonifi9488
 
cloud computing vai.pptx for the project
vaibhavdobariyal79
 
OA presentation.pptx OA presentation.pptx
pateldhruv002338
 
Using Anchore and DefectDojo to Stand Up Your DevSecOps Function
Anchore
 
Agile Chennai 18-19 July 2025 Ideathon | AI Powered Microfinance Literacy Gui...
AgileNetwork
 
Orbitly Pitch Deck|A Mission-Driven Platform for Side Project Collaboration (...
zz41354899
 
The-Ethical-Hackers-Imperative-Safeguarding-the-Digital-Frontier.pptx
sujalchauhan1305
 
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
AI in Daily Life: How Artificial Intelligence Helps Us Every Day
vanshrpatil7
 

Designing with Groovy Traits - Gr8Conf India

  • 1. Designing with Groovy Traits Naresha K Chief Technologist Channel Bridge Software Labs [email protected] @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 elds trait Auditable { String createdBy String modifiedBy Date dateCreated Date lastUpdated } class Price implements Auditable { String productCode BigDecimal mrp BigDecimal sellingPrice }
  • 36. common elds - 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)