I was just reminded of one of the horrible anti-patterns I’ve seen in working with a template library that will remain nameless.
At a previous company we called it “text subclassing.” It’s what we called it when you had to copy huge pieces of text out of a superclass and paste it into your subclass.
Admittedly, sometimes this is just the programmer being lazy and not wanting to think at all, but more frequently it shows a problem in the underlying architecture that needs to be fixed. For example, in the template library I was working with, there was this class:
class EventHandler: # define all events that need to be overridden here def createHeader(self): ... def createFooter(self): ...
You’d think that it would be really straightforward to override certain pieces of the template generation, would you not? Unfortunately, the EventHandler was used in this context:
class Compiler: def Compile(self, filename = sourceFile): # elide 100s of lines here self.setHandler(EventHandler()) # elide 100s of lines here
Yup, the EventHandler was nicely designed, but it was in the middle of 100s of lines of code. Can’t change the base class — it wasn’t mine. So the only recourse is to do a “text subclass,” and in the process I added the factory pattern that should have been there in the first place:
class MyCompiler: def createEventHandler(self): "Here's the factory method." return MyEventHandler() def doCompile(self, filename = sourceFile): # same 100s of lines that I don't understand here self.setHandler(self.createEventHandler()) # more 100s of lines that I don't understand here
Text subclassing: All of the speed of subclassing without that extra pesky method call!
Comments are moderated whenever I remember that I have a blog.
There are no comments on this article.