I'm working on a project that uses an in-house library, which is also used by other projects.
There are some classes and methods in other classes that are not used outside the library itself, is there a design pattern that I could use so those classes and methods are not exposed outside the library?
The overall architecture looks something like this (please excuse me for the rough design and lack of detail). NOTES: LibClassB is used in a lot of threads only inside the lib.
Library code:
LibBaseClassA{
public:
static LibBaseClassA* factory(int arg){
if( arg==CONST_VAL1 ){
return new LibSubClassA2();
}
return new LibSubClassA1 ();
}
~LibBaseClassA(){
LibClassB::getInstance()-remove_observed(this);
}
virtual int function_a(int direction)=0;
int internal_function2(int arg){ do_something(); do_stuff(); }
void init_all(int arg1, int arg2);
protected:
LibBaseClassA(){
LibClassB::getInstance()->add_observed(this);
}
}
LibSubClassA1 : private LibBaseClassA{
public:
virtual int function_a(int direction){ init_task_a1(direction); do_task_sca1(); }
private:
init_task_a1();
do_task_sca1();
}
LibSubClassA2 : private LibBaseClassA{
public:
virtual int function_a(int direction){ init_task_a2(); do_task_sca2(direction); }
private:
init_task_a2();
do_task_sca2();
}
LibClassB{
public:
void transform(LibBaseClassA* pObj){
pObj->internal_function2( this->calculate_somthing() )
}
void add_observed(LibBaseClassA* pObj){
m_classA_instances.append(pObj);
}
void remove_observed(LibBaseClassA* pObj){
m_classA_instances.remove(pObj);
}
static LibClassB* getInstance(){
static LibClassB instance;
return &instance;
}
private:
LibClassB(){
read_hardware_data();
start_thread(&(this->function_thread());
}
void function_thread(){
for(obs:m_classA_instances)
{
transform(obs);
}
}
list m_classA_instances;
}
Individual projects code:
#include <LibBaseClassA>
int main(int argc, argv**){
list actuators_list;
while(!finish){
int option = get_external_input();
if(option>0 && option<2 ){
LibBaseClassA* obj = LibBaseClassA::factory(option);
actuators_list.append(obj);
}
else
{
for(obj:actuators_list){
obj->function_a(option);
}
}
wait(SLEEP_TIME);
}
}
-
4What's wrong with PIMPL?Basilevs– Basilevs02/12/2025 20:16:19Commented Feb 12 at 20:16
-
4More info about what Basilevs is suggesting (if you need it): en.cppreference.com/w/cpp/language/pimplGreg Burghardt– Greg Burghardt02/12/2025 21:15:53Commented Feb 12 at 21:15
-
4This doesn't even look like it needs PIMPL. Just put the factory method implementation into a source file like the most basic separate compilation in C++, and then you can put the subclasses into the source file as well.Sebastian Redl– Sebastian Redl02/13/2025 07:47:03Commented Feb 13 at 7:47