Policy Driven Development

I was reading some stuff on aspect oriented programming. I admit, I don’t know what that is. In my pursuit of trying to find out, I stumbled upon an idea.

First a bit of backstory. I use c++ for most (ok, almost all) of my work. It’s a language I’m familiar with. It’s the language that my toolkits are available in. It’s pretty much what my fingers write by default. As a user of c++ I’ve read many book on the language. One of my favorite books is Modern C++ Design by Andrei Alexandrescu (amazon). In this book he offers a generic way to drive certain behaviors of your design implementations. He uses policies, which are small classes that drive a template class. Example: If you write a generic singleton class, you may require thread safety at times, or not. Given a policy, you can control whether thread safety code is present or not. Since you use a template, there is no cost for inheritance, virtual tables, etc. The cost is born by the compiler when it resolves the template. Go read the book for a more in depth description of the why and how behind this.

So we have policies driving fundamental behavior of our classes. In Andrei’s book, he outlines many different uses of policies, however he doesn’t seem to reuse policies that often. In his classes, there’s not really that much overlap, so this lack of reuse is understandable. In a real environment though, I believe that there is quite a bit of overlap. With a bit of forethought, I believe that policy reuse could give us an entirely new level of code reuse. Entire threading models could be available just by reimplementing policies. Memory managers could also be swapped in and out. All that would be required is a new policy, and a recompile. There would be no runtime cost, yet you would get similar benefits as inheritance. Pretty utopian, and probably not realistic, but as I’ve not tried it, perhaps it is fun to think about.

Now Andrei does have a library that he created that provides the classes he describes in Modern C++ Design. It’s called the Boost library. He very well may do what I’m describing here. I honestly haven’t looked at it much. I do intend to now though.

Anyway, that’s my thought for the day. Perhaps it’s already implemented. Perhaps it’s impractical. Perhaps it’s worth trying out. We’ll see I guess.

Oh yeah, as for aspect oriented programming, you’ll have to go google that one for yourself.

-Synwan

Comments are closed.