mercredi 27 mai 2020

design pattern to avoid unnecessary addition of abstract functions to accommodate new functionality

In below code I have abstract class TestAlgModule which I will be exposing to library users and there are several functionalities they can use such as VOLUME, MIXER and so on. However, suppose users need a new function which is added only in MixerManager then I need to add that in TestAlgModule abstract class and now suddenly all the derived class needs to add that without any benefit.

How do I avoid this?

 #include <iostream>                                                                                                                                                                                        
 using namespace std;                                                                                                                                                                                       

 enum {VOLUME, MIXER, UNKNONWN};                                                                                                                                                                            

 class TestAlgModule {                                                                                                                                                                                      
 public:                                                                                                                                                                                                    
     virtual void open(int type) = 0;                                                                                                                                                                       
     virtual void close(int type) = 0;                                                                                                                                                                      
 };                                                                                                                                                                                                         

 class volumeManager : public TestAlgModule                                                                                                                                                                 
 {                                                                                                                                                                                                          
 public:                                                                                                                                                                                                    
     void open(int type) {}                                                                                                                                                                                 
     void close(int type) {}                                                                                                                                                                                
 };                                                                                                                                                                                                         

 class mixerManager : public TestAlgModule                                                                                                                                                                  
 {                                                                                                                                                                                                          
 public:                                                                                                                                                                                                    
     void open(int type) {}                                                                                                                                                                                 
     void close(int type) {}                                                                                                                                                                                
     void differentFunction() {};                                                                                                                                                                           
 };                                                                                                                                                                                                         

 /* users calls this to get algModule and then call functions to get the job done */                                                                                                                                                                                         
 TestAlgModule *getTestAlgModule(int type) {                                                                                                                                                                
     switch(type) {                                                                                                                                                                                         
         case VOLUME:                                                                                                                                                                                       
             return new volumeManager();                                                                                                                                                                    
         case MIXER:                                                                                                                                                                                        
             return new mixerManager();                                                                                                                                                                     
         default:                                                                                                                                                                                           
             break;                                                                                                                                                                                         
     }                                                                                                                                                                                                      
     return nullptr;                                                                                                                                                                                        
 }                                                                                                                                                                                                          

 int main() {                                                                                                                                                                                               
     TestAlgModule * test = getTestAlgModule(MIXER);                                                                                                                                         
     test->open();     
     //test->differentFunction();          this can't be called as it is not part of abstract class and users are exposed only abstract class                                                                                                                                                              
     return 0;                                                                                                                                                                                              
 }                     

If something is not clear please let me know and I will do my best to answer it. I am looking for a better way to do this i.e. change in VolumeManager should be independent of MixerManager.

Aucun commentaire:

Enregistrer un commentaire