SlideShare una empresa de Scribd logo
1 de 29
Descargar para leer sin conexión
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

Más contenido relacionado

Destacado

Destacado (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 a 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 a 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
 

Más de 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
 

Más de 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
 

Último

Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 3652toLead Limited
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningLars Bell
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxNavinnSomaal
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenHervé Boutemy
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionDilum Bandara
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .Alan Dix
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Commit University
 
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfHyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfPrecisely
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteDianaGray10
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek SchlawackFwdays
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxhariprasad279825
 
Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piececharlottematthew16
 
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostLeverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostZilliz
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Manik S Magar
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfAlex Barbosa Coqueiro
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsPixlogix Infotech
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsMiki Katsuragi
 

Último (20)

Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine Tuning
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptx
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache Maven
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An Introduction
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!
 
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfHyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test Suite
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptx
 
Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piece
 
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostLeverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdf
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and Cons
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering Tips
 

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