Listing 2: How circle_interface and circle_implementation work together with client code


int main()
    {
    //
    //  Call some magic API in the system that returns
    //  an implementation object.  Under the hood, this
    //  API calls 'malloc', 'new', or something
    //  similar to mint a new implementation object, or
    //  returns a pointer to an already existing object.
    //  For this example, I assume the creation succeeds.
    //
    circle_implementation *implementation = NULL;
    create_circle_implementation(&implementation);
    //
    //  Ask the implementation object to create an
    //  interface object.  Remember, the interface is an
    //  abstract base class, so you can't declare
    //  objects of that type -- only pointers and
    //  references.
    //
    circle_interface *interface =
            dynamic_cast<circle_interface *>
            (implementation->create_interface(CIRCLE_ID));
    if (interface != NULL)
        {
        //
        //  Implementation object supports the interface.
        //  Call the interface's members, then decrement
        //  the interface's reference count.
        //
        interface->set_radius(10);
        cout << "area is " << interface->get_area()
                << endl;
        interface->release();
        }
    //
    //  Free the implementation object the system handed
    //  back earlier.  Because this API is meant to
    //  be called from any language, it is not terribly
    //  object-oriented (e.g., we can't call
    //  'implementation->destroy()', or rely on the
    //  implementation's destructor).
    //
    destroy_circle_implementation(implementation);
    return 0;
    }
//End of File