Interface and Abstract Class
Synopsis
You need to keep client classes independent of classes that implement a behavior and ensure consistency of behavior between the behavior-implementing classes. Don’t choose between using an interface and an abstract class; have the classes implement an interface and extend an abstract class.
Context
You are designing a framework. You want to hide the class or classes that implement some behavior by making the classes private to their package and having them implement a public interface. For the sake of consistency and convenience of implementation, you want the classes to extend a common abstract class. You are not sure how to decide between basing the classes on an interface and an abstract class.
Forces
| Using the Interface pattern, Java interfaces can be used to hide the specific class that implements a behavior from the clients of the class. |
| Organizing classes that provide related behaviors with a common superclass helps to ensure consistency of implementation. Through reuse, it may also reduce the effort required for implementation. |
| When people are presented with two different ways to improve the organization of classes, there is a common tendency to choose either one or the other. |
Solution
If you are presented with the need to hide from its clients the class of an object that provides a service, then use the Interface pattern. Have the client objects access the service-providing object indirectly through an interface. The indirection allows the clients to access the service-providing object without having to know what kind of objects they are.
If you need to design a set of related classes that provide similar functionality, then organize the common portions of their implementation into an abstract superclass.
If you are presented with both of these needs in the same object design, then use both an interface and an abstract class as shown in Figure 4.11.
Figure 4.11: Interface and abstract class.
When using the combination of an interface and abstract class in this way, the interface is public; the abstract class is package private, if possible.
Consequences
| Using the Interface and Abstract Class pattern allows an object design to benefit from both an interface and an abstract class. |
Java API Usage
The package javax.swing.table contains interfaces and classes to support tables in a user interface. For every table that appears in a user interface, a corresponding data model object contains the data values displayed in the table. To be used as the data model for a table, an object must be an instance of a class that implements the javax.swing.table.TableModel interface. The package also includes a class named AbstractTableModel. AbstractTableModel is an abstract class that contains some default logic useful in implementing the methods declared by the TableModel interface. Finally, there is a concrete class named DefaultTableModel that is the default class used to instantiate a data model for a table. These relationships are shown in Figure 4.12.
Figure 4.12: javax.swing.table relationships.
Code Example
The code example for the Interface and Abstract Class pattern consists of an interface and classes for managing a data structure called a doubly linked list. There is a class called DoubleLinkedListMgr that performs various insert and delete operations on the elements of a doubly linked list. The DoubleLinkedListMgr class does not require that the objects in the doubly linked list be instances of any particular class. It does require that all of the elements implement an interface called DoubleLinkIF. There is an abstract class called AbstractDoubleLink that implements the DoubleLinkIF interface. Extending the AbstractDoubleLink class is a convenient way to write concrete classes that can be manipulated in a doubly linked list.
These classes and the interface are part of org.clickblocks.dataStructure package of the ClickBlocks software on the Web site for this book.
The following is a listing of the DoubleLinkIF interface:
public interface DoubleLinkIF {
/**
* Return the node that follows this one in the linked
* list or null if this is the last node.
*/
public DoubleLinkIF getNext() ;
/**
* Set the node that is to follow this one in the linked
* list.
*
* @param node
* The node that is to follow this one in the
* linked list or null if this node is to be the
* last one in the list.
*/
public void setNext(DoubleLinkIF newValue) ;
/**
* Return the node that precedes this one in the linked
* list or null if this is the first node.
*/
public DoubleLinkIF getPrev() ;
/**
* Set the node that is to precede this one in the linked
* list.
*
* @param node
* The node that is to precede this one in the
* linked list or null if this node is to be the
* first one in the list.
*/
public void setPrev(DoubleLinkIF newValue) ;
} // interface DoubleLinkIF
The following is a listing of the abstract class AbstractDoubleLink that implements the DoubleLinkIF interface:
public abstract class AbstractDoubleLink
implements DoubleLinkIF {
private DoubleLinkIF previous;
private DoubleLinkIF next;
/**
* Return the node that follows this one in the linked
* list or null if this is the last node.
*/
public DoubleLinkIF getNext() { return next; }
/**
* Set the node to follow this one in the linked list.
*
* @param node
* The node to follow this one or null if this node
* is to be the last one in the list.
*/
public void setNext(DoubleLinkIF newValue) {
next = newValue;
} // setNext(DoubleLinkIF)
/**
* Return the node that precedes this one or null
* if this is the first node.
*/
public DoubleLinkIF getPrev() { return previous; }
/**
* Set the node that is to precede this one.
*
* @param node
* The node that is to precede this one or
* null if this node is to be the first.
*/
public void setPrev(DoubleLinkIF newValue) {
previous = newValue;
} // setPrev(DoubleLinkIF)
...
} // class AbstractDoubleLink
Classes that extend the AbstractDoubleLink class generally add additional information of their own.
Related Patterns
Interface. The Interface and Abstract Class pattern uses the Interface pattern.
Abstract Class. The Interface and Abstract Class pattern uses the Abstract Superclass pattern.
No comments:
Post a Comment