A typical Scenario of GUI and application logic separation

From: Myster Ious (you_never_know_whos_at_yahoo.com)
Date: 02/12/04


Date: 11 Feb 2004 23:46:08 -0800

Say I want to implement MVC in my media player application. The GUI of
the player has a toggle button "Start Playback / Stop Playback". On
first press, it starts playing a media file, and on next press, it
stops it, and so on. On each press, a handler in the application code
is called:

void CController::OnPlaybackButton( )
{
 ...
}

How should I separate the 'application logic' from GUI in this case

Solution 1 - If this handler simply notifies the model that playback
button has been pressed, this is redundant. Model will have to define
a similar handler though with different implementation. Furthermore,
Model should not need to know about playback button. Button info
should remain in the GUI part:

void CController::OnPlaybackButton( )
{
  itsModel->OnPlaybackButton( );
}

void CModel::OnPlaybackButton( )
{
  if (itsState == PLAYING)
    itsPlayer->StopPlayback( );
  else if (itsState == IDLE)
    itsPlayer->StartPlayback( );
}

Solution 2 - If handler checks the state of the button or the model
itself and issue specific commands to the Model, it seems as if
application logic has been placed in the Controller:

void CController::OnPlaybackButton( )
{
  if (itsModel->GetState( ) == PLAYING)
    itsModel->StartPlayback( );
  else if (itsModel->GetState( ) == IDLE)
    itsModel->StopPlayback( );
}

void CModel::StartPlayback( )
{
  itsPlayer->StartPlayback( );
}

void CModel::StopPlayback( )
{
  itsPlayer->StopPlayback( );
}

What is the best way to implement this scenario?
Any articles or resources useful in this context?

Myster.


Quantcast