T
T
trauus2018-08-22 14:53:48
Programming
trauus, 2018-08-22 14:53:48

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();
}

I understand that this is a bad decision, but there is not enough experience to understand how to do it better.
How are such tasks solved?

Answer the question

In order to leave comments, you need to log in

5 answer(s)
G
GavriKos, 2018-08-22
@GavriKos

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.

A
ApeCoder, 2018-08-22
@ApeCoder

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

Z
zahardzhan, 2018-09-25
@zahardzhan

Such tasks are gracefully handled using aspect-oriented programming methods, as an example, look at the Common Lisp Object System method combinators .

D
ddd329, 2019-05-15
@ddd329

Here is the most suitable pattern Chain of Responsibility

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question