DOUBLE TRY

Ensures the automatic connection establishment, when a request is made.


class Connection
{
  public:
    void request( const string & req );
    // ...
};

class Connector
{
  public:
    Connection * connect();
    // ...
};


The Double-Try pattern is then,


Connector * connector;
Connection * connection;

{
  if ( connection ) 
  {
    try {
      connection->request( ... );
    } catch ( ConnectionException & e ) {
      delete connection;
    }
  }
  
  if ( ! connection )
  {
    try {
      connection = connector->connect();
      connection->request( ... );
    } catch ( ConnectionException & e ) {
      if ( connection ) 
        delete connection;
      // safe backup for request failure
    }
  }
}
The ConnectionException may be hierarchically structured as

Marco Corvi - Page hosted by geocities.com.