11.07.2015 Views

Advanced Programming Guide

Advanced Programming Guide

Advanced Programming Guide

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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

3.5 Modeling Objects3.5 Modeling Objects • 103A principle application of modules is modeling objects. An object is somethingthat has both state and behavior. Many programming languagesprovide support for programming with objects. Some of these are calledobject-oriented; popular examples include Smalltalk, CLOS, Java TM , andC++.Maple is not an object-oriented programming language, but it doessupport programming with objects. In Maple, an object can be representedby a module. The state of the object (module) is stored in the localand exported data variables. The behavior of the object is represented byprocedures assigned to the exported variables. In Maple, procedures standon an equal footing with other expressions in the language; the distinctionbetween state and behavior is somewhat artificial and exists only asa convention.The essential idea behind programming with objects is that the objectscarry their behavior with them. Clients of an object can elicit behaviorby sending the object messages. Objects respond to these messagesby performing some prescribed computation that is determined by boththe recipient of the message (the object) and the message itself (whichmay be parameterized by other arguments). This is in contrast to nonobject-orientedapproaches to programming, in which the objects in asoftware system merely contain static data and serve as inputs and outputsof the algorithms, which are represented separately from the objectsby procedures or other routines.Objects and Constructors Objects are usually created by invoking aconstructor. A constructor is a procedure that builds the object expressionfrom some (possibly empty) set of inputs. Maple uses constructors for anumber of its native expression types. For example, the procedure tableis a constructor for Maple tables, and series is a constructor for Mapleseries expressions.A constructor must be used to create objects that have no inputsyntax (such as series and tables, in Maple), but can also be used forexpressions that do have an input syntax (the Float constructor is anexample of the latter case). Therefore, most user-defined objects must becreated by using a constructor. Most of the object examples in this sectionare defined by specifying a constructor for the object.Example: Complex Number Constructor A simple example of an objectis the following representation of a complex number.> MakeComplex := proc( real, imag )

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

Saved successfully!

Ooh no, something went wrong!