Skip to content
Permalink
Browse files

Fixes #126 Document 1-1 associations

  • Loading branch information...
TimLethbridge committed Oct 1, 2018
1 parent f8a4da8 commit 3923d112b016caa6524e386e0cffb5fe5bbbb223
@@ -0,0 +1,28 @@
One-to-One Associations
Associations
noreferences

@@description

<p>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 <a href="RoleNames.html">zero graduate students supervised by a professor, and zero professors might be assigned to supervise a graduate student initially.</a></p>

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

<p>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 <em>does</em> allow 1-1 associations and creates a special API to allow both objects to be created at the same time.</p>

<p>The generated code has a constructor with the arguments normally found in the constructors of <em>both</em> 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.</p>

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

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


<br />
<img width="380" src="examples/OneToOneAssociations.png" title="Class diagram generated by Umple from the following example"/>

&nbsp;<br/>

@@example @@caption An Executable Example Showing the Use of a One-to-One Association @@endcaption
@@source manualexamples/AssociationOneToOne.ump
@@endexample

@@ -0,0 +1,96 @@
// 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());

}
}//$?[End_of_model]$?

class Company
{
position 50 30 109 45;
}

class BoardOfDirectors
{
position 50 149 123 41;
position.association BoardMember__BoardOfDirectors 124,23 0,6;
}

class BoardMember
{
position 275 150 144 92;
position.association BoardMember:boardMembership__Person 30,0 29,75;
}

class Person
{
position 276 17 140 75;
}
Binary file not shown.

0 comments on commit 3923d11

Please sign in to comment.
You can’t perform that action at this time.