dimanche 28 juillet 2019

C++ design pattern for sockets

The goal is to be able to support both IPv4 and IPv6 for a rather complex application. At present, only IPv4 is handled. There is a class called Socket and a class called TlsSocket which extends from it. Let's say that Socket has a set of methods M1,M2,....,M9. TlsSocket overrides M7, M8 and M9.

Given the current design I was thinking of making Socket an abstract class and extending it twice - SocketIPv4 and SocketIPv6 which would implement methods M5 and M6 differently. However, then I would have to extend them twice again to have a TLS version for both IPv4 socket and IPv6 socket leading to code duplication. I was looking at the best design pattern for the problem at hand and I was convinced that the decorator design pattern would work best.

However, then TlsSocket would inherit from the abstract Socket class and then be composed of a concrete implementation of Socket (either IPv4 or IPv6). Therefore, I would essentially be initializing two Socket instances (one for composition and the other is TlsSocket itself) pointing to the same file descriptor. Everything should work fine but I am slightly uncomfortable initializing two socket instances pointing to the same file descriptor. Is there an alternative design pattern that I have missed and should consider?

Aucun commentaire:

Enregistrer un commentaire