mardi 12 janvier 2021

What design pattern should I use to reduce the number of method arguments?

I'm developing a networking library in C++. I have a method that sends a network packet. Signature:

void Send(PacketId packet_id,
          const UserPacket& user_packet,
          const std::shared_ptr<RemoteSystem>& remote_system,
          bool broadcast,
          std::function<void(Packet* p)>&& response_callback,
          uint32_t response_timeout,
          std::function<void()>&& response_timeout_callback,
          uint32_t response_to_packet);

Arguments:

  • packet_id - Packet id
  • user_packet - User packet data
  • remote_system - remote system to send a packet
  • broadcast - broadcasting mode. if true - sends to all connected systems excluding remote_system
  • response_callback - waits for the response to the sent packet
  • response_timeout - waiting for the response timeout in milliseconds
  • response_timeout_callback - callback that will be called when response timed out
  • response_to_packet - ID of a received packet to respond to

Have bigger than 3 arguments it's a bad practice. So, how can I design my code better? What design pattern can you advise me? Thanks in advance!

Aucun commentaire:

Enregistrer un commentaire