Data Structures and Algorithms
with Object-Oriented Design Patterns in C# |
Abstract classes can be used in many interesting ways. One of the most useful paradigms is the use of an abstract class for algorithmic abstraction . The Erase and MoveTo methods defined in Program are examples of this.
The Erase and MoveTo methods are implemented in the abstract class GraphicalObject. The algorithms implemented are designed to work in any concrete class derived from GraphicalObject, be it Circle, Rectangle or Square. In effect, we have written algorithms that work regardless of the actual class of the object. Therefore, such algorithms are called abstract algorithms.
Abstract algorithms typically invoke abstract methods. For example, both MoveTo and Erase ultimately invoke Draw to do most of the actual work. In this case, the derived classes are expected to inherit the abstract algorithms MoveTo and Erase and to override the abstract method Draw. Thus, the derived class customizes the behavior of the abstract algorithm by overriding the appropriate methods. The C# method resolution mechanism ensures that the ``correct'' method is always called.