5.3 Reuse with Interfaces

 < Day Day Up > 



5.3 Reuse with Interfaces

Reuse is the principle that some parts of a program that have already been developed can be applied to new problems in the current program or even to an entirely different program. Usually, when programmers think of reuse they think of code that can either be copied or abstracted into a method that can then be applied in a different situation. For example, one of the most common ways to reuse code is to take an existing function or object, copy it, and modify it to make it work to solve a new problem. This is referred to as reuse by copy and is perhaps the most common form of reuse. This type of reuse where existing code is applied to new situations is covered in Chapters 10 and 11.

Another less common way to think about reuse in programming is to write algorithms, collection classes, and components so that they can be applied to a number of different data types. These data types, called generic data types, are data types for which the operations are defined but the actual types are not. Components that are implemented using generic data types are called generic components. When implementing a generic component, the implementation details of an object (such as the object's methods) are not being reused; rather, the way the algorithm or component acts on an object is being reused. A simple example of this type of reuse is the Vector class from Chapter 4, where the Vector could be used to store any object regardless of its data type.

Generic objects [2] can be implemented in two ways. One is to allow class, such as a collection class, to be defined with a dummy place-holder data type. This data type will be substituted with a real data type when the class is compiled, and a new class will actually be created for that specific data type at compile time. This type of reuse does not exist in Java, so an example using templates in C++ is shown in Exhibit 1 (Program5.1a). In this program, a class template is defined for a stack collection object. This stack will store items with a data type of ItemType, but the statement in the definition "template<class ItemType>" tells the compiler that this type is not a real type, but one that will be substituted for when the program is compiled.

Exhibit 1: Program5.1a: Class Template for Generic Stack

start example

 template<class ItemType> class Stack {   public:     Stack();     void push(ItemType item);     ItemType pop();   private:     int top;     ItemType items[10]; }; template<class ItemType> Stack<ItemType>::Stack() {   top = 0; } template<class ItemType> void Stack<ItemType>::push(ItemType item) {   items[top] = item;   top++; } template<class ItemType> ItemType Stack<ItemType>::pop() {   top -- ;   return items[top] = item; } 

end example

In the main method in Exhibit 2 (Program5.1b), the template class is used to define two new Stack classes, one that stores ints and the other that stores floats. When the program is compiled, it is as if the compiler replaces everywhere the occurrence of the ItemType string in the Stack class template with the actual type (int or float), and then the new template class with the correct type is compiled. This means that two stack classes are created, one class for each object. Nothing like templates currently exists in Java.

Exhibit 2: Program5.1b: Main Program Creating Two Stack Template Classes

start example

 int main() {   Stack<int> intStack;   Stack<float> floatStack; } 

end example

The second way to write generic objects is to use interfaces. Unlike a template, only one component class is ever defined for generic objects that use interfaces, but that one class can handle any data type that implements the interface. This allows the component classes using interfaces to be used with many actual data types. How this is accomplished is the subject of the rest of this chapter.

According to Gamma et al. [GAM95, p. 18], "Program to an interface, not an implementation." The reason is that components and algorithms written to apply to objects with a given behavior can be used with any object that implements that behavior. Designing components around interfaces rather than specific data types allows those components to be used in a wide variety of situations and separates the definition of the object from implementation of the object, which decouples the objects in the program and tends to make the program more robust.

[2]The term generic object will be used throughout the rest of this chapter to refer to to algorithms, collection classes, or components that are built around an interface.



 < Day Day Up > 



Creating Components. Object Oriented, Concurrent, and Distributed Computing in Java
The .NET Developers Guide to Directory Services Programming
ISBN: 849314992
EAN: 2147483647
Year: 2003
Pages: 162

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