mardi 3 octobre 2023

design pattern to write a software development layer in go

I am attempting to create an API layer or software development kit (SDK) on top of an existing library in Golang. My goal is to make it highly flexible and extensible in Go so that we can add or extend new functionality without altering the existing codebase. For example, if I'm implementing a send_msg() function, it should be capable of sending different types of messages based on the message type specified as a parameter.

eg if I am implementing a generic api to send msg based on specific msg as below

  send_msg(msg_type M1, param1....paramN);
  send_msg(msg_type M2, param1....paramN);
  send_msg(msg_type M3, param1....paramN);`

to implement that i can simply write a function like below (pseudu code) in go

func send_msg(mst_type int, param1 type1.....paramN typeN) 
{
    case: msg_type M1
          perform msg processing and sending based on M1;
    case: msg_type M2
          perform msg processing and sending based on M2;
    case: msg_type M3
          perform msg processing and sending based on M3;

    default:
          other type processing
}

But the above code may not be highly extendable. For instance, if we wish to introduce new msg_type and its corresponding processing, we would need to modify the existing send_msg() function. Are there any better design patterns that support extensible plugins or do you have any other design pattern or design suggestions to address this?

Aucun commentaire:

Enregistrer un commentaire