Standard Interfaces


In order to help your understanding of interfaces, it s useful to take a look at what other developers have created. In this section we will look at some of the common interfaces provided by Java and C#. This is by no means a complete list of interfaces, but it will show you some patterns in how the architects of these languages and their associated class and interface libraries designed their systems.

As we discuss these interfaces, keep the following in mind: Interfaces implement a behavior, which implies some other code will be calling methods within the interface. In our example, although all our classes implement the HTMLSource interface, it s the ShowHTML method that makes use of the interface. You can consider interfaces as providing callback functions.

Standard Java Interfaces

The following is a list of some commonly used Java interfaces, with a brief description of each.

actionListener

The actionListener interface is implemented by classes that want to be notified of actions. This is commonly seen implemented in classes such as forms to respond to user interface events such as button clicks. The actionPerformed method is the only member of this interface.

BeanInfo

This interface is implemented by JavaBean classes that want to provide information about what they provide. See the section titled Components for more information on JavaBeans.

Cloneable

Classes that provide a valid clone method should implement this interface. It has no methods but is used to indicate whether it is safe to call the clone method for an object.

Collection

Classes that maintain a collection implement this interface. It has methods that work with collections such as add , clear , isEmpty , size , and so on.

Note that the predefined collection classes such as LinkedList , Vector , HashSet , and so on, all implement this interface.

Iterator

Classes that provide some sort of collection usually implement this interface. Methods in the Iterator interface include hasNext , next , and remove .

Serializable

Classes that have their data saved to and loaded from a stream implement this interface.

Standard C# Interfaces

The following is a list of some commonly used C# interfaces, with a brief description of each.

ICloneable

Implemented by classes that support the clone method.

IComponent

If a class is to be considered a component, it should implement this interface. Note that the Component class implements this interface.

IContainer

Classes that contain components implement this interface. For example, the Form class is derived from a class that implements this interface.

ICollection

Classes that maintain a collection of data implement ICollection . Many classes are precreated with this interface already implemented.

INumerator

Classes that provide iteration or enumeration implement this interface.

ISerializable

Classes that have their data saved to and loaded from a stream implement this interface.

As you can tell from these lists, interfaces tend to be generic behaviors, such as implement a collection. You ll also note that Java and C# have similar interfaces. Interfaces make up a key part of how the JavaBeans and C# components work, in that they have predefined behaviors your new classes must implement before these classes can be considered a bean or component.




OOP Demystified
OOP Demystified
ISBN: 0072253630
EAN: 2147483647
Year: 2006
Pages: 130

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