| MATLAB® | ![]() |
| On this page… |
|---|
Handle and value classes are useful in different situations. For example, value classes enable you to create new array classes that have the same semantics as MATLAB® numeric classes.
Example — A Polynomial Class and Example — Representing Structured Data provides examples of value classes.
Handle classes enable you to create objects that can be shared by more than one function or object. Handle objects allow more complex interactions among objects because they allow objects to reference each other.
Example — Implementing Linked Lists and Developing Classes — Typical Workflow provides examples of a handle class.
You should use a handle class when:
No two instances of a class can have exactly the same state, making it impossible to have exact copies. For example:
A copy of a graphics object (such as a line) has a different position in its parents list of children than the object from which it was copied, so the two objects are not identical.
Nodes in lists or trees having specific connectivity to other nodes—no two nodes can have the same connectivity.
The class represents physical and unique objects like serial ports or printers where the entity or state cannot exist in a MATLAB variable, but a handle to such entity can be a variable.
The class defines events and notifies listeners when an event occurs (notify is a handle class method).
The class creates listeners by calling the handle class addlistener method.
The class is derived from the dynamicprops class (a subclass of handle) so that instances can define dynamic properties.
The class is derived from the hgsetget class (a subclass of handle) so that it can implement a Handle Graphics™ style set/get interface.
Value class instances behave like normal MATLAB variables. A typical use of value classes is to define data structures. For example, suppose you want to define a class to represent polynomials. This class can define a property to contain a list of coefficients for the polynomial and implement methods that enable you to perform various common operations on the polynomial object, such as 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. See Subclassing MATLAB® Built-In Classes for more information on value classes.
![]() | Comparing Handle and Value Classes | The Handle Base Class | ![]() |
| © 1984-2008- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |