18.20. Interfaces


There are times when a designer does not want to create a new type. Rather, the designer wants to describe a set of behaviors that any number of types might implement. For example, a designer might want to describe what it means to be storable (i.e., capable of being written to disk) or printable.

Such a description is called an interface. An interface is a contract; the designer of the interface says "if you want to provide this capability, you must implement these methods." The implementer of the interface agrees to the contract and implements the required methods.

When a class implements an interface, it tells any potential client, "I guarantee I'll support the methods, properties, and events of the named interface." The interface details the return type from each method and the parameters to the methods.

It is easy to get confused about who is responsible for what with interfaces . There are three concepts to keep clear:


The interface

This is the contract. By convention, interface names begin with a capital I, and so your interface might have a name like IPrintable. The IPrintable Interface might describe a method Print. (Creating and using interfaces is described below.)


The implementing class

This is the class that agrees to the contract described by the interface. For example, Document might be a class that implements IPrintable and thus implements the Print method.


The client class

This is a class that calls methods on the implementing class. For example, you might have an Editor class that calls the Document's Print method.



Programming Visual Basic 2005
Programming Visual Basic 2005
ISBN: 0596009496
EAN: 2147483647
Year: 2006
Pages: 162
Authors: Jesse Liberty

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net