Chapter 13MOP Method Injection

We’ve heard groans like, “It would be so convenient if the String class supported an encrypt method.” Object-oriented programming is about extensibility, but languages often limit how far we can extend. What if we could open any class and add methods we desire, based on the needs of the application on hand? This would give us unbounded extensibility—make it easy to write expressive code. In Groovy we can do just that, with little effort.

In Groovy we can open a class at any time. That is, we can add methods to classes dynamically, allowing them to change behavior at runtime. Rather than working with a static structure and a predefined set of methods, objects can be agile and flexible, and assimilate behavior ...

Get Programming Groovy 2 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.