I’m studying some source codes and I’m just wondering why a class (or classes) is often implemented this way:
class EventHandler
{
...
EventDispatcher* m_Dispatcher;
virtual bool ProcEvent( EventHandler* Sender, int Task, int Event, void* Param );
...
};
class ClassA: public EventHandler
{
...
ClassA* m_Impl;
ClassA* m_Iface;
...
public:
// virtual functions
virtual bool ProcEvent( EventHandler* Sender, int Task, int Event, void* Param );
virtual void OnDataWritten(const PIOResult&) {;}
...
// sample functions
void SetImplement( ClassA* aImpl );
void SetInterface( ClassA* aIface );
ClassA* GetImplement() { return m_Impl; }
ClassA* GetInterface() { return m_Iface; }
bool GetData( list& aList );
};
// Implementation of some sample functions; Most of its function contain more
// or less the same format as below, with the return m_Impl->XXX having the same
// name as the function being defined (e.g. A::XXX)
bool ClassA::GetData( list< Data >& aList )
{
if( m_Impl )
return m_Impl->GetData( aList );
else
return false;
}
class ClassAFactory: public EventHandler
{
private:
ClassAFactory* m_Impl;
ClassAFactory* m_Iface;
protected:
virtual ClassA* MakeTransport();
virtual bool ProcEvent( EventHandler* Sender, int Task, int Event, void* Param );
virtual ClassA* CreateClassA() { return 0; }
...
};
// In some member function of ClassB (ClassB inherits ClassA)
switch( status )
{
case 1:
GetInterface()->OnDataWritten();
case 2:
// ...
};
I believe it’s for some design pattern but I’m not familiar with it. It could help me understand if I know what it is. Can anyone help me point out which it could be or what is the use of these classes such that it is implemented this way?
I think it’s for some event handling and used together with some factory but I’m not sure.
I am afraid the names used do not have the
usualmeaning, so without examples of what is put in or how they are used, it’s going to be difficult to guess.There are 2 design patterns that you should check, that make heavy use of this kind of
self-recursion(at class level*):And I am afraid that you are looking at something that fails to emulate either of those.
In the
Decorator, the point is to add functionality. To this end you have anInterfaceof which derives aConcreteclass and aWrapperinterface. Then various wrappers will derive ofWrapperand you can chain them:Each wrapper add some functionality, whereas here we only have perfect forwarding… so it’s not a
Decorator.The
Compositepattern is different. Its goal is to hide whether you treat with a collection of elements or a single element. The usual example is a tree: you can apply operations either to an entire subtree or just a leaf node if it’s implemented with aCompositePattern.Once more, there is not such thing here.
So my best guess is that you have either a wild design (perhaps a misguided attempt to emulate a well-known pattern) or you haven’t given enough information (source code) for us to figure out the role. It seems strange anyway.
*Note: by
self-recursionat class level I mean that an object of classApoints to another object of classA, but this does not mean (certainly) that it points to the same instance… otherwise we would have a Stack Overflow (pun intended). This not the same instance bit is certainly worth checking during theSetImplementationcall: note that any cyclic reference would cause death by recursion.