Re: Class interaction question
Angus <anguscomber@gmail.com> wrote in news:1187798603.500722.26020
@m37g2000prh.googlegroups.com:
I am designing an FTP server and with FTP commands are dispatched on
one port and the actual data transmission on another.
So I thought I would have a CFTPControl class for handlng the commands
and a CFTPData class for the actual data transmission.
But CFTPControl needs to know when the data transmission has
completed. What is the best way to handle this interaction between
the classes?
It seems like the Observer pattern is a natural fit.
class CFTPData;
class IDataReport
{
public:
virtual void OnDataDone(CFTPData *) = 0;
virtual ~IDataReport() {}
};
class CFTPData
{
IDataReport * m_pObserver;
public:
void RegisterObserver(IDataReport * pObserver)
{ m_pObserver = pObserver; }
void Process()
{ /* Do stuff */ if (m_pObserver) m_pObserver->OnDataDone(this); }
};
class CFTPControl : private IDataReport
{
public:
void Observe(CFTPData * pSubject)
{ pSubject->RegisterObserver(this); }
private:
void OnDataDone(CFTPData * pSubject)
{ /* wheee the data is done */ }
};
I don't know that you strictly need the pointer to the subject, but I
have yet to use the Observer pattern where knowing which object was
notifying you wasn't important in some fashion. Obviously, the single
pointer can be a vector of pointers if you are going to allow more than
one observer. The functionality in the Observe() method can be done
anyplace you have a pointer to the subject, so don't take the above too
literally.
joe