Wednesday, November 19, 2008

Example : COM Interface


Here's another way to visualize an interface. In this section we'll present a COM interface without any of the C++ baggage. We'll try to look at an interface in its abstract form. Imagine a "car" object. All "car" objects that you are familiar with in the real world have a "driving" interface that allows you to direct the car left and right and also to speed the car up and slow it down. The member functions for the driving interface

might be "left", "right", "faster", "slower", "forward" and "reverse". Many cars also happen to have a "radio" interface as well, if they have a radio installed. The functions for the radio interface might be "on", "off", "louder", "softer", "next station" and "previous station".


Driving Radio

Left() On()

Right() Off()

Slower() Louder()

Faster() Softer()

Forward() NextStation()

Reverse() PrevStation()

There are many kinds of cars, but not all of them have radios. Therefore, they do not implement the radio interface, although they do support

the driving interface. In all cars that do have radios the capabilities of the radio are the same. A person driving a car without a radio can still

drive, but cannot hear music. In a car that does have a radio, the radio interface is available.

COM supports this same sort of model for COM classes. A COM object can support a collection of interfaces, each of which has a name. For

COM objects that you create yourself, you will often define and use just a single COM interface. But many existing COM objects support

multiple COM interfaces depending on the features they support.

Another important distinction is that the driving interface is not the car. The driving interface doesn't tell you anything about the brakes, or the

wheels, or the engine of the car. You don't drive the engine for example, you use the faster and slower methods (accelerator and brakes) of

the driving interface. You don't really care how the slower (brake) method is implemented, as long as the car slows down. Whether the car has

hydraulic or air brakes isn't important.

0 comments:

Your Title