Monday, November 15, 2004

Composition vs. "wiring"

At last week's seminar I created a new exercise during the threading portion. The goal of the exercise was to get people comfortable with the idea that threading can cause collisions over shared resources, and that the synchronized keyword prevents these collisions.

While people were solving this exercise I discovered that although the concept of composition was clear to everyone, the idea of setting up a number of object instantiations and wiring them together (by handing a reference of one object to the constructor for another object), was very unclear. As this is, to me anyway, one of the fundamental idioms for solving problems, I've found it interesting that either (1) I wasn't conveying the concept (probably thinking it to be obvious) or (2) somehow the way we approach objects distracts us from such a fundamental idea. It's almost as if an electrical engineer who spends too much time with equations and models doesn't know how to actually wire together a set of chips.

Another observation that comes from this is the overuse of Singleton. I have noticed that there seems to be a movement in parts of the design patterns community to deprecate or even ban Singleton. I wonder if this isn't because of all the emphasis on the design of classes and how you use inheritance and polymorphism in interesting ways, and the underlying assumption that people will naturally know how to design programs using these classes. Without more explanation about how you create objects and wire them together into a program, singleton may be what people tend to think about.