Saturday, July 20, 2013

Getting shaved by visitors

Yesterday's cold kept me delightfully bed-ridden all day. I lay there most of the day, sometimes draped over a chair like a wet towel, sometimes prostrate on the bed like a dead hooker, but always impossibly listless. I ended up reading all day, and I feel a little bit sorry for the book I demolished - Prodigy by Marie Lu - I would have probably enjoyed it more if I'd read it in a non-zombified state. Still, it was enjoyable if not quite enjoyed. It was as expected, not quite as interesting as Legend (the first book in the series). This is pretty typical of dystopian trilogies; the first book is about starting a revolution, earth-shattering realizations, the death of ignorance, and the next N sequels are about war and hypocrisy and all kinds of nonsense dealt with better in books that deal solely with war and hypocrisy. Prodigy did better in that respect than Hunger Games, Divergent, and the Matrix sequels, but it still suffered from the same side-effects.

I learned a new object oriented programming pattern today that provides a cool way to follow the open / closed principle.

In case you don't know, the open / closed principle says that you should design things to be extensible but not modifiable. Meaning you don't want people modifying your diaper design, but you do want people building plugins for it. The original design, once complete, should only modified if something is broken. And this seems to apply on many levels, from objects to systems. Now onto the pattern, called The Visitor.

The Visitor pattern basically has you build in an uber-simple but multi-purpose socket, that you can later build things to plug into. The socket doesn't need to know beforehand exactly what will be plugging into it, a vibrator or the Omega 13 device, it's the job of the Visitor implementor to build plugs for those devices. The result is that once you have the socket set up, you're virtually unlimited in what you can plug into it, without ever modifying the diaper.

Neat, though it seems a bit hacky to me. It appears mostly to be used when you want to take a function that might apply to many different objects, and encapsulate all the variants into an object. This object, which is called a Visitor but should really be called a DirtyDirtySlut, then plugs into those objects and provides a single service, albeit adapted per their needs. So you might have a ShaveVisitor object, that defines 10 different variants of the function visit(): bear.visit(), chicken.visit(), patrickRothfuss.visit(), etc, and when the bear or the chicken decide they need a shave, they call in the ShaveVisitor to visit (shave) them. And of course everyone who shaves, lives in perpetual fear because they have no way of knowing who the ShaveVisitor shaved before them, and because there are rumors going around that there's a new Butthole object which likes to get shaved all the freakin' time. And who wants to be the next one shaved after that?

No comments: