Define a Good Interface in C++
In C++, there is no keyword interface or protocol to define something what should conform to specific set of rules. But, it doesn't mean we can't express the concept with available tools. C++ has virtual member-function what enables polymorphic behavior and inheritance to share common code across related types.
Here's an example how to define a good interface:
struct LoginSession {
int last_time_active;
};
class Account {
public:
virtual ~Account() = default;
virtual std::vector<LoginSession> FetchLoginSessions(
int since) const = 0;
};
The interface has the following properties:
does NOT declare any fields
defines default virtual destructor
does NOT contain any user-defined implementations
does NOT use interface in its name
Let's figure out each point one-by-one (to be continued…)