11.07.2015 Views

Advanced Programming Guide

Advanced Programming Guide

Advanced Programming Guide

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

110 • Chapter 3: <strong>Programming</strong> with ModulesNote: The fully commented source code for the PriorityQueue constructoris available in the samples directory of your Maple installation.An Object-oriented Shapes PackageThis section demonstrates an object-oriented approach to the Shapespackage described in section 3.3. The earlier revision of the package usedunevaluated function calls as the concrete representation of shapes. Thissection demonstrates how to build a package that offers the same functionality,but which represents shapes as objects. Each shape uses a moduleas its concrete representation. The package itself does not export thearea and circumference features of the traditional style package, becausethese features are available as part of each shape object. Instead,the package is merely a collection of constructors for the various kindsof shapes. You could use the object representation at a lower level, andpresent exactly the same interface as the first Shapes package, but thissection shows how to make the object-oriented nature of shape expressionsmore apparent to the user.The point Constructor Points are simple shapes, so the correspondingconstructor is similarly simple.> point := proc( x, y )> module()> export area, circumference, xcoord, ycoord;> xcoord := () -> x;> ycoord := () -> y;> area := () -> 0;> circumference := () -> 0;> end module> end proc:The module returned by this constructor uses the lexically scopedparameters x and y, representing the abscissa and ordinate of the point.These values are part of the local state, or instance data, of each pointconstructed. These points are captured in the closures of the exportedmethods, so that variables local to the module in which to store thesevalues are not necessary.The segment Constructor Segments are represented using the start andend points of the segment. These are the points returned by the pointconstructor.> segment := proc( pt1, pt2 )> module()> export area,> circumference,

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!