SlideShare une entreprise Scribd logo
1  sur  29
Télécharger pour lire hors ligne
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

Contenu connexe

En vedette

En vedette (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
 

Similaire à 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
 

Similaire à 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
 

Plus 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
 

Plus 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
 

Dernier

Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...DianaGray10
 
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot ModelNavi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot ModelDeepika Singh
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
Ransomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfRansomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfOverkill Security
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Jeffrey Haguewood
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MIND CTI
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024The Digital Insurer
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingEdi Saputra
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?Igalia
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FMESafe Software
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...apidays
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProduct Anonymous
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherRemote DBA Services
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyKhushali Kathiriya
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Zilliz
 

Dernier (20)

Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot ModelNavi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Ransomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfRansomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdf
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
 

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