Another two examples of using changing classes

Itamar Shtull-Trauring itamarst at yahoo.com
Tue Aug 14 00:26:10 CEST 2001


Changing classes at runtime can be useful at times, and I've done it
myself at times. And since Twisted uses it and I intend to use Twisted
for my server architecture, that's a 3rd use as well.


== 1 ==
I have a proxy class that forwards network requests to a remote
server. When it gets disconnected, it still needs to respond to
commands, albeit with failure responses.

However, if a certains commands are given to the instance it can
reconnect and start working normally again.

The other way to do this is to have two classes - Connected and
Disconnected, and the instance switches between the classes to change
the way it works. Doing this without changing classes would require
having a frontend instance and a changing backend instance, and double
the number of function calls done with every operation.


== 2 ==
Consider a (SocketServer) network server that supports multiple
protocols on the same port. When a connection is opened the first
message tells the handler what protocol to use, and the handler then
changes its class to the appropriate handler class.



More information about the Python-list mailing list