Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 设计模式,避免不必要地添加抽象函数以适应新功能_C++_Oop_Design Patterns - Fatal编程技术网

C++ 设计模式,避免不必要地添加抽象函数以适应新功能

C++ 设计模式,避免不必要地添加抽象函数以适应新功能,c++,oop,design-patterns,C++,Oop,Design Patterns,在下面的代码中,我有一个抽象类TestAlgModule,我将向库用户公开这个抽象类,他们可以使用一些功能,例如音量、混音器等等。但是,假设用户需要一个只在MixerManager中添加的新函数,那么我需要在TestAlgModule抽象类中添加该函数,现在突然所有派生类都需要添加该函数,而没有任何好处 我如何避免这种情况 #include <iostream>

在下面的代码中,我有一个抽象类
TestAlgModule
,我将向库用户公开这个抽象类,他们可以使用一些功能,例如
音量、混音器
等等。但是,假设用户需要一个只在
MixerManager
中添加的新函数,那么我需要在
TestAlgModule
抽象类中添加该函数,现在突然所有派生类都需要添加该函数,而没有任何好处

我如何避免这种情况

 #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;                                                                                                                                                                                              
 }                     
#包括
使用名称空间std;
枚举{VOLUME,MIXER,unknown};
类TestAlgModule{
公众:
虚空打开(整型)=0;
虚空关闭(int类型)=0;
};                                                                                                                                                                                                         
类卷管理器:公共测试模块
{                                                                                                                                                                                                          
公众:
void open(int类型){}
无效关闭(int类型){}
};                                                                                                                                                                                                         
类mixerManager:公共TestalModule
{                                                                                                                                                                                                          
公众:
void open(int类型){}
无效关闭(int类型){}
void differentFunction(){};
};                                                                                                                                                                                                         
/*用户调用此函数以获取algModule,然后调用函数以完成任务*/
TestAlgModule*getTestAlgModule(int类型){
开关(类型){
案件数量:
返回新的volumeManager();
混音器:
virtual void differentFunction() {}
if(mixerManager* mm = dynamic_cast<mixerManager*>(test)) {
    mm->differentFunction();
}