dimanche 23 avril 2017

how to design a programmatic interface for a known protocol

So I have been breaking my head over this.

I have a protocol spec that i have to implement. The protocol itself is relatively simple. Just a command with a few parameters. The command and parameters are just strings with constraints. For instance a command is 'STATECHANGE' with parameters 'ORIGIN' which can be 'MOTOR' or 'LIGHT' and another parameter 'STATE' which can be 'ON' or 'OFF'.

Originally I wanted to make a struct with the command and a vector of all params as states but I would lose a lot of type checking with that (parameter type checking basically). And I wonder is there a know best way to do something like this.

I have considered the following.

  • Make a command factory with overloaded function for each different command and return an abstract representation of command that can be serialized. Also make an overload that takes a string and parses the original command out of it.

  • Create a class that represents each different command as a seperate function and also send the command. The advantage if this approach is that the interface looks just like a normal API.

Aucun commentaire:

Enregistrer un commentaire