MATLAB Object-Oriented Programming

(Joyce) #1

Avoid Property Initialization Order Dependency


In this section...
“Control Property Loading” on page 13-12
“Dependent Property with Private Storage” on page 13-13
“Property Value Computed from Other Properties” on page 13-14

Control Property Loading


Problems can occur if property values depend on the order in which load sets the
property values.

Suppose that your class design is such that both of the following are true:


  • A property set method changes another property value.

  • A property value is computed from other property values.


Then the final state of an object after changing a series of property values can depend on
the order in which you set the properties. This order dependency can affect the result of
loading an object.

The load function sets property values in a particular order. This order can be different
from the order in which you set the properties in the saved object. As a result, the loaded
object can have different property values than the object had when it was saved.

Restore Nondependent Properties

If a property set function changes the values of other properties, then define the
Dependent attribute of that property as true. MATLAB does not save or restore
dependent property values.

Use nondependent properties for storing the values set by the dependent property. Then
the load function restores the nondependent properties with the same values that were
saved. The load function does not call the dependent property set method because there
is no value in the saved file for that property.

13 Saving and Loading Objects

Free download pdf