Saturday, October 24, 2009

Abstract Superclass




















Chapter 4 -
Fundamental Design Patterns
Patterns in Java, Volume 1: A Catalog of Reusable Design Patterns Illustrated with UML, Second Edition
by Mark Grand
John Wiley & Sons � 2002



























Abstract Superclass



This pattern was originally described in [Rhiel00].




Synopsis


Ensure consistent behavior of conceptually related classes by giving them a common abstract superclass.






Context


You want to write classes to provide sequential and read-only access to some data structures. You decide that these classes will implement the interface java.util.Iterator.


The Iterator interface includes a method called remove. The documented purpose of the remove method is to remove objects from the source over which an Iterator object iterates. However, the description of the remove method also says that it is an optional method; an implementation of the method may simply throw an UnsupportedOperationException.


Because your intention is to provide read-only access to data structures, you want all of your classes to implement the remove method by throwing an UnsupportedOperationException. To ensure that these classes implement the remove method in the same way, you create a common abstract class for all of your Iterator classes to inherit from. The common superclass implements the remove method by having it always throw an UnsupportedOperationException. This organization is shown in Figure 4.9.






Figure 4.9: Iterators with abstract superclass.





Forces




















J



You want to ensure that logic common to related classes is implemented consistently for each class.




J



You want to avoid the runtime and maintenance overhead of redundant code.




J



You want to make it easy to write related classes.




L



You want to organize common behavior, although in many situations, inheritance is not an appropriate way to accomplish this. The Delegation pattern describes this in detail








Solution


Organize the common behavior of related classes into an abstract superclass.


To the extent possible, organize variant behavior into methods with common signatures.2 Declare the abstract superclass to have abstract methods with these common signatures. Figure 4.10 shows this organi zation.






Figure 4.10: Abstract Superclass pattern.

The following are the roles that classes play in the Abstract Superclass pattern:



AbstractSuperclass.  A class in this role is an abstract superclass that encapsulates the common logic for related classes. The related classes extend this class so they can inherit methods from it. Methods whose signature and logic are common to the related classes are put into the superclass so their logic can be inherited by the related classes that extend the superclass. Methods with different logic but the same signature are declared in the abstract class as abstract methods, ensuring that each concrete subclass has a method with those signatures.



ConcreteClass1, ConcreteClass2, and so on.  A class in this role is a concrete class whose logic and purpose is related to other concrete classes. Methods common to these related classes are refactored into the abstract superclass.


Common logic that is not encapsulated in common methods is refactored into common methods.






Implementation


If the common method signatures are public, they should, if possible, be organized into a Java interface that the abstract class implements.






Consequences














J



Fewer test cases may be needed to completely test your classes, because there are fewer pieces of code to test.




L



Using the Abstract Superclass pattern creates dependencies between the superclass and its subclasses. Changes to the superclass may have unintended effects on some subclasses, thus making the program harder to maintain.








Java API Usage



The class java.awt.AWTEvent is an abstract class for classes that encapsulate events related to the graphical user interface (GUI). It defines a small number of methods that are common to user-interface event classes.






Code Example


Implementations of the classes discussed under the Context heading are the code example for this pattern. These classes are taken from the ClickBlocks software, which you can find on the Web site for this book in the org.clickblocks.util package.


The following is a listing of the AbstractIterator class:



/**
* This abstract class provides the convenience of allowing
* Iterators to be defined by overriding just the
* getNextElement single method.
*/
abstract public class AbstractIterator implements Iterator {
private Object nextElement;
/**
* This method must be called by a subclass's constructor.
*/
protected void init() {
        nextElement = getNextElement();
    }  // init()
/**
* This method returns the next element in the data
* structure to be traversed. If there is no next
* element, then return this object.
*/
    public abstract Object getNextElement();
/**
* Return true if the iteration has more elements.
*/
    public boolean hasNext(){
return nextElement!=this;
} // hasNext()
/**
* Returns the next element in the iteration.
*
* @exception NoSuchElementException iteration has no more elements.
*/
public Object next(){
if (nextElement==this) {
            throw new NoSuchElementException();
        } // if
Object previous = nextElement;
        nextElement = getNextElement();
        return previous;
} // next()
/**
* Remove from the underlying collection the last element
* returned by the next method.
*
* @exception UnsupportedOperationException
* if the remove operation is not supported by
* this Iterator.
*/
    public void remove(){
        throw new UnsupportedOperationException();
} // remove()
} // class AbstractIterator






Related Patterns



Interface and Abstract Class.  The Interface and Abstract Class pattern uses the Abstract Superclass pattern.



Template Method.  The Template Method pattern uses the Abstract Superclass pattern.


















No comments:

Post a Comment