uOttawaUniversity of Ottawa - Canadas University
list of dots

Umple User Manual    [Previous]   [Next]   

Loading

Immutable Pattern

Mark a class as immutable to force all its contents to be immutable. The code ensures all attributes and associations can only be set in the constructor, and cannot be modified again subsequently.

The only associations allowed to be immutable are directed associations to other immutable classes

A class can only be immutable if all its superclasses are also immutable. Declaring a superclass immutable forces its subclasses to be immutable; in other words, immutability is inherited. If the subclasses break immutability constraints (such as the type of attributes allowed), then errors will be raised.

Individual attributes and associations can be marked as immutable instead of the entire class. An attribute can be marked as lazy immutable if it needs to be initialized after the constructor has finished.

For more details on the Immutable pattern, see this Wikipedia page.


Example

// A simple example of an immutable class
class Point2D
{
  immutable;
  Float x;
  Float y;
}

      

Load the above code into UmpleOnline

 

Another Example

// An example of one immutable class making reference to another

class Path
{
  immutable;
  1 -> * Point2D pathElements;
}

class Point2D
{
  immutable;
  Float x;
  Float y;
}

      

Load the above code into UmpleOnline

 

Another Example

// Example of the declaration of an association to be immutable
// Note that this can be set only the first time
class X {
  Integer id;
  immutable * -> 0..1 Y;
}

class Y {
  immutable;
  someInfo;
}  

      

Load the above code into UmpleOnline

 

Syntax


association : [=modifier:immutable]? [[associationEnd]] [=arrow:--
    |->
    |<-
    |><
    |<@>-
    |-<@>] [[associationEnd]] ;

inlineAssociation : [=modifier:immutable]? [[inlineAssociationEnd]] [=arrow:--
    |->
    |<-
    |><
    |<@>-
    |-<@>] [[associationEnd]] ;

derivedAttribute- : [=modifier:immutable
    |settable
    |internal
    |defaulted
    |const
    |fixml]? [[typedName]] = ( [[moreCode]] )+

complexAttribute- : [=unique]? [=lazy]? [=input]? [=modifier:immutable
    |settable
    |internal
    |defaulted
    |const
    |fixml]? [[typedName]] (= [**value])? ;

softwarePattern- : [[isA]] | [[singleton]] | [[immutable]] | [[keyDefinition]] | [[codeInjection]]

// A class that can't be modified when created ImmutablePattern
immutable- : [=immutable] ;

association : association [associationNum];