[Twisted-Python] composition VS. inheritance

Ian Haywood ian at haywood.id.au
Sat Jun 27 00:47:14 MDT 2020


In smb I have a SMBPacketReceiver that inherits from t.i.p.Protocol, it 
breaks the incoming TCP stream into logical packets (the analogue of 
LineReceiver in line-based protocols).

I then subclass SMBPacketReceiver to SMBProtocol which does a lot of the 
"heavy lifting" analyzing incoming packets.

I've been told in code review to use composition instead of inheritance, 
which is fine in a general sense but I have difficulty applying to 
twisted-specific tasks.

1. how to do Factory.buildProtocol? It has to return a t.i.p.Protocol, 
but with composition the Protocol object is a private variable of 
SMBPacketReceiver, in turn a private variable of SMBProtocol.

2. what to do instead of overriding Protocol.dataReceived and access 
incoming data if not allowed to subclass it?

Now  its not that I cant think of workarounds to these two problems, but 
they're ugly

Is there some recent twisted code using composition that I can look at?


Ian



More information about the Twisted-Python mailing list