Pattern: Value Model


Context

A 'value' is not always a single standalone object. It may be part of another larger object. It may be computed from other objects when it is required. It may be shared by more than one client. These implementation details should be hidden from users of the 'value'.

Solution

Hide the implementation details of the value by using a ValueModel. This will provide a standard interface to a value so that it can be accessed by a common operation. Interested parties will be notified when the value changes.

A ValueModel provides a layer of indirection between a value object (we'll call this the subject) and other objects which need to get and set the value of the subject (we'll call these clients).

ValueModel is an Abstract Class that defines a standard protocol implemented by its concrete subclasses. It defines #value and #value: methods for getting and setting the value of the subject. The #value method answers the value of the subject. The #value: method sets the value of the subject. In addition, whenever the value of the subject is changed, the ValueModel triggers the #value event. Clients can register an interest in the subject value, and they will be notified whenever it is changed.

ValueHolder is a concrete subclass of ValueModel. It is a specialisation of ValueModel which holds its own value.

ValueAspectAdaptor is a concrete subclass of ValueModel. It automatically configures its #value and #value: methods to access a particular aspect of the subject.

Known Uses

SmalltalkSystem>>chooseDefaultFont a FontDialog on the #font aspect of the Desktop. The FontDialog is able to access and manipulate the font aspect as a single value by sending value and value: messages to a ValueAspectAdaptor created on the aspect.

SmalltalkSystem>>chooseDefaultFont
	"Choose the default font to use for the development system tools"

	(FontDialog on: (View desktop aspectValue: #font)) showModal
Object>>aspectValue: anAspectSymbol
	"Answer a ValueAspectAdapter capable of treating anAspectSymbol of the receiver
	as a ValueModel"

	^ValueAspectAdaptor subject: self aspect: anAspectSymbol

Related Patterns