architecture OOP, Design Pattern

Discussion in 'C++' started by Vincent RICHOMME, Apr 20, 2007.

  1. 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.
    Vincent RICHOMME, Apr 20, 2007
    #1
    1. Advertising

  2. Vincent RICHOMME wrote:
    > 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
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Apr 20, 2007
    #2
    1. Advertising

  3. Victor Bazarov a écrit :
    > Vincent RICHOMME wrote:
    >> 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

    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 ...
    Vincent RICHOMME, Apr 21, 2007
    #3
  4. On Apr 21, 3:29 am, Vincent RICHOMME <> wrote:
    > 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
    =?iso-8859-1?q?Kirit_S=E6lensminde?=, Apr 21, 2007
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Muhammad Khan
    Replies:
    4
    Views:
    1,210
    Mike Treseler
    Jul 10, 2003
  2. sunny
    Replies:
    1
    Views:
    449
    Salt_Peter
    Dec 7, 2006
  3. Pallav singh
    Replies:
    0
    Views:
    341
    Pallav singh
    Jan 22, 2012
  4. Pallav singh
    Replies:
    0
    Views:
    387
    Pallav singh
    Jan 22, 2012
  5. Pallav singh
    Replies:
    1
    Views:
    442
    Peter Remmers
    Jan 22, 2012
Loading...

Share This Page