[Divunal-author] Charge interfaces

Christopher Armstrong radix@twistedmatrix.com
Sun, 11 May 2003 22:55:50 -0400


So I'm getting through the implementation of charge and whatnot,
and have run into a problem I can't figure out how to solve.

So far I have a new Interface for every kind of socket and plug
(or anything that can be connected, really). The general pattern
is IBattery, IBatterySink, IBatteryCharger, IBatteryChargerSink,
etc (Battery implements IBattery and IBatteryChargerSink). The
problem I'm having is with implementing the `connect' command:

  connect battery to walkman

I've implemented a couple of utility Interfaces that aren't
directly used, IChargeSource and IChargeSink; the interfaces
listed above subclass them, but obviously that doesn't have
any meaning, it's just so I don't have to keep typing the same
method names. However, I really wish I could say something like

   def doAction(self):
       try:
           IChargeSource(self.tool).sinkTo(IChargeSink(self.target))
       except Incompatible:
           ....

This doesn't actually work, of course. And the idea completely breaks
when there are two kinds of IChargeSource on an object. :-(

The only alternative I have come up with is rather lame: There will
only be IChargeSource and IChargeSink interfaces, and they'll both
define a `getShape' method (or maybe getSinkShape and getSourceShape,
because some objects will probably want to implement both source and
sink in the same class). Each implementation of these interfaces will
check to make sure that the opposite is a compatible shape, which
could just be a string. I actually partially implemented this, but it
smelled bad so I threw it out. But now that I've run into the problem
with the `connect' action, it seems to be the only way to do it.

-- 
 Twisted | Christopher Armstrong: International Man of Twistery
  Radix  |          Release Manager,  Twisted Project
---------+     http://twistedmatrix.com/users/radix.twistd/