SlideShare a Scribd company logo
1 of 29
Download to read offline
Mopping Up With Groovy*




Brent Snook
brent@fuglylogic.com    *obligatory lame pun
What is a MOP?
Duck Typing

• POJ (Plain Old Java) – you can
  interact with an object based on
  what it is (Class)

• Groovy – you can interact based on
  which properties and methods are
  available on the object (MetaClass)
Modifying Class Behaviour at Runtime
• AKA Monkey Patching
• AKA Duck Punching
• see whytheluckystiff's My Complete List Of
  Substitute Names For The Maneuver We
  Now Know To Be Monkeypatching
Why Do It?
• it has an amusing name
• it makes you more attractive to the opposite
  (or same) sex
• it allows you to write more succinct,
  expressive code
But I Feel Dirty …
• you may need to change how you
  think about things
• the laws are different in the duck
  typing universe
• question old attitudes and
  conventions
How Does Groovy
Implement a MOP?
The MetaClass
• every Groovy object has one
• richer introspection and invocation interface
  than Java Class
• you can set the MetaClass for an object to
  change the rules for how it can behave
The Final Shape of an Object


         Added Methods

 Class   ++ Properties
                         =   MetaClass
The ExpandoMetaClass
• core of Groovy metaprogramming
• allows you to customise behaviour on the fly
• when you ask a newly created GroovyObject
  for its metaClass, this is what you get back



                   <<interface>>
                                   ExpandoMetaClass
 GroovyObject       MetaClass
What Can You Do With It?
•   borrow methods from other classes
•   add or override constructors
•   dynamically create method names
•   add methods to interfaces
•   add or override instance methods
•   add or override properties
•   add or override static methods
    http://groovy.codehaus.org/ExpandoMetaClass
Adding a Class Method
Adding a Property
Overriding an Instance Method
The Same In Ruby
Invocation/Access Hooks
•   invokeMethod
•   getProperty
•   setProperty
•   propertyMissing
•   methodMissing
AOP with invokeMethod
Flexible Methods with methodMissing
MOP!!...huh! What is
  it Good For?
Syntactic Sugar
Mocking/Stubbing Behaviour
DSL - The Original Class
Adding Some MOP Trickery
Icecream Time
Replacing/Simplifying Frameworks
              and Patterns
•   Spring, EasyMock/JMock
•   circumvent the rigidity of the Java language
•   patterns like Builder
•   Dependency Injection
•   with Groovy these can be simplified or
    disappear altogether
Finally
The Pros and Cons
Pros                 Cons
• flexibility        • sometimes clunky
• extensibility        syntax
• power              • slower performance
With Great Power…
• don’t get wowed by the coolness factor
• remember to KISS
• the end result should be more
  understandable, not less
• keep pushing the boundaries but exercise
  judgment
Further Reading and References
•   http://www.gnu.org/software/guile/docs/goops/Metaobjects-
    and-the-Metaobject-Protocol.html#Metaobjects%20and%20the
    %20Metaobject%20Protocol
•   http://en.wikipedia.org/wiki/Meta-object_protocol
•   http://www.grails-exchange.com/files/GraemeRocher%20-
    %20Keynote-DynamicGroovyMetaMagic.pdf
•   http://groovy.codehaus.org/ExpandoMetaClass
•   http://whytheluckystiff.net/articles/
    seeingMetaclassesClearly.html
•   http://groovy.codehaus.org/ExpandoMetaClass
•   http://hackety.org/2007/08/10/
    myCompleteListOfSubstitutePhrasesForTheActWeNowKnowTo
    BeMonkeypatching.html
•   Groovy In Action

More Related Content

Viewers also liked

Viewers also liked (9)

General Cleaning Module 3 PowerPoint
General Cleaning Module 3 PowerPointGeneral Cleaning Module 3 PowerPoint
General Cleaning Module 3 PowerPoint
 
IRC Internship reflection
IRC Internship reflectionIRC Internship reflection
IRC Internship reflection
 
Clil
ClilClil
Clil
 
General Cleaning Module 2 PowerPoint
General Cleaning Module 2 PowerPointGeneral Cleaning Module 2 PowerPoint
General Cleaning Module 2 PowerPoint
 
Bed making
Bed makingBed making
Bed making
 
Bed making
Bed makingBed making
Bed making
 
FATbit Immortalizes Neil Patel With Graphic Wiki – Review & Share!
FATbit Immortalizes Neil Patel With Graphic Wiki – Review & Share!FATbit Immortalizes Neil Patel With Graphic Wiki – Review & Share!
FATbit Immortalizes Neil Patel With Graphic Wiki – Review & Share!
 
Housekeeping dept.
Housekeeping dept.Housekeeping dept.
Housekeeping dept.
 
Hotel housekeeping
Hotel housekeepingHotel housekeeping
Hotel housekeeping
 

Similar to Mopping Up With Groovy

Groovy And Grails Introduction
Groovy And Grails IntroductionGroovy And Grails Introduction
Groovy And Grails IntroductionEric Weimer
 
Deliverance: Plone theming without the learning curve from Plone Symposium Ea...
Deliverance: Plone theming without the learning curve from Plone Symposium Ea...Deliverance: Plone theming without the learning curve from Plone Symposium Ea...
Deliverance: Plone theming without the learning curve from Plone Symposium Ea...Jazkarta, Inc.
 
Groovy Metaprogramming for Dummies
Groovy Metaprogramming for DummiesGroovy Metaprogramming for Dummies
Groovy Metaprogramming for DummiesDarren Cruse
 
JavaOne 2008 - TS-5793 - Groovy and Grails, changing the landscape of Java EE...
JavaOne 2008 - TS-5793 - Groovy and Grails, changing the landscape of Java EE...JavaOne 2008 - TS-5793 - Groovy and Grails, changing the landscape of Java EE...
JavaOne 2008 - TS-5793 - Groovy and Grails, changing the landscape of Java EE...Guillaume Laforge
 
Feelin' Groovy: An Afternoon of Reflexive Metaprogramming
Feelin' Groovy: An Afternoon of Reflexive MetaprogrammingFeelin' Groovy: An Afternoon of Reflexive Metaprogramming
Feelin' Groovy: An Afternoon of Reflexive MetaprogrammingMatt Stine
 
Java to Golang: An intro by Ryan Dawson Seldon.io
Java to Golang: An intro by Ryan Dawson Seldon.ioJava to Golang: An intro by Ryan Dawson Seldon.io
Java to Golang: An intro by Ryan Dawson Seldon.ioMauricio (Salaboy) Salatino
 
Rapid Prototyping With J Query
Rapid Prototyping With J QueryRapid Prototyping With J Query
Rapid Prototyping With J QueryBootstrap
 
Ajax Tutorial
Ajax TutorialAjax Tutorial
Ajax Tutorialoscon2007
 
New Ideas for Old Code - Greach
New Ideas for Old Code - GreachNew Ideas for Old Code - Greach
New Ideas for Old Code - GreachHamletDRC
 
Grails @ Java User Group Silicon Valley
Grails @ Java User Group Silicon ValleyGrails @ Java User Group Silicon Valley
Grails @ Java User Group Silicon ValleySven Haiges
 
Grooming with Groovy
Grooming with GroovyGrooming with Groovy
Grooming with GroovyDhaval Dalal
 
Metaprogramming Techniques In Groovy And Grails
Metaprogramming Techniques In Groovy And GrailsMetaprogramming Techniques In Groovy And Grails
Metaprogramming Techniques In Groovy And GrailszenMonkey
 
Introduction to Groovy runtime metaprogramming and AST transforms
Introduction to Groovy runtime metaprogramming and AST transformsIntroduction to Groovy runtime metaprogramming and AST transforms
Introduction to Groovy runtime metaprogramming and AST transformsMarcin Grzejszczak
 
Ola Bini Evolving The Java Platform
Ola Bini Evolving The Java PlatformOla Bini Evolving The Java Platform
Ola Bini Evolving The Java Platformdeimos
 
Metaprogramming with Groovy
Metaprogramming with GroovyMetaprogramming with Groovy
Metaprogramming with GroovyAli Tanwir
 

Similar to Mopping Up With Groovy (20)

Groovy And Grails Introduction
Groovy And Grails IntroductionGroovy And Grails Introduction
Groovy And Grails Introduction
 
Deliverance: Plone theming without the learning curve from Plone Symposium Ea...
Deliverance: Plone theming without the learning curve from Plone Symposium Ea...Deliverance: Plone theming without the learning curve from Plone Symposium Ea...
Deliverance: Plone theming without the learning curve from Plone Symposium Ea...
 
Groovy Metaprogramming for Dummies
Groovy Metaprogramming for DummiesGroovy Metaprogramming for Dummies
Groovy Metaprogramming for Dummies
 
JavaOne 2008 - TS-5793 - Groovy and Grails, changing the landscape of Java EE...
JavaOne 2008 - TS-5793 - Groovy and Grails, changing the landscape of Java EE...JavaOne 2008 - TS-5793 - Groovy and Grails, changing the landscape of Java EE...
JavaOne 2008 - TS-5793 - Groovy and Grails, changing the landscape of Java EE...
 
Groovy MOPping
Groovy MOPpingGroovy MOPping
Groovy MOPping
 
Feelin' Groovy: An Afternoon of Reflexive Metaprogramming
Feelin' Groovy: An Afternoon of Reflexive MetaprogrammingFeelin' Groovy: An Afternoon of Reflexive Metaprogramming
Feelin' Groovy: An Afternoon of Reflexive Metaprogramming
 
Java to Golang: An intro by Ryan Dawson Seldon.io
Java to Golang: An intro by Ryan Dawson Seldon.ioJava to Golang: An intro by Ryan Dawson Seldon.io
Java to Golang: An intro by Ryan Dawson Seldon.io
 
Grooscript greach
Grooscript greachGrooscript greach
Grooscript greach
 
Whats New In Groovy 1.6?
Whats New In Groovy 1.6?Whats New In Groovy 1.6?
Whats New In Groovy 1.6?
 
Rapid Prototyping With J Query
Rapid Prototyping With J QueryRapid Prototyping With J Query
Rapid Prototyping With J Query
 
Ajax Tutorial
Ajax TutorialAjax Tutorial
Ajax Tutorial
 
New Ideas for Old Code - Greach
New Ideas for Old Code - GreachNew Ideas for Old Code - Greach
New Ideas for Old Code - Greach
 
Groovy And Grails
Groovy And GrailsGroovy And Grails
Groovy And Grails
 
Grails @ Java User Group Silicon Valley
Grails @ Java User Group Silicon ValleyGrails @ Java User Group Silicon Valley
Grails @ Java User Group Silicon Valley
 
Grooming with Groovy
Grooming with GroovyGrooming with Groovy
Grooming with Groovy
 
Metaprogramming Techniques In Groovy And Grails
Metaprogramming Techniques In Groovy And GrailsMetaprogramming Techniques In Groovy And Grails
Metaprogramming Techniques In Groovy And Grails
 
Introduction to Groovy runtime metaprogramming and AST transforms
Introduction to Groovy runtime metaprogramming and AST transformsIntroduction to Groovy runtime metaprogramming and AST transforms
Introduction to Groovy runtime metaprogramming and AST transforms
 
Ola Bini Evolving The Java Platform
Ola Bini Evolving The Java PlatformOla Bini Evolving The Java Platform
Ola Bini Evolving The Java Platform
 
Metaprogramming with Groovy
Metaprogramming with GroovyMetaprogramming with Groovy
Metaprogramming with Groovy
 
MetaProgramming with Groovy
MetaProgramming with GroovyMetaProgramming with Groovy
MetaProgramming with Groovy
 

More from fuglylogic

It's Full of Stars - Building, Measuring and Learning With Events
It's Full of Stars - Building, Measuring and Learning With EventsIt's Full of Stars - Building, Measuring and Learning With Events
It's Full of Stars - Building, Measuring and Learning With Eventsfuglylogic
 
The Daily Grind - Milling Stories to Reduce Risk
The Daily Grind - Milling Stories to Reduce RiskThe Daily Grind - Milling Stories to Reduce Risk
The Daily Grind - Milling Stories to Reduce Riskfuglylogic
 
Agile/UX: Making the Marriage Work
Agile/UX: Making the Marriage WorkAgile/UX: Making the Marriage Work
Agile/UX: Making the Marriage Workfuglylogic
 
Microservices Without the Macrocost
Microservices Without the MacrocostMicroservices Without the Macrocost
Microservices Without the Macrocostfuglylogic
 
Building the Right Thing
Building the Right ThingBuilding the Right Thing
Building the Right Thingfuglylogic
 
Bumps - Live Features with Google Wave and Cucumber
Bumps - Live Features with Google Wave and CucumberBumps - Live Features with Google Wave and Cucumber
Bumps - Live Features with Google Wave and Cucumberfuglylogic
 
Wrangling Your Stuff
Wrangling Your StuffWrangling Your Stuff
Wrangling Your Stufffuglylogic
 
BDD: What's in it for me?
BDD: What's in it for me?BDD: What's in it for me?
BDD: What's in it for me?fuglylogic
 

More from fuglylogic (10)

It's Full of Stars - Building, Measuring and Learning With Events
It's Full of Stars - Building, Measuring and Learning With EventsIt's Full of Stars - Building, Measuring and Learning With Events
It's Full of Stars - Building, Measuring and Learning With Events
 
The Daily Grind - Milling Stories to Reduce Risk
The Daily Grind - Milling Stories to Reduce RiskThe Daily Grind - Milling Stories to Reduce Risk
The Daily Grind - Milling Stories to Reduce Risk
 
Agile/UX: Making the Marriage Work
Agile/UX: Making the Marriage WorkAgile/UX: Making the Marriage Work
Agile/UX: Making the Marriage Work
 
Microservices Without the Macrocost
Microservices Without the MacrocostMicroservices Without the Macrocost
Microservices Without the Macrocost
 
Building the Right Thing
Building the Right ThingBuilding the Right Thing
Building the Right Thing
 
Bumps - Live Features with Google Wave and Cucumber
Bumps - Live Features with Google Wave and CucumberBumps - Live Features with Google Wave and Cucumber
Bumps - Live Features with Google Wave and Cucumber
 
Wrangling Your Stuff
Wrangling Your StuffWrangling Your Stuff
Wrangling Your Stuff
 
Trio of Gems
Trio of GemsTrio of Gems
Trio of Gems
 
BDD: What's in it for me?
BDD: What's in it for me?BDD: What's in it for me?
BDD: What's in it for me?
 
We All Wii
We All WiiWe All Wii
We All Wii
 

Recently uploaded

Introduction to FDO and How It works Applications _ Richard at FIDO Alliance.pdf
Introduction to FDO and How It works Applications _ Richard at FIDO Alliance.pdfIntroduction to FDO and How It works Applications _ Richard at FIDO Alliance.pdf
Introduction to FDO and How It works Applications _ Richard at FIDO Alliance.pdfFIDO Alliance
 
The Value of Certifying Products for FDO _ Paul at FIDO Alliance.pdf
The Value of Certifying Products for FDO _ Paul at FIDO Alliance.pdfThe Value of Certifying Products for FDO _ Paul at FIDO Alliance.pdf
The Value of Certifying Products for FDO _ Paul at FIDO Alliance.pdfFIDO Alliance
 
Simplified FDO Manufacturing Flow with TPMs _ Liam at Infineon.pdf
Simplified FDO Manufacturing Flow with TPMs _ Liam at Infineon.pdfSimplified FDO Manufacturing Flow with TPMs _ Liam at Infineon.pdf
Simplified FDO Manufacturing Flow with TPMs _ Liam at Infineon.pdfFIDO Alliance
 
ERP Contender Series: Acumatica vs. Sage Intacct
ERP Contender Series: Acumatica vs. Sage IntacctERP Contender Series: Acumatica vs. Sage Intacct
ERP Contender Series: Acumatica vs. Sage IntacctBrainSell Technologies
 
Easier, Faster, and More Powerful – Notes Document Properties Reimagined
Easier, Faster, and More Powerful – Notes Document Properties ReimaginedEasier, Faster, and More Powerful – Notes Document Properties Reimagined
Easier, Faster, and More Powerful – Notes Document Properties Reimaginedpanagenda
 
The Metaverse: Are We There Yet?
The  Metaverse:    Are   We  There  Yet?The  Metaverse:    Are   We  There  Yet?
The Metaverse: Are We There Yet?Mark Billinghurst
 
Tales from a Passkey Provider Progress from Awareness to Implementation.pptx
Tales from a Passkey Provider  Progress from Awareness to Implementation.pptxTales from a Passkey Provider  Progress from Awareness to Implementation.pptx
Tales from a Passkey Provider Progress from Awareness to Implementation.pptxFIDO Alliance
 
TEST BANK For, Information Technology Project Management 9th Edition Kathy Sc...
TEST BANK For, Information Technology Project Management 9th Edition Kathy Sc...TEST BANK For, Information Technology Project Management 9th Edition Kathy Sc...
TEST BANK For, Information Technology Project Management 9th Edition Kathy Sc...marcuskenyatta275
 
Structuring Teams and Portfolios for Success
Structuring Teams and Portfolios for SuccessStructuring Teams and Portfolios for Success
Structuring Teams and Portfolios for SuccessUXDXConf
 
Continuing Bonds Through AI: A Hermeneutic Reflection on Thanabots
Continuing Bonds Through AI: A Hermeneutic Reflection on ThanabotsContinuing Bonds Through AI: A Hermeneutic Reflection on Thanabots
Continuing Bonds Through AI: A Hermeneutic Reflection on ThanabotsLeah Henrickson
 
AI mind or machine power point presentation
AI mind or machine power point presentationAI mind or machine power point presentation
AI mind or machine power point presentationyogeshlabana357357
 
Human Expert Website Manual WCAG 2.0 2.1 2.2 Audit - Digital Accessibility Au...
Human Expert Website Manual WCAG 2.0 2.1 2.2 Audit - Digital Accessibility Au...Human Expert Website Manual WCAG 2.0 2.1 2.2 Audit - Digital Accessibility Au...
Human Expert Website Manual WCAG 2.0 2.1 2.2 Audit - Digital Accessibility Au...Skynet Technologies
 
WebAssembly is Key to Better LLM Performance
WebAssembly is Key to Better LLM PerformanceWebAssembly is Key to Better LLM Performance
WebAssembly is Key to Better LLM PerformanceSamy Fodil
 
How we scaled to 80K users by doing nothing!.pdf
How we scaled to 80K users by doing nothing!.pdfHow we scaled to 80K users by doing nothing!.pdf
How we scaled to 80K users by doing nothing!.pdfSrushith Repakula
 
WebRTC and SIP not just audio and video @ OpenSIPS 2024
WebRTC and SIP not just audio and video @ OpenSIPS 2024WebRTC and SIP not just audio and video @ OpenSIPS 2024
WebRTC and SIP not just audio and video @ OpenSIPS 2024Lorenzo Miniero
 
Extensible Python: Robustness through Addition - PyCon 2024
Extensible Python: Robustness through Addition - PyCon 2024Extensible Python: Robustness through Addition - PyCon 2024
Extensible Python: Robustness through Addition - PyCon 2024Patrick Viafore
 
Event-Driven Architecture Masterclass: Engineering a Robust, High-performance...
Event-Driven Architecture Masterclass: Engineering a Robust, High-performance...Event-Driven Architecture Masterclass: Engineering a Robust, High-performance...
Event-Driven Architecture Masterclass: Engineering a Robust, High-performance...ScyllaDB
 
Collecting & Temporal Analysis of Behavioral Web Data - Tales From The Inside
Collecting & Temporal Analysis of Behavioral Web Data - Tales From The InsideCollecting & Temporal Analysis of Behavioral Web Data - Tales From The Inside
Collecting & Temporal Analysis of Behavioral Web Data - Tales From The InsideStefan Dietze
 
Easier, Faster, and More Powerful – Alles Neu macht der Mai -Wir durchleuchte...
Easier, Faster, and More Powerful – Alles Neu macht der Mai -Wir durchleuchte...Easier, Faster, and More Powerful – Alles Neu macht der Mai -Wir durchleuchte...
Easier, Faster, and More Powerful – Alles Neu macht der Mai -Wir durchleuchte...panagenda
 
Hyatt driving innovation and exceptional customer experiences with FIDO passw...
Hyatt driving innovation and exceptional customer experiences with FIDO passw...Hyatt driving innovation and exceptional customer experiences with FIDO passw...
Hyatt driving innovation and exceptional customer experiences with FIDO passw...FIDO Alliance
 

Recently uploaded (20)

Introduction to FDO and How It works Applications _ Richard at FIDO Alliance.pdf
Introduction to FDO and How It works Applications _ Richard at FIDO Alliance.pdfIntroduction to FDO and How It works Applications _ Richard at FIDO Alliance.pdf
Introduction to FDO and How It works Applications _ Richard at FIDO Alliance.pdf
 
The Value of Certifying Products for FDO _ Paul at FIDO Alliance.pdf
The Value of Certifying Products for FDO _ Paul at FIDO Alliance.pdfThe Value of Certifying Products for FDO _ Paul at FIDO Alliance.pdf
The Value of Certifying Products for FDO _ Paul at FIDO Alliance.pdf
 
Simplified FDO Manufacturing Flow with TPMs _ Liam at Infineon.pdf
Simplified FDO Manufacturing Flow with TPMs _ Liam at Infineon.pdfSimplified FDO Manufacturing Flow with TPMs _ Liam at Infineon.pdf
Simplified FDO Manufacturing Flow with TPMs _ Liam at Infineon.pdf
 
ERP Contender Series: Acumatica vs. Sage Intacct
ERP Contender Series: Acumatica vs. Sage IntacctERP Contender Series: Acumatica vs. Sage Intacct
ERP Contender Series: Acumatica vs. Sage Intacct
 
Easier, Faster, and More Powerful – Notes Document Properties Reimagined
Easier, Faster, and More Powerful – Notes Document Properties ReimaginedEasier, Faster, and More Powerful – Notes Document Properties Reimagined
Easier, Faster, and More Powerful – Notes Document Properties Reimagined
 
The Metaverse: Are We There Yet?
The  Metaverse:    Are   We  There  Yet?The  Metaverse:    Are   We  There  Yet?
The Metaverse: Are We There Yet?
 
Tales from a Passkey Provider Progress from Awareness to Implementation.pptx
Tales from a Passkey Provider  Progress from Awareness to Implementation.pptxTales from a Passkey Provider  Progress from Awareness to Implementation.pptx
Tales from a Passkey Provider Progress from Awareness to Implementation.pptx
 
TEST BANK For, Information Technology Project Management 9th Edition Kathy Sc...
TEST BANK For, Information Technology Project Management 9th Edition Kathy Sc...TEST BANK For, Information Technology Project Management 9th Edition Kathy Sc...
TEST BANK For, Information Technology Project Management 9th Edition Kathy Sc...
 
Structuring Teams and Portfolios for Success
Structuring Teams and Portfolios for SuccessStructuring Teams and Portfolios for Success
Structuring Teams and Portfolios for Success
 
Continuing Bonds Through AI: A Hermeneutic Reflection on Thanabots
Continuing Bonds Through AI: A Hermeneutic Reflection on ThanabotsContinuing Bonds Through AI: A Hermeneutic Reflection on Thanabots
Continuing Bonds Through AI: A Hermeneutic Reflection on Thanabots
 
AI mind or machine power point presentation
AI mind or machine power point presentationAI mind or machine power point presentation
AI mind or machine power point presentation
 
Human Expert Website Manual WCAG 2.0 2.1 2.2 Audit - Digital Accessibility Au...
Human Expert Website Manual WCAG 2.0 2.1 2.2 Audit - Digital Accessibility Au...Human Expert Website Manual WCAG 2.0 2.1 2.2 Audit - Digital Accessibility Au...
Human Expert Website Manual WCAG 2.0 2.1 2.2 Audit - Digital Accessibility Au...
 
WebAssembly is Key to Better LLM Performance
WebAssembly is Key to Better LLM PerformanceWebAssembly is Key to Better LLM Performance
WebAssembly is Key to Better LLM Performance
 
How we scaled to 80K users by doing nothing!.pdf
How we scaled to 80K users by doing nothing!.pdfHow we scaled to 80K users by doing nothing!.pdf
How we scaled to 80K users by doing nothing!.pdf
 
WebRTC and SIP not just audio and video @ OpenSIPS 2024
WebRTC and SIP not just audio and video @ OpenSIPS 2024WebRTC and SIP not just audio and video @ OpenSIPS 2024
WebRTC and SIP not just audio and video @ OpenSIPS 2024
 
Extensible Python: Robustness through Addition - PyCon 2024
Extensible Python: Robustness through Addition - PyCon 2024Extensible Python: Robustness through Addition - PyCon 2024
Extensible Python: Robustness through Addition - PyCon 2024
 
Event-Driven Architecture Masterclass: Engineering a Robust, High-performance...
Event-Driven Architecture Masterclass: Engineering a Robust, High-performance...Event-Driven Architecture Masterclass: Engineering a Robust, High-performance...
Event-Driven Architecture Masterclass: Engineering a Robust, High-performance...
 
Collecting & Temporal Analysis of Behavioral Web Data - Tales From The Inside
Collecting & Temporal Analysis of Behavioral Web Data - Tales From The InsideCollecting & Temporal Analysis of Behavioral Web Data - Tales From The Inside
Collecting & Temporal Analysis of Behavioral Web Data - Tales From The Inside
 
Easier, Faster, and More Powerful – Alles Neu macht der Mai -Wir durchleuchte...
Easier, Faster, and More Powerful – Alles Neu macht der Mai -Wir durchleuchte...Easier, Faster, and More Powerful – Alles Neu macht der Mai -Wir durchleuchte...
Easier, Faster, and More Powerful – Alles Neu macht der Mai -Wir durchleuchte...
 
Hyatt driving innovation and exceptional customer experiences with FIDO passw...
Hyatt driving innovation and exceptional customer experiences with FIDO passw...Hyatt driving innovation and exceptional customer experiences with FIDO passw...
Hyatt driving innovation and exceptional customer experiences with FIDO passw...
 

Mopping Up With Groovy

  • 1. Mopping Up With Groovy* Brent Snook brent@fuglylogic.com *obligatory lame pun
  • 2. What is a MOP?
  • 3. Duck Typing • POJ (Plain Old Java) – you can interact with an object based on what it is (Class) • Groovy – you can interact based on which properties and methods are available on the object (MetaClass)
  • 4. Modifying Class Behaviour at Runtime • AKA Monkey Patching • AKA Duck Punching • see whytheluckystiff's My Complete List Of Substitute Names For The Maneuver We Now Know To Be Monkeypatching
  • 5. Why Do It? • it has an amusing name • it makes you more attractive to the opposite (or same) sex • it allows you to write more succinct, expressive code
  • 6. But I Feel Dirty … • you may need to change how you think about things • the laws are different in the duck typing universe • question old attitudes and conventions
  • 8. The MetaClass • every Groovy object has one • richer introspection and invocation interface than Java Class • you can set the MetaClass for an object to change the rules for how it can behave
  • 9. The Final Shape of an Object Added Methods Class ++ Properties = MetaClass
  • 10. The ExpandoMetaClass • core of Groovy metaprogramming • allows you to customise behaviour on the fly • when you ask a newly created GroovyObject for its metaClass, this is what you get back <<interface>> ExpandoMetaClass GroovyObject MetaClass
  • 11. What Can You Do With It? • borrow methods from other classes • add or override constructors • dynamically create method names • add methods to interfaces • add or override instance methods • add or override properties • add or override static methods http://groovy.codehaus.org/ExpandoMetaClass
  • 12. Adding a Class Method
  • 15. The Same In Ruby
  • 16. Invocation/Access Hooks • invokeMethod • getProperty • setProperty • propertyMissing • methodMissing
  • 18. Flexible Methods with methodMissing
  • 19. MOP!!...huh! What is it Good For?
  • 22. DSL - The Original Class
  • 23. Adding Some MOP Trickery
  • 25. Replacing/Simplifying Frameworks and Patterns • Spring, EasyMock/JMock • circumvent the rigidity of the Java language • patterns like Builder • Dependency Injection • with Groovy these can be simplified or disappear altogether
  • 27. The Pros and Cons Pros Cons • flexibility • sometimes clunky • extensibility syntax • power • slower performance
  • 28. With Great Power… • don’t get wowed by the coolness factor • remember to KISS • the end result should be more understandable, not less • keep pushing the boundaries but exercise judgment
  • 29. Further Reading and References • http://www.gnu.org/software/guile/docs/goops/Metaobjects- and-the-Metaobject-Protocol.html#Metaobjects%20and%20the %20Metaobject%20Protocol • http://en.wikipedia.org/wiki/Meta-object_protocol • http://www.grails-exchange.com/files/GraemeRocher%20- %20Keynote-DynamicGroovyMetaMagic.pdf • http://groovy.codehaus.org/ExpandoMetaClass • http://whytheluckystiff.net/articles/ seeingMetaclassesClearly.html • http://groovy.codehaus.org/ExpandoMetaClass • http://hackety.org/2007/08/10/ myCompleteListOfSubstitutePhrasesForTheActWeNowKnowTo BeMonkeypatching.html • Groovy In Action