问题
So I'm building a fairly simple file transfer server/client but having a hard time to figure out a good design for processing different commands and states within the protocol..
Say that you have 3 different commands upload
, remove
, download
, you could use 3 different if-statements and keep track of states with additional if-statements, but that wont scale and will be impossible to maintain..
The Chain Of Responsibility design pattern could be used for sequential stuff such as encryption and compression, but it wont take care of the logic for commands.
I know that this is a quite broad question, but does anyone have any suggestions?
回答1:
The following may be helpful:
To provide different implementations for the same operation based on some conditions: http://en.wikipedia.org/wiki/Strategy_pattern
To maintain state of the communication (e.g. the current protocol command): http://en.wikipedia.org/wiki/State_pattern
For security/compression: http://en.wikipedia.org/wiki/Decorator_pattern or Chain-of-responsibility
来源:https://stackoverflow.com/questions/21801110/design-patterns-for-network-protocols