list of dots Digital Research Alliance of Canada logo  NSERC logo  University of Ottawa logo / UniversitĂ© d'Ottawa

User Manual    [Previous]   [Next]   

Classes as Attribute Types

You can declare a class as a data type of an attribute. This allows for declaration of a 'containment' relationship.

If the data type you are using for an attribute is an Umple class, users should also consider using an association, and particularly a composition instead. This allows drawing of diagrams and more consistency checks by the Umple compiler.

If you use the methods in the Umple-generated API to access the object stored in such an attribute, and pass this contained object to some remote subsystem, then that remote subsystem will be able to affect the containing object in a backhanded sort of way. This is considered content coupling in software engineering, and should be carefully controlled or avoided.

In the examples below, we show how using attribute notation is very similar to using a directed association, except that with attribute notation, you can set the value to null.

Example

// This first example uses attribute notation
// The address is required on the constructor, but
// can be null. An instance of Address can be
// considered to be contained within the
// Person class.
//
// Although there is nothing currently preventing
// the same address instance from being re-used in
// multiple classes, it is strongly suggested to
// avoid that. If you want to reuse the same
// Address, then use association notation instead.
class Person {
  name;
  
  // The following uses a class as the attribute type
  Address address;
}

class Address {
  street;
  city;
  postalCode;
  country;
}
      

Load the above code into UmpleOnline

 

Alternative to the Above that Uses an Association


// Contrast this example with the previous one
// Here we use a directed association instead of 
// an attribute.
// Note that the multiplicity on the left is of no
// relevance. It is conventional to show it as *
// to indicate that the value could be attached to
// several objects.
//
// Here when you construct a Person, the address
// cannot be null. 
class Person {
  name;
  
  * -> 1 Address address;
}

class Address {
  street;
  city;
  postalCode;
  country;
}
      

Load the above code into UmpleOnline

 

Another Alternative with an Association Having Multiplicity Indicating Optional

// In this example, we have made the address
// optional. Now, it does not appear on the
// constructor. However when you add an address it
// still cannot be null.
//
// Note also that in this example, the role name
// 'address' is left off to show  it is optional.

class Person {
  name;
  
  * -> 0..1 Address;
}

class Address {
  street;
  city;
  postalCode;
  country;
}
      

Load the above code into UmpleOnline