MATLAB Object-Oriented Programming

(Joyce) #1

Restore Listeners


In this section...
“Create Listener with loadobj” on page 13-36
“Use Transient Property to Load Listener” on page 13-36
“Using the BankAccount and AccountManager Classes” on page 13-38

Create Listener with loadobj


Suppose that you create a property listener and want to be able to save and restore the
event source and the listener. One approach is to create a listener from the loadobj
method.

Use Transient Property to Load Listener


The BankAccount class stores the account balance and an account status. A PostSet
listener attached to the AccountBalance property controls the account status.

When the AccountBalance property value changes, the listener callback determines the
account status. Important points include:


  • The BankAccount class defines the AccountManagerListener property to contain
    the listener handle. This property enables the loadobj method to create a listener
    and return a reference to it in the object that is loaded into the workspace.

  • The AccountManagerListener property is Transient because there is no need to
    store the listener handle with a BankAccount object. Create a listener that is
    attached to the new BankAccount object created during the load process.

  • The AccountBalance listener sets the AccountStatus.

  • Only the AccountManager class can access AccountStatus property.


classdef BankAccount < handle
properties (SetObservable, AbortSet)
AccountBalance
end
properties (Transient)
AccountManagerListener
end
properties (Access = ?AccountManager)

13 Saving and Loading Objects

Free download pdf