Wednesday, 14 August 2013

Implementing observer design pattern and handling the messages

Implementing observer design pattern and handling the messages

I am using observer design pattern and updating the GUI from model which
is working great. For each operation or function call from GUI, the model
sends a series of updates at various stages to notify the GUI accordingly
to do updates. This resulted in rather big if-else block in UpdateView()
method. Is this how it should be or can this improved by applying another
design pattern? I have split message from each function to a different
handler which helps quite a bit. Note: the code is not compiled below but
i took the essence of what I am doing. I have studied design patterns for
a while but I am implementing it for the first time.
class Msg
{
int operation;
int opCode;
}
// in the model class
Device::Calibrate()
{
Msg msg;
msg.operation = CALIBRATE;
// ..... do some work -----
msg.opCode = DEPENDENT_MODULE_NOT_LOADED;
Notify( msg )
// .... do some work ------
msg.opCode = HARDWARE_MODULE_1_FAILED
Notify( msg )
// .... do some work ------
msg.opCode = CALIBRATION_IN_PROGRESS
Notify( msg )
// --- so on -----
}
void CMainDlg::UpdateView(const Msg * msg )
{
if( msg->operation == VERIFY ) // all message from Verify() button
pressed
OnVerify( msg );
if( msg->operation == CALIBRATE ) // all messages from Calibrate
button pressed
OnCalibrate();
}
void CMainDlg::OnCalibrate(const Msg * msg)
{
if (msg->opCode == DEPENDENT_MODULE_NOT_LOADED)
{
}
else if msg->opCode == HARDWARE_MODULE_1_FAILED)
{
}
else if msg->opCode == HARDWARE_MODULE_2_FAILED)
{
}
else if msg->opCode == CALIBRATION_IN_PROGRESS)
{
}
else if msg->opCode == OUTPUT_FILE_COULD_NOT_BE_CREATED)
{
}
else if msg->opCode == CALIBRATION_FAILED)
{
}
else if msg->opCode == CALIBRATION_PASSED)
{
}
}

No comments:

Post a Comment