uOttawaUniversity of Ottawa - Canadas University
list of dots

Umple User Manual    [Previous]   [Next]   

Loading

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 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. We may in future add a capability to Umple to ensure this cannot happen.

In the examples below, we show how using attribute notation is very simular 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
// 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

 

Another Example


// 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 Example

// 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