Mopping Up With Groovy

Loading...

Flash Player 9 (or above) is needed to view presentations.
We have detected that you do not have it on your computer. To install it, go here.

0 comments

Post a comment

    Post a comment
    Embed Video
    Edit your comment Cancel

    Favorites, Groups & Events

    Mopping Up With Groovy - Presentation Transcript

    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
    7. How Does Groovy Implement a MOP?
    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
    13. Adding a Property
    14. Overriding an Instance Method
    15. The Same In Ruby
    16. Invocation/Access Hooks • invokeMethod • getProperty • setProperty • propertyMissing • methodMissing
    17. AOP with invokeMethod
    18. Flexible Methods with methodMissing
    19. MOP!!...huh! What is it Good For?
    20. Syntactic Sugar
    21. Mocking/Stubbing Behaviour
    22. DSL - The Original Class
    23. Adding Some MOP Trickery
    24. Icecream Time
    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
    26. Finally
    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

    + fuglylogicfuglylogic, 2 years ago

    custom

    946 views, 0 favs, 1 embeds more stats

    An introduction to using the Meta Object Protocol w more

    More info about this document

    © All Rights Reserved

    Go to text version

    • Total Views 946
      • 942 on SlideShare
      • 4 from embeds
    • Comments 0
    • Favorites 0
    • Downloads 11
    Most viewed embeds
    • 4 views on http://melbournepatterns.org

    more

    All embeds
    • 4 views on http://melbournepatterns.org

    less

    Flagged as inappropriate Flag as inappropriate
    Flag as inappropriate

    Select your reason for flagging this presentation as inappropriate. If needed, use the feedback form to let us know more details.

    Cancel
    File a copyright complaint
    Having problems? Go to our helpdesk?

    Categories