[ Content | Sidebar ]

layers and observers

September 19th, 2004

Here’s a question that’s come up at work over the last week or two. A couple of us are trying to rehabilitate some unmaintainable code that’s currently in our product: trying to bring it “From Mud to Structure”, as POSA1 puts it. The obvious pattern to start with is is Layers: we’d like to separate the pieces of software into a core that provides the core functionality that we need and that uses the same classes and patterns as the rest of our system, with an outer layer on top of it that satisfies customer-specific needs (e.g. using CORBA instead of our own RPC system and threading model) and that could be easily changed for different customers.

So: how do adjacent layers communicate? You could have adjacent layers invoke each other via function calls, but that would couple them too tightly. One standard trick to use here is to use the Observer pattern: in one direction, you’re allowed to do function calls, but in the other direction you’re only allowed to publish state change events. (And, of course, information can also be passed in the other direction via return values of function calls.)

But who gets to do function calls, and who is restricted to being observed? You could do it either way (replace a function call foo() by publishing the event that your desiredAction attribute has been set to foo), but presumably one way is more natural than the other. In the situation, it’s pretty obvious: the outer, customer-specific layer frequently wants to invoke customer-neutral functionality in the inner layer, get the results back in a customer-neutral format, and translate them to a customer-specific format. There are also some situations where the inner layer wants to trigger actions in the outer layer: for example, the outer layer may request that the inner layer start a long-running action, and want to know when the action is finished. Which is a classic situation where Observer works well. So, here, it looks like the best solution is for the outer layer to make function calls on the inner layer, and for it also to be an observer of the inner layer.

Is this a general rule, then? If you have a layered architecture, do you want your function calls to always go inward? At work, there’s somebody who sometimes gets annoyed if it looks like we’re doing things that he disagrees with, so I looked to see if he’d written anything on the subject. He gives an example at the other end of a layer architecture – a machine-independent part (of a kernel?) on the outside, and a hardware abstraction layer on the inside. And there, he wants the hal to call and be an observer of the mi layer.

So, some questions:

  • Does this make sense, or should it be reversed? Honestly, here I’m hampered by my lack of experience – it sounds fishy to me, but I’ve never written such software, and the devil is in the details. (It is the case that the person in question thinks about observers fairly differently from me.) One thought experiment is: what if we go down still further, so the outer layer is the hal and the inner layer is the hardware itself? If my understanding of hardware is correct (which it probably isn’t!), we have two communication mechanisms: the hal can read/write memory (and some memory locations may trigger special actions, e.g. disk writes), and the hardware can raise interrupts. If that’s the case, then I’d say that reading and writing memory is more like performing a function call (consider, e.g., replacing a public member variable by a pair of accessors), while an interrupt is more like publishing an event to on observer (“something happened!”).
  • If we do agree with his point of view, is it really inconsistent with the example I’ve given above? In my example, the customer-specific layer was a caller and observer of the customer-independent layer; in his example, the hardware-specific layer was a caller and observer of the hardware-independent layer. Maybe if I thought about it more, I’d decide that we were both right, and come up with a generalization along the lines of X-independent vs. X-specific.
  • Is there really only one correct answer here? POSA1 says no: it’s more common for outer layers to call and observe inner layers, but that the other direction is also possible. (As well as other solutions, e.g. observers everywhere to reduce coupling still further.)

dance class; hdtv

September 18th, 2004

Miranda’s taking dance lessons (tap and ballet for 5-6 year olds). The second one was today. At the first one, I got to see what a girl-dominated world might look like: all 10 kids were girls, 8 of the ten were wearing light pink, one was a mixture of pink and black, and one was all-black. One other parent was male, the rest were female. Much more balanced today, though – more black, a few kids were wearing lavender, and one was dressed in white. And I think there were four fathers there.

On the drive there, while passing some fire trucks: Miranda: “Fire trucks can go through red lights because they’re red!”. Me thinking: Hmm… Miranda: “And taxis can go through yellow lights because they’re yellow!”. Me thinking: That would explain some things, wouldn’t it?

We bought an hdtv last week; I mostly actually wanted to use it as a monitor for 480p inputs and sd cable, but this model did a nice job of upconverting 480p inputs to make them look like 720p, so spending the money for an hdtv monitor was worth it to me. (Not all hdtv monitors did as well, though, so be warned: go to a store and get the sales clerk to plug in a dvd player via the component inputs.) And it actually has a built-in tuner, so I went out and bought an antenna for it. Which I’ve had mixed experiences with so far: it’s amplified, but signals still go in and out sometimes. So, with less amplification, it would go between 0 and 2 bars, while with more amplification it jumps from 0 to 5 bars, but the frequency of 0 bars doesn’t get reduced as much as I’d like.

Right now, though, I’m watching baseball on Fox, and I’ve actually managed to fiddle with the antenna to get a consistent signal. It’s being transmitted as hd, according to the tv, but it sure looks to me like sd. So I’m pretty sure that it’s being recorded with sd cameras. And, ironically, the result is in some ways worse than what I get if I switch the antenna over to cable: the tv recognizes that the cable signal is sd, so it uses its clever machinery to improve the quality of the signal, while it leaves the hd signal as it is. In particular, on the hd signal, the interlacing is pretty obvious, while the tv has circuitry to convert the sd 480i signal to 480p. So there are tradeoffs. (Probably better hdtv receivers have less of an issue with this; for that matter, there’s no technical reason why a tv with a built-in hdtv receiver couldn’t display the signal as 1080p instead of 1080i, I think.) (Edit: actually, MPEG can encode interlacing, so there really is a difference between a 1080i signal and a 1080p signal.)

Still, I’m glad to see that I’m getting a consistent signal here, because presumably the playoffs and world series really will be broadcast in hd.

manny!

September 17th, 2004

Jordan’s comment reminded me that I forgot about his other website. Which, at least currently, links to a page advertising his book, The Grasshopper King. Which I like a lot, so go buy it! I remember enjoying reading the manuscript when we were in grad school together, but in the mists of time I couldn’t remember whether I liked it because he wrote it or because it’s actually good. Fortunately, the latter proved to be the case. My only quibble is that I always feel guilty about enjoying novels set in academia. But I suppose that you should write about what you know; anyways, now that I’ve been out in the real world for a whole year now, I guess I can start getting over that.

Side note to Jordan: at first I assumed the web page was the publisher’s page for the book, but a closer examination showed that not to be the case. What’s your connection with this Zeek magazine that’s hosting it? Who did the web site design?

Anyways, the real reason why I’m typing now instead of reading a book is my happiness at the Red Sox’s victory over the Yankees (at the expense of Mariano Rivera, no less), and at the awesome catch that Manny Ramirez made to take a home run out of the stands. Manny has a bad defensive rep, but he’s actually always been more or less decent defensively. Still, that was probably the best play I’ve seen him make. Good Red Sox crowd, too; it took me a little while to realize that, in fact, the game was in New York. We went to an A’s-Red Sox game a few weeks ago where the crowd as a whole was, I think, slightly tilted towards the Red Sox, and the fans around us were heavily leaning that way. Miranda was highly amused by the simultaneous “Let’s go Red Sox” and “Let’s go Oakland” chants, but her favorite chant of the night was one all the fans could agree on, “Yankees Suck”. She is a fabulous daughter!

art of the fugue

September 16th, 2004

I was listening to the Gustav Leonhardt recording of The Art of the Fugue in the car for the last few days, and I kept on waiting for the unfinished fugue to show up. But it never did! Reading through the liner notes (what is the accepted term for CD booklets?), Leonhardt is of the opinion that the unfinished fugue wasn’t intended to become as part of the work. I hadn’t realized that there was any serious debate on this issue (though it is, I suspose, a bit suspicious that the main theme of the work doesn’t appear in the portion of that fugue that we have); at any rate, I miss it.

Not my favorite recording of The Art of the Fugue; it seems a bit flat to me. (Flat in the sense of “insufficiently textured”, not in the musical sense, of course.) The other recordings on the CD are better, though (Clavierubung II and Prelude, Fugue, and Allegro BWV 998). I’m looking for more recordings of The Art of the Fugue; the other ones I have are the ones by Davitt Moroney (which I quite like) and the partial recording by Glenn Gould, largely on organ, which is dreadful. (At least the organ parts.) Anybody with any favorites?

up and working

September 16th, 2004

Welcome to my blog. It’s implemented using WordPress, which seems pleasantly nice to use. (And is GPL‘d.) Many thanks to the good folks at Red Bean for their assistance, especially Ayse Sercan and Karl Fogel. (While I’m mentioning people, I’ll say hi to Jordan Ellenberg, since he promised to read the thing.)

Great things await. Or not. We’ll see.