Aman
Aman

Reputation: 783

How to wrap two different protocols into one api?

I am working with two different protocols that are wrapped into one class which the system calls. The wrapper calls the appropriate protocol based on the provided protocol type. Protocol A and B have about 90% common functions but there are several important functions that are exclusive to each protocol. Currently I am handling the protocol-specific functions by them public and using friend classes.

This seems to do the trick but I feel like there must be a way to handle these types of issues in a better way. I am self-taught so I lack knowledge in these areas. What are design patterns to handle these types of issues?

Upvotes: 0

Views: 131

Answers (1)

Mr.WorshipMe
Mr.WorshipMe

Reputation: 763

  1. Create an abstract class (i.e. at least one virtual function = 0), implement the common functions in this class.
  2. Inherit this class for protocol A, and B and implement the differing functions.
  3. (optional) make a factory class, which returns a unique_ptr for protocol A or protocol B, depending on the enum you give it.

Upvotes: 2

Related Questions