I have a C++ audio project. I created a class to handle the communication between two different classes using the Mediator pattern. However, the user is supposed to use them in a way like the following one in pseudo-code:
~
Class A;
Class mediator;
Class B(mediator);
~
I declared every methods public in the Mediator class, to allow the proper access from the two distinct classes, however now that I am distributing the code I need the public an API, but I feel there is no need to publicly expose this class . Please provide a feedback about the correct exposure of this class. Note: the class is a derived class from an interface, to use the Dependency inversion principle. Also note that the Mediator class is completely defined in just one header file (~ 50 lines of code).
Assistant
Using the Mediator pattern helps decouple classes and improve the modularity of your code. Since you're concerned about API exposure, it's essential to strike a balance between providing necessary functionality to users and encapsulating your internal implementation details.
Current Setup Review
Based on your description, you have three main components:
Class A
Class Mediator (derived from an interface to adhere to the Dependency Inversion Principle)
Class B (dependent on the Mediator)
Here's a more concrete illustration of your setup in C++:
cpp
// Interface for Mediator
class IMediator {
public:
virtual void notify(A* sender, std::string event) = 0;
};
// Class A
class A {
IMediator& mediator;
public:
A(IMediator& mediator) : mediator(mediator) {}
void doSomething() {
// Logic code
mediator.notify(this, "event_from_A");
}
};
// Class B
class B {
IMediator& mediator;
public:
B(IMediator& mediator) : mediator(mediator) {}
void doSomethingElse() {
// Logic code
mediator.notify(this, "event_from_B");
}
};
// Mediator class
class Mediator : public IMediator {
A* a;
B* b;
public:
void setA(A* pa) { a = pa; }
void setB(B* pb) { b = pb; }
void notify(A* sender, std::string event) override {
if (event == "event_from_A") {
// Handle event from A
}
// Add more events or notifications if necessary
}
void notify(B* sender, std::string event) override {
if (event == "event_from_B") {
// Handle event from B
}
// Add more events or notifications if necessary
}
};
Recommendations for API Exposure
Limit the Exposure of the Mediator Implementation:
Expose the interface IMediator and let the users interact with IMediator instead of the Mediator class.
Keep the Mediator class internal to your library if users do not need to instantiate or directly interact with it.
Factory Methods and Dependency Injection:
Use factory methods or functions to create instances of A, B, and Mediator. This prevents users from needing to directly instantiate or configure the mediator.
Consider providing a facade class to simplify the user API further. This facade can internally manage the creation and wiring of A, B, and the Mediator.
Provide Clear Documentation:
Ensure that the documentation clearly describes the use cases and usage patterns, highlighting that users should interact with IMediator.
Final Implementation Example:
Header file api.hpp:
cpp
#ifndef API_HPP
#define API_HPP
#include <memory>
#include <string>
// Interface for Mediator
class IMediator {
public:
virtual void notify(A* sender, std::string event) = 0;
};
// Class declarations for A and B
class A;
class B;
// Factory functions
std::unique_ptr<A> createA(std::unique_ptr<IMediator> mediator);
std::unique_ptr<B> createB(std::unique_ptr<IMediator> mediator);
std::unique_ptr<IMediator> createMediator();
#endif // API_HPP
Use interfaces to abstract the implementation and maintain loose coupling.
Keep specific implementations (Mediator) internal and provide factory functions for flexible and safe instantiation.
Document the intended usage patterns for clarity.
This approach ensures your users gain the benefits of the Mediator pattern without exposing unnecessary details, thus maintaining a clean and concise API.