August 2005 Archives

A Note on Delegation

| No Comments

I'm reading Joshua Bloch's Effective Java and getting quite a lot of it. The book is a catalogue of numbered items of advice, some standard, some controversial, with fairly detailed exposition on each.

Item 14: Favor composition over inheritance is uncontroversial, being a restatement of the second principle of object-oriented design in Design Patterns, the famous Gang of Four book by Gamma and company. However, in his discussion, Bloch writes that

Sometimes the combination of composition and forwarding is erroneously known as delegation. Technically, it's not delegation unless the wrapper object passes itself to the wrapped object [Gamma95, p. 20].

I think Bloch misread page 20 and, thanks to the reference, I was able look at page 20 over his shoulder:

Delegation is a way of making composition as powerful for reuse as inheritance [...] [W]ith inheritance, an inherited operation can always refer to the receiving object through the this member in C++ [...]. To achieve the same effect with delegation, the receiver passes itself to the delegate to let the delegated operation refer to the receiver.

This text is suggestive, however the example that follows makes no attempt to achieve the this effect. In the example, Window object delegates an Area() operation to a Rectangle object. The Window object does not pass itself to the Rectangle.

In the Glossary of the GoF book, on page 360, there is another definition of delegation: An implementation mechanism in which an object forwards or delegates a request to another object. The delegate carries out the request on behalf of the original object. I haven't edited anything out this time.

I think Bloch took something optional and made it an essential part of the definition of delegation. The paragraph I quoted from the GoF is a little ambiguous, but the rest of the book seems clear: it is still delegation even if the delegating object does not pass itself as part of the request to its delegate.

To be sure, this is a detail of no importance. The definition of delegation doesn't really depend on the GoF. My quibble hardly affects my opinion of Effective Java: it's still a book I'm glad I bought. It's just a case where the reference doesn't give support to the author the way it appears to.

A Man of Leisure

| No Comments

I've been unemployed for most of July and will be for most of August.

During the last few weeks, job hunting has taken priority over blogging. The first opportunity I found out about, on July 11th, was the one that turned into an offer, on August 5th. I start at the new job on the 22nd.

At my previous job, I was a lone developer. Therefore, I took an approach to professional development that involved a lot of reading. Becoming self employed without an income, I took up reading as more or less a full time day job. I did update my resume and hit the job boards, but not as my primary activity. I wanted to pace myself and not set up too many contacts with possible employers at once.

I was surprised to find that a lot of the tech bubble infrastructure is still in place. There are headhunters who search for people they can pass on to employers for a fee. Some headhunters are conscientious, others spam with abandon. In one case, I got an urgent e-mail and voice mail from someone looking for a fluent Japanese speaker. The word "Japanese" did occur in my resume, but I never pretended to speak the language.

I was lucky. I didn't find the employer; a headhunter found me and figured out I was suitable for the position. I didn't answer every question perfectly in the interviews, but I got better as I progressed. This employer was more technically focussed than most; it paid off to read up on the details of Java, threads and RMI (among other things).

I have nearly two more weeks of leisure. I'll keep up with the reading, but I'll also have time to take care of chores and errands. If the earning member of the household could get time off, we'd probably go somewhere. But there's no need to go anywhere for warm, sunny weather. I'll try to enjoy the relaxed pace of summer while I still can.