for example , consider a system , which has a global main controller that has a sub controller , and the sub controller may call main controller to update:
MainController.h
class SubController;
class MainController{
public:
static void setGlobalMainController(MainController* mainController);
static MainController* getGlobalMainController();
void init();
void updateSystem();
protected:
SubController* subController;
};
MainController.cpp
#include "MainController.h"
#include "SubController.h"
MainController* globalMainController;
void MainController::setGlobalMainController(MainController* mainController){
globalMainController=mainController;
}
MainController* MainController::getGlobalMainController(){
return globalMainController;
}
void MainController::init(){
this->subController=new SubController();
//wait sub controller to press button...
}
void MainController::updateSystem(){
//do something
}
SubController.h
class SubController{
protected:
void onButtonPressed();
}
SubController.cpp
#include "SubController.h"
#include "MainController"
void SubController::onButtonPressed(){
//do something...
MainController::getGlobalMainController()->updateSystem();
}
the SubController may accept button event and then need to update the main controller.
It has circular dependency,then I tried to break circular dependency using inheritance: create a new class MainControllerImp and move all concrete method in MainController to MainControllerImp:
MainController.h
class SubController;
class MainController{
public:
static void setGlobalMainController(MainController* mainController);
static MainController* getGlobalMainController();
virtual void init()=0;
virtual void updateSystem()=0;
protected:
SubController* subController;
};
MainController.cpp
#include "MainController.h"
#include "SubController.h"
MainController* globalMainController;
void MainController::setGlobalMainController(MainController* mainController){
globalMainController=mainController;
}
MainController* MainController::getGlobalMainController(){
return globalMainController;
}
MainControllerImp.h
#include "MainController.h"
class MainControllerImp : public MainController{
virtual void init();
virtual void updateSystem();
}
MainControllerImp.cpp
#include "MainControllerImp.h"
void MainControllerImp::init(){
this->subController=new SubController();
//wait sub controller to press button...
}
void MainControllerImp::updateSystem(){
//do something
}
SubController.h
class SubController{
protected:
void onButtonPressed();
}
SubController.cpp
#include "SubController.h"
#include "MainController"
void SubController::onButtonPressed(){
//do something...
MainController::getGlobalMainController()->updateSystem();
}
the circular dependency seems disappeared. But after some time, I start thinking if I need breaking dependency at this case: it only moves all methods from parent class to child class, besides that it seems no any benefits. Also, it has one more class, which is less readable and the code is less straight forward.
Should I leave circular dependency at this case? or there are other better design pattern to break circular dependency?
Aucun commentaire:
Enregistrer un commentaire