3

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);
 }
}
asked Feb 12 at 19:01
3
  • 4
    What's wrong with PIMPL? Commented Feb 12 at 20:16
  • 4
    More info about what Basilevs is suggesting (if you need it): en.cppreference.com/w/cpp/language/pimpl Commented Feb 12 at 21:15
  • 4
    This 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. Commented Feb 13 at 7:47

0

Know someone who can answer? Share a link to this question via email, Twitter, or Facebook.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.