Friday, April 18, 2008

Mixins - An OO Idea Worth Getting To Know

A while back I commented about how I was impressed the PLT Scheme OO library included a keyword/concept I hadn't heard of before: pubment.

Today, for the first time, I used another concept which I've only seen implemented in PLT Scheme: mixins.

The idea behind a mixin is simple, but unexpected: you write code that has a parameterized superclass. And then, at run time, pass in the super class you'd like your code to use. Or as stated here:

We're taking in a superclass, and "mixing in" a few more methods to create a new class.

While I had seen this feature documented in the manual, it wasn't until I read this recipe that I both Got It and appreciated how I could use this capability to solve real problems.

Even if you don't program in Scheme, it's worth taking a few minutes out and reviewing that recipe. Just like pubment, it's good to remind yourself that there's more to OO than Classes, Interfaces, Methods and Fields.

Perhaps what I like most about mixins is that they have the same play-doh feel that most of Scheme does. Want to, at run time, generate a class dynamically? No problem, easy to do. It's just a whole lot more flexible than the the Java OO world I've spent quite a bit of time in.

4 comments:

  1. Yet another contribution to the programming world from Smalltalk :)

    ReplyDelete
  2. Mixins (and much, much more) can be accomplished in Java using AOP. Spring AOP, AspectJ, JBoss AOP, and probably others all have support for mixins.

    ReplyDelete
  3. Ahhh, Smalltalk.

    Simon, I can't say I'm surprised at all.

    One day I'm so going to have to learn it...

    -Ben

    ReplyDelete
  4. Helmy -

    That's a good point. I'm so thinking core Java of a few years ago.

    Do you have any experience with using Java AOP? Have you found it to make it cleaner to solve problems?

    ReplyDelete