Previous Table of Contents Next


6.4 Example

   For example, in a business application it is extremely common to need to display a list of objects of a given type, with some identifying attribute like account number and a translated text description such as “Savings Account.? A developer might define an interface such as Describable whose methods provide this information, and implement this interface on a wide range of types. This allows the method that displays items to take an argument of type Describable and query it for the necessary information. The Describable objects passed in to the display method may be either CORBA interface types (passed in as object references) or CORBA value types (passed in by value).

   In this example, Describable is used as a polymorphic abstract type. No instances of type Describable exist, but many different instances have interfaces that support the Describable type abstraction. In C++, Describable would be an abstract base class; in Java, an interface. In statically typed languages, the compiler can check that the actual parameter type passed by callers of display is a valid subtype of Describable and therefore supports the methods defined by Describable. The display method can simply invoke the methods of Describable on the objects that it receives, without concern for any details of their implementation.

   Describable could not be declared as a regular IDL interface. This is because arguments of declared interface type are always passed as object references (see Section 5.2.4, “Parameter Passing,? on page 5-4) and we also want the display method to be able to accept value type objects that can only be passed by value.

   Similarly we cannot define Describable as a value type because then the display method would not be able to accept actual parameter objects that only support passing as an object reference. Abstract interfaces are needed to cover such cases.

   The Describable abstract interface could be defined and used by the following IDL:

   abstract interface Describable { string get_description();

   };

   interface Example { void display (in Describable anObject);

   };

   interface Account : Describable {// passed by reference // add Account methods here

   };

   valuetype Currency supports Describable {// passed by value // add Currency methods here

   };

   If Describable were defined as a regular interface instead of an abstract interface, then it would not be possible to pass a Currency value to the display method, even though the Currency IDL type supports the Describable interface.