Changes between Version 1 and Version 2 of DesignPatterns/Value


Ignore:
Timestamp:
Mar 5, 2005 12:24:18 PM (15 years ago)
Author:
hauma
Comment:

Matrix example added.

Legend:

Unmodified
Added
Removed
Modified
  • DesignPatterns/Value

    v1 v2  
    11= The Value Pattern =
    22
    3 The Value pattern is a meta-pattern summarizing all approaches to express things in an object oriented language that are not ''first-class'' objects.
     3The Value pattern is a meta-pattern summarizing all approaches to express all kind of data in an object oriented language that does not require all properties of ''first-class'' objects.
     4
     5== Objects with identity ==
    46
    57Besides encapsulated state that is accessed through the objet's methods, a first-class object has an ''identity''. This identity is unique for each created object. It distinguishes an object from all other objects, no matter whether those objects are instances of the same class, or encapsulate exactly the same state.
     
    79In Java, two objects (object references) {{{a}}} and {{{b}}} are identical, if the (non-overloadable) operator {{{==}}} returns {{{true}}} for the comparision {{{a == b}}}. Another way how the identity of objects can be revealed, is their usage as locks in synchronization operations. Two variables refer to identical objects, if no  concurrent access to regions {{{synchronized (a) {...} }}} and {{{synchronized(b) {...} }}} is granted.
    810
    9 Values do not (should not) have such identity. Two values are identical, if they ''behave'' equally. It makes no sense to use values locks in synchronization primitives.
     11== Objects without identity ==
     12
     13Values do not (should not) have such identity. Two values are identical, if they ''behave'' equally. E.g., it makes no sense to use values as locks in synchronization primitives.
     14
     15== Value example ==
     16
     17A class for representing matrices with operations "add", "invert", "scalarMultiply" and "matrixMultiply" should serve as example for a typical value class. Two matrix objects should considered identical, if they represent the same  mathematical object.
     18
     19There are two opposed approaches for implementing value classes. One is the mutable value class and the other is the immutable value class.
     20
     21=== Mutable value class ===
     22
     23Let us first consider the mutable value class approach. Typical signatures of the methods would look like in the following figure:
     24
     25{{{
     26class Matrix {
     27   void add(Matrix that, Matrix result);
     28   void invert(Matrix result);
     29   void scalarMultiply(double factor, Matrix result);
     30   void matrixMultiply(Matrix that, Matrix result);
     31}
     32}}}
     33
     34When programming in the mutable value class style, the caller of an operation can provide storage for result value. This is considered commonly most efficient, because the client can decide, whether storage allocation is required. This reduces unnecessary temporary storage allocation.
     35
     36
     37=== Immutable value class ===
     38
     39In the immutable value class style, each operation returns a fresh allocated matrix object. This is necessary, because a matrix once created cannot be modified any longer. A matrix class with the same operations but formulated in the immutable value style would look like in the following figure:
     40
     41{{{
     42class Matrix {
     43   Matrix add(Matrix that);
     44   Matrix invert();
     45   Matrix scalarMultiply(double factor);
     46   Matrix matrixMultiply(Matrix that);
     47}
     48}}}
     49