Answer the question
In order to leave comments, you need to log in
How to climb a few levels of abstraction below without producing crooked code?
There is a proxy application that requests commands from the server and sends them to the desired local addresses.
The proxy does not know and does not look into the internal format of commands, they are all processed in the same way.
The task arose for two specific commands to implement a separate processing strategy with partial parsing of the internal format.
An ugly solution comes to mind - add directly to the general command processing code:
if(command_type == A)
{
Xml.Parse(command_payload);
if(Xml.ContainsKey("some_key")
{
DoSomething1();
}
else
{
DoSomething2();
}
}
else
{
ProcessGenericCommand();
}
Answer the question
In order to leave comments, you need to log in
Dictionary, where the key is the command type, the value is the handler.
If the command type is not found in the dictionary, we feed it to the default handler. If found, then we feed by value.
Options:
- an event that the parser calls after understanding the command. If the event handler is not processed, the default one is called.
- Strategy pattern
- Same thing, only DI container or abstract factory or whatever
Such tasks are gracefully handled using aspect-oriented programming methods, as an example, look at the Common Lisp Object System method combinators .
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question