search

Interface myphysicslab.lab.util.Subject

Provided By
All Extended Interfaces
All Known Subinterfaces
All Known Implementations

A Subject notifies its Observers when something changes in the Subject. This can be a change in the value of a Parameter, or the occurrence of an GenericEvent. The Subject maintains a list of its Observers. An Observer is connected to the Subject via the #addObserver method, which is typically called by the Observer's constructor or the entity that creates the Observer.

See also the myPhysicsLab documentation about Subject, Observer, Parameter.

When a change occurs in the Subject, the #broadcast method should be called to inform all Observers of the change. For a Parameter, the 'setter' method of the Subject should call #broadcastParameter at the end of the setter method.

To enable universal scripting of simulations, we need Parameters and SubjectEvents to have language independent names. Therefore Parameter.getName() and SubjectEvent.getName() return a language independent name which is derived from the English localized name by converting the English name to uppercase and replacing spaces and dashes by underscore. You can use the function myphysicslab.lab.util.UtilityCore#toName to convert an English name to the universal name. Or use SubjectEvent.nameEquals() which handles the conversion to language independent name.

The Subject and Observer interfaces are an implementation of the well known Observer design pattern.

Example Scenario

This section shows an example usage of the Subject and Observer interfaces, along with Parameters and user interface controls.

TO DO: ELASTICITY IS NO LONGER DEFINED ON RIGIDBODYSIM NEED TO UPDATE THIS

Subject/Observer Relationships with Parameters

The diagram shows a RigidBodySim as the Subject. It has a list of Parameters, including a Parameter representing the elasticity of the rigid bodies. The Subject also has a list of Observers, including a NumericControl which is connected to the elasticity ParameterNumber. In its constructor, the NumericControl adds itself to the list of Observers by calling #addObserver on the Subject.

Whenever the elasticity is changed, the Subject should notify each Observer by calling #broadcast. In this example only the NumericControl cares about the elasticity Parameter. The NumericControl will then call ParameterNumber.getValue to get the new value, and the ParameterNumber in turn finds the value by calling a certain 'getter' method on the Subject.

If the user modifies the value of the NumericControl, the NumericControl will then call ParameterNumber.setValue to modify the value; the ParameterNumber in turn calls a certain 'setter' method on the Subject to change the value. This also causes the Subject to notify all Observers of this change by calling Subject.broadcast.

This design is very decoupled. The Subject knows nothing about the NumericControl except that it is an Observer. The Parameter is entirely unaware of the NumericControl. The NumericControl only knows that the Parameter is of type ParameterNumber and that it has a Subject providing notification of changes.

Instance Methods