Implementing an Interface

An interface implementation is a single unit of functionality that is instantiated through the interface definition. An implementation is created by deriving a class from the interface definition, and implementing its functions. An interface implementation actually provides the services defined by the interface.

  1. Derive a class from the interface definition.
  2. Implement the object creation functions like NewL() or ConstructL() for the derived class.
  3. Call the appropiate REComSession::CreateImplementationL() function to instantiate an interface implementation that satisfies the specified interface.
  4. Implement the pure virtual functions defined by the interface definition, to provide required services to clients.

Creating an interface implementation example

The following code depicts an example segment of the interface definition class derived from CActive.

class CExampleInterfaceDefinition : public CActive
    {
public:
    // Wraps ECom object instantiation
    static CExampleInterfaceDefinition* NewL();
    // Wraps ECom object destruction 
    virtual ~CExampleInterfaceDefinition();
    // Interface service 
    virtual void DoMethodL() = 0;
...
private:
    // Instance identifier key or UID.
    TUid iDtor_ID_Key;
    };

The following code depicts a class derived from the interface definition CExampleInterfaceDefinition.

class CExampleIntImplementation : public CExampleInterfaceDefinition
    {
public:
    // Two-phase constructor
    static CExampleIntImplementation* NewL();
    // Destructor
    ~CExampleIntImplementation();
    // Implement the required methods from CExampleInterfaceDefinition 
    void DoMethodL();

private:
    void ConstructL();
    };

The following code depicts the implementation of the object creation functions and virtual functions in CExampleInterfaceDefinition.

CExampleIntImplementation* CExampleIntImplementation::NewL()
{
    CExampleIntImplementaion self = new (ELeave) CExampleIntImplmentation();
    CleanupStack::PushL(self);
    self->ConstructL();
    CleanupStack::Pop();
    return self;
}

void CExampleIntImplementation::ConstructL()
{
// specific implementation
}
CExampleIntImplementation::~CExampleIntImplementation()
{
}
    ~CExampleIntImplementation();

void CExampleIntImplementation::DoMethodL()
{
    // specific implementation of the DoMetholdL()
}