advertisement

Mopping Up With Groovy

67 %
33 %
advertisement
Information about Mopping Up With Groovy
Technology

Published on November 10, 2008

Author: fuglylogic

Source: slideshare.net

Description

An introduction to using the Meta Object Protocol within Groovy
advertisement

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

Add a comment

Related presentations

Presentación que realice en el Evento Nacional de Gobierno Abierto, realizado los ...

In this presentation we will describe our experience developing with a highly dyna...

Presentation to the LITA Forum 7th November 2014 Albuquerque, NM

Un recorrido por los cambios que nos generará el wearabletech en el futuro

Um paralelo entre as novidades & mercado em Wearable Computing e Tecnologias Assis...

Microsoft finally joins the smartwatch and fitness tracker game by introducing the...

Related pages

The Pragmatic Bookshelf | Programming Groovy 2

title: Programming Groovy 2: Dynamic Productivity for the Java Developer, by: Venkat Subramaniam, isbn: 9781937785307, date: 2013-07-15
Read more

The Pragmatic Bookshelf | Programming Groovy

title: Programming Groovy: Dynamic Productivity for the Java Developer, by: Venkat Subramaniam, isbn: 9781934356098, date: 2008-04-01, description: Be more ...
Read more

Java Day Kiev: "Groovy MOPping" Izzet Mustafaiev - YouTube

Slides are available at http://javaday.org.ua/en/program-en Конференция по технологиям для разработки ...
Read more

What readers are saying about - strangemic resource network

What readers are saying about Programming Groovy More than a tutorial on the Groovy language, Programming Groovy is an excellent resource for learning the ...
Read more

Programming Groovy

III MOPping Groovy 185 12 Exploring Meta-Object Protocol (MOP) 186 12.1 Groovy Object . . . . . . . . . . . . . . . . . . . . . . . . 187
Read more

Mopping | LinkedIn

View 28413 Mopping posts, presentations, experts, and more. Get the professional knowledge you need on LinkedIn.
Read more

Programming Groovy 2 : dynamic productivity for the Java ...

Get this from a library! Programming Groovy 2 : dynamic productivity for the Java developer. [Venkat Subramaniam]
Read more

Programming Groovy 2

Programming Groovy 2 Dynamic Productivity for the Java Developer Venkat Subramaniam The Pragmatic Bookshelf Dallas, Texas • Raleigh, North Carolina
Read more