Data Structures and Algorithms with Object-Oriented Design Patterns in C#
next up previous contents index

Interfaces

Consider a program for creating simple drawings. Suppose the program provides a set of primitive graphical objects, such as circles, rectangles, and squares. The user of the program selects the desired objects, and then invokes commands to draw, to erase, or to move them about. Ideally, all graphical objects support the same set of operations. Nevertheless, the way that the operations are implemented varies from one object to the next.

We implement this as follows: First, we define a C# interface  which represents the common operations provided by all graphical objects. A C# interface declares a set of methods. An object that supports an interface must provide

Program gif defines the GraphicsPrimitives interface comprised of three methods, Draw, Erase, and MoveTo. the methods declared in the interface.

   program56983
Program: GraphicsPrimitives interface.

The Draw method is invoked in order to draw a graphical object. The Erase method is invoked in order to erase a graphical object. The MoveTo method is used to move an object to a specified position in the drawing. The argument of the MoveTo method is a Point struct. Program gif defines the Point struct which represents a position in a drawing.

   program57002
Program: Point struct.


next up previous contents index

Bruno Copyright © 2001 by Bruno R. Preiss, P.Eng. All rights reserved.