Books : reviews

Ira R. Forman, Scott H. Danforth.
Putting Metaclasses to Work.
Addison-Wesley. 1999

rating : 3.5 : worth reading
review : 19 May 1999

Imagine you have almost finished developing an application in your favourite object oriented language. There is a requirements change, and all accesses to objects of certain classes have to be via proxy objects. Maybe you write a separate proxy class for each ordinary class. Then there is another new requirement: you have to add an audit trail on every method call. Maybe you modify each method of each class to call the auditing function. Then there is yet another new requirement... Your beautiful application is getting messy and unmaintainable. You need this book, which shows you how to use metaclasses to implement inheritable properties, such as being a proxy, or being auditable.

In OO, knowledge is localised: objects know how to execute their methods and look after their state, and classes know how to create objects that are instances of the class. Given the pure OO view, everything is an object (and so classes are objects) and every objects has a class (and the class of a class object is a metaclass). Metaclasses know how to create classes. So you can write a metaclass that knows how to create proxy classes for any class, or how to create auditable classes where every method call is automatically audited. Metaclasses are also a good way to implement frameworks.

There is lots of good stuff here. It is a difficult book to read, however, because it lays out all the background about objects, and the implementation of the metaobject protocol with all its intricacies, before going through examples demonstrating the power and advantages of the metaclasses. So all the motivation comes after all the hard work. But it's worth the effort of persevering.