[Twisted-Python] composition VS. inheritance

Barry Scott barry.scott at forcepoint.com
Tue Jun 30 09:41:24 MDT 2020


On Saturday, 27 June 2020 07:47:14 BST Ian Haywood wrote:
> 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?

Have a look at the twisted code that handles HTTP. It switches from line handling for
the headers to body handing in we either 1 block or chunked encoding.

I think that matches what you are up to.

Barry

> 
> 
> Ian
> 
> _______________________________________________
> Twisted-Python mailing list
> Twisted-Python at twistedmatrix.com
> https://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
> 






More information about the Twisted-Python mailing list