MATLAB Object-Oriented Programming

(Joyce) #1

Which Kind of Class to Use


In this section...
“Examples of Value and Handle Classes” on page 7-11
“When to Use Value Classes” on page 7-11
“When to Use Handle Classes” on page 7-12

Examples of Value and Handle Classes


Handle and value classes are useful in different situations. For example, value classes
enable you to create new array classes that have the same behavior as MATLAB numeric
classes.

“Representing Polynomials with Classes” on page 19-2 and “Representing Structured
Data with Classes” on page 3-19 provides examples of value classes.

Handle classes enable you to create objects that more than one function or object can
share. Handle objects allow more complex interactions among objects because they allow
objects to reference each other.

“Implementing Linked Lists with Classes” on page 3-31 and “Developing Classes —
Typical Workflow” on page 3-8 provides examples of a handle class.

When to Use Value Classes


Value class objects behave like normal MATLAB variables. A typical use of value classes is
to define data structures. For example, suppose that you want to define a class to
represent polynomials. This class can define a property to contain a list of coefficients for
the polynomial. It can implement methods that enable you to perform various operations
on the polynomial object. For example, implement addition and multiplication without
converting the object to another class.

A value class is suitable because you can copy a polynomial object and have two objects
that are identical representations of the same polynomial. For an example of value
classes, see “Subclasses of MATLAB Built-In Types” on page 12-50.

For information on MATLAB pass-by-value semantics, see “Avoid Unnecessary Copies of
Data”.

Value or Handle Class — Which to Use

Free download pdf