architecture OOP, Design Pattern

V

Vincent RICHOMME

Hi,

I would like your opinion about the following subject :

I have a class called CBackupSimContact that is used to copy contact
from a SIM card to the phone memory.

I have another class called CBackupDialog that is supposed to display
copy progress.

So very basically I have the following

class CBackupSimContact
{
public:

void CopyContact()
{
for (int i = 0; i < NumContacts; i++)
{
SendMessage(CONTACT_ADDED); // BAAADDD because using windows
specific mechanism
}
}
};


// MFC MACROS (BAD)
ON_MESSAGE( CONTACT_ADDED, &CBackupDialog::OnContactAdded)

class CBackupDialog
{
public:
CBackupDialog::CBackupDialog ()
{
pBackupMgr = new CBackupSimContact();
}
protected:

void OnContactAdded()
{
ProgressBar.StepIt(); // move progress bar
}



private:
ProgressBar m_ProgressBar;
CBackupSimContact* m_pBackupMgr;
};


So it's a very simple case a class doing some operations and another
that receives notifications.
For now notifications is done by a windows specific mechanism called
message and I am not satisfied with this.


What I would like to know is how could I do it in a more OO way.
I have thought of an observer pattern but don't you think it's a bit
heavy for this simple task?

I am all eyes.
 
V

Victor Bazarov

Vincent said:
I would like your opinion about the following subject :

I have a class called CBackupSimContact that is used to copy contact
from a SIM card to the phone memory.

I have another class called CBackupDialog that is supposed to display
copy progress.

So very basically I have the following

class CBackupSimContact
{
public:

void CopyContact()
{
for (int i = 0; i < NumContacts; i++)
{
SendMessage(CONTACT_ADDED); // BAAADDD because using windows
specific mechanism
}
}
};


// MFC MACROS (BAD)
ON_MESSAGE( CONTACT_ADDED, &CBackupDialog::OnContactAdded)

class CBackupDialog
{
public:
CBackupDialog::CBackupDialog ()
{
pBackupMgr = new CBackupSimContact();
}
protected:

void OnContactAdded()
{
ProgressBar.StepIt(); // move progress bar
}



private:
ProgressBar m_ProgressBar;
CBackupSimContact* m_pBackupMgr;
};


So it's a very simple case a class doing some operations and another
that receives notifications.
For now notifications is done by a windows specific mechanism called
message and I am not satisfied with this.


What I would like to know is how could I do it in a more OO way.
I have thought of an observer pattern but don't you think it's a bit
heavy for this simple task?

No, I don't think so.

You don't have to implement the full-blown event mechanism unless you
think you can use it for other stuff around your application. I would
simply do

class CBackupSimContact {
...
template<class ContactAddedRegister>
void CopyContact(ContactAddedRegister & register)
{
for (int i = 0; i < NumContacts; i++)
{
register.OnContactAdded();
}
}
};

And then

...
CBackupSimContact pMyDoer;
CBackupDialog pDialog;

pMyDoer.CopyContact(pDialog);

V
 
V

Vincent RICHOMME

Victor Bazarov a écrit :
No, I don't think so.

You don't have to implement the full-blown event mechanism unless you
think you can use it for other stuff around your application. I would
simply do

class CBackupSimContact {
...
template<class ContactAddedRegister>
void CopyContact(ContactAddedRegister & register)
{
for (int i = 0; i < NumContacts; i++)
{
register.OnContactAdded();
}
}
};

And then

...
CBackupSimContact pMyDoer;
CBackupDialog pDialog;

pMyDoer.CopyContact(pDialog);

V
Waou. Really interesting this solution with templates. I find it very
elegant.Now let's say the problem is more complex.
Actually my CBackupSimContact is using a thread to do its copy.


So I have :

class CBackupSimContact {
...
template<class ContactAddedRegister>
void CopyContact(ContactAddedRegister & register)
{
// Create the thread and pass this to the static method
::CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)
ThreadCopyContact,this,0,0);


}

static void ThreadCopyContact(DWORD pvParam)
{
CBackupSimContact *pThis=reinterpret_cast< CBackupSimContact
*>(pvParam);

for (int i = 0; i < NumContacts; i++)
{
?????
}
}
};


Now the solution doesn't work anymore ...
 
?

=?iso-8859-1?q?Kirit_S=E6lensminde?=

Hi,

I would like your opinion about the following subject :

I have a class called CBackupSimContact that is used to copy contact
from a SIM card to the phone memory.

I have another class called CBackupDialog that is supposed to display
copy progress.

So very basically I have the following
[snip]

So it's a very simple case a class doing some operations and another
that receives notifications.
For now notifications is done by a windows specific mechanism called
message and I am not satisfied with this.

What I would like to know is how could I do it in a more OO way.
I have thought of an observer pattern but don't you think it's a bit
heavy for this simple task?

OO is all about message passing, but that message often gets lost in
all the fuss about the objects and inheritance. It's generally a
question of which message passing system you wish to use, or more
properly, which is most appropriate.

You can use the ones the compiler understands, ones that the operating
system uses or ones that you write yourself.

Which is best in this situation depends to a certain extent (as ever)
on how they're going to be used. Are the two objects in the same or
separate threads? The answer depends not on the two classes that
you've shown, but on the classes around them and how they fit into a
wider pattern.

If you take a look at the Smalltalk GUI and have a think about the
message pump you'll spot that the message pump is just taking the
place of the message dispatcher in Smalltalk and allows GUIs on modern
operating systems to be written in procedural languages.


K
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,967
Messages
2,570,148
Members
46,694
Latest member
LetaCadwal

Latest Threads

Top