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

User Manual    [Previous]   [Next]   

One-to-One Associations

The examples of associations in the previous user manual pages all showed cases where the lower-bound of the multiplicity at both ends is zero. For example there can be zero graduate students supervised by a professor, and zero professors might be assigned to supervise a graduate student initially.

Allowing a lower bound of zero means that one object can be created without having to first create the object(s) it is to be linked to. The vast majority of associations have a lower bound of zero at at least one of the two ends.

However there are a few situations where the lower bound at both ends of the association ought to be greater than zero. Here we will consider the one-to-one case. In the example below, a Company must always have one BoardOfDirectors and a BoardOfDirectors must always belong to one Company. But which should be created first? If we create the Company first, then a multiplicity constraint would be violated since for a period of time it would lack a Board. One solution would be to relax the constraint and decide that the association should not be 1-1. However Umple does allow 1-1 associations and creates a special API to allow both objects to be created at the same time.

The generated code has a constructor with the arguments normally found in the constructors of both associated classes. So in the following main program, the call to the constructor of Company with just a single argument (company name) will construct both the Company and the BoardOfDirectors and link them (the BoardOfDirectors class normally takes no argument on its constructor). If the constructor of BoardOfDirectors had had any arguments they could have been provided to the constructor of Company. The constructor of Board of Directors also could have been used to create both objects simultaneously.

It is worth noting that when a one-to-one association exists, there must always exist in the running system an equal number of both instances of both associated classes, and they must be linked in pairs.

Another observation is that modellers tend to over-use one-to-one associations. A superior modeling solution might be to merge the two classes; in this case just have a Company class.


 

An Executable Example Showing the Use of a One-to-One Association

// Core class of the example
// A company is a corporation
class Company {
  name;
  
  // A company must have a BoardOfDirectors
  // that must be created at the same time
  // as the Company itself
  1 -- 1 BoardOfDirectors;
}

// BoardOfDirectors is in a 1-1 relationship
// with Company meaning that theoretically it
// could just be merged into the Company class
// However to facilitate separation of concerns
// it is sometimes best to keep such classes
// separate
class BoardOfDirectors {
  // A BoardMember is just a member of one
  // board (but a person can be on multiple
  // boards using multiple BoardMembers)
  1 -- * BoardMember;
}

// A board must have a set of members
// Former board members tracked 
class BoardMember {
  bio; // Brief description of background
  lazy Date joinedBoard;
  lazy Date leftBoard;
  
  // One of the roles a person could play would
  // be as a Board member
  * boardMembership -- 1 Person;
}

// Generic Person class
class Person {
  name;
  Date dateOfBirth;
}

// Mixin with a main program demonstrating
// manipulation of 1--1 and other associations
class Company {
  depend java.sql.Date;
  
  public static void main(String [] args) {
  
    // First create some instances of Person
    // They are initially not on any boards
    Person p1 = new Person("Alice",
      Date.valueOf("1990-01-01"));
    Person p2 = new Person("Bob",
      Date.valueOf("1991-02-02"));
    
    // Create a Company using the simpler of its
    // two constructors that doesn't require a
    // board to already exist. This will actually
    // create the BoardOfDirectors at the same time
    Company c = new Company("UmpleCorp");
    
    BoardOfDirectors b = c.getBoardOfDirectors();
    
    b.addBoardMember("Largest Shareholder", p1);
    b.addBoardMember("Founder", p2);
    
    // Output the results to prove that this works
    System.out.println("Key company info: "+c);
    System.out.println("Board members: "+
      b.getBoardMembers());
    
  }
}
      

Load the above code into UmpleOnline