次の方法で共有


On OOP & "implements"

As a part of last week's blog discussion on MS & OOP, my friend Bill Fisher sent me a pointer to this blog entry on the Delphi "implements" keyword. I think Bill sent this to me for a couple of reasons, firstly because the "interface implementation by delegation" design pattern it enables was germane to the discussion on OOP, and secondly because Bill knew that I was the guy responsible for "implements." "Implements" was probably my one major contribution to the Object Pascal language. It enables a class to implement a particular interface by surfacing a property of class or interface type that actually implements the interface methods. The idea was to be able to have pre-built, reusable interface implementations (maybe complex, maybe just boilerplate) that could be recycled and aggregated with outer classes of all colors and creeds.

Truth be told, the primary motivation for this feature was COM. COM notoriously required developers to implement lots of different interfaces on a class to make it useful (like, an automation object or an ActiveX control), and these interface implementations were often largely the same in every instance. In such a world, the ability to aggregate together a bunch of "mini-class" interface implementations into one superclass has many benefits over a traditional inheritance pattern. C++ developers in the crowd may be looking at this going, "well, duh! that's what multiple inheritance does!" And, in fact, I admit that one of the primary reasons I pushed for this language feature was my own ATL envy. The C++/ATL technique is a slightly different route to arrive at the same destination; ATL involves multiple inheritance of pre-build interface implementations, whereas the "implements" approach involves single inheritance with delegation of interface methods to objects that are properties of the superclass.

Bill's pointer got me doing a bit more research into folks using "implements," and I was interested to see that a number of folks were using this in non-COM situations as a general design pattern. I was also interested to see some interest for a feature like this in C# (which does not support multiple inheritance). Currently this would not be possible because the fundamentals do not exist in CLI to support implementation by delegation (Delphi also doesn't support "implements" when targeting CLR, only when compiling for native Win32). I'd certainly like to see something like this. This little episode reinforced for me of the notion that developers will use tools in interesting ways not conceived by the inventor. I wish I could say I had the foresight to see my feature borne of ATL envy used as a more general, non-COM pattern, but I didn't.

And so I finally get to my point: developers, particularly those that make things for other software developers, shouldn't arbitrarily limit the flexibility of their software. Chances are, your users will surprise you with their creativity. This basic notion is not new. In fact, maybe George S. Patton, Jr. said it best when he said, "Never tell people how to do things. Tell them what to do and they will surprise you with their ingenuity." This relates also to one of Guy Kawaski's Rules for Revolutionaries, which says, "Let a thousand flowers bloom." In other words, when people use your product in an entirely new way, embrace the change. You'll win fans, not just users.

Comments

  • Anonymous
    July 05, 2005
    Just to point it out -- it would be absolutely possible to add this to C# by just having it automatically generate the wrapper methods that forward to the existing object -- iow, pure syntactic sugar.
  • Anonymous
    July 06, 2005
    Yep, implements is /very/ cool. I really miss it in the .Net side of Delphi.
  • Anonymous
    July 06, 2005
    Indeed it would be possible to do add "implements" to C# using syntactic sugar, although it would argue that if work were done in this area it should be done right. The code would look good when you wrote it, but the MSIL could get messy. And I would hate to have to figure out "sugared" classes decompiled by something like Reflector. :)
  • Anonymous
    November 04, 2008
    PingBack from http://blog.mischel.com/2008/11/04/interface-annoyances/