MATLAB Object-Oriented Programming

(Joyce) #1

The details of how methods perform these steps are encapsulated in the class design.
Each node object contains the functionality to insert itself into or remove itself from the
list.


For example, in this class, every node object has an insertAfter method. To add a node
to a list, create the node object and then call its insertAfter method:


nnew = NodeConstructor;
nnew.insertAfter(n1)


Because the node class defines the code that implements these operations, this code is:



  • Implemented in an optimal way by the class author

  • Always up to date with the current version of the class

  • Properly tested

  • Can automatically update old-versions of the objects when they are loaded from MAT-
    files.


The object methods enforce the rules for how the nodes interact. This design removes the
responsibility for enforcing rules from the applications that use the objects. It also means
that the application is less likely to generate errors in its own implementation of the
process.


Fostering Modularity


As you decompose a system into objects (car –> engine –> fuel system –> oxygen sensor),
you form modules around natural boundaries. Classes provide three levels of control over
code modularity:



  • Public — Any code can access this particular property or call this method.

  • Protected — Only this object's methods and methods of the object's derived from this
    object's class can access this property or call this method.

  • Private — Only the object's own methods can access this property or call this method.


Overloaded Functions and Operators


When you define a class, you can overload existing MATLAB functions to work with your
new object. For example, the MATLAB serial port class overloads the fread function to
read data from the device connected to the port represented by this object. You can define
various operations, such as equality (eq) or addition (plus), for a class you have defined
to represent your data.


Why Use Object-Oriented Design
Free download pdf