MATLAB Object-Oriented Programming

(Joyce) #1

When to Use Handle Classes


Handle objects are useful in specialized circumstances where an object represents a
physical object such as a graph or an external device rather than a mathematical object
like a number or matrix. Handle objects are derivations of the handle class, which
provides functionality such as events and listeners, destructor method, and support for
dynamic properties.

Use a handle class when:


  • No two instances of a class can have 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. Therefore, 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 and printers.

  • The class represents visible objects like graphics components.

  • 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 subclasses the dynamicprops class (a subclass of handle) so that
    instances can define dynamic properties.

  • The class subclasses the matlab.mixin.SetGet class (a subclass of handle) so that
    it can implement a graphics object style set/get interface to access property values.

  • You want to create a singleton class or a class in which you track the number of
    instances from within the constructor.

  • Instances of a class cannot share state, such as nodes in a linked list.


See Also


Related Examples



  • “Handle Compatible Classes” on page 12-39


7 Value or Handle Class — Which to Use

Free download pdf