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 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access