Mapping associations with and without attributes using: references (reference collections) association arrays
Outline Definition of an association Association symbols Mapping simple associations to Java Multiplicity Reference collections Associative arrays Exercises
Definition Association defines a relationship between classes of objects which allows one object instance to cause another to perform an action on its behalf.
Association symbols Associations in UML class diagrams - thin line connecting two classes The label are highly recommended (one or two words describing the association). For example „works_for” The black triangle is the reading direction. PersonCompany works_for 1 1..*
Association One-To-One class City { String name; public City(String name) { this.name = name; } class Country { City cap_city; public City Polska(String cap) { cap_city = new City(cap); return cap_city; }
Unidirectional Association class Person { Person p; BDate bdate; String name; public Person(String name, BDate bdate) { this.bdate=bdate; this.name = name; } class BDate { int month; int date; public BDate(int month, int date) { this.month = month; this.date = date; }
Association to itself public class Person { Person manager; Collection employees = new Vector(); public Person(Person o) { manager = m; } public void addEmplyees(Person p) { employees.add(p); }
Association with attributes class Person{ String personName; public Person(String name) { this.personName=name; } class Company{ String companyName; public Company(String name) { this.companyName=name; } public static class Employment{ Person employee; Company employer; int sal; public Employment(Person p, Company c, int salary) { employee = p; employer=c; sal = salary; }
Multiplicity The multiplicity of a binary association is placed at the end, specifies the number of target instances that may be associated with a single source instance across the given association CompanyEmployee 11..*
Multiplicity Multiplicity indicators 1 1, 2, 3,... 2, 3, 4,... 3, 4, 5 2, 4, 18 1, ? 0, 1 0, 1, 2, * 2..* 3-5 2,4, * * UMLmeanings
Array reference - collections Collection is any group of ordered items that has one or more properties in common. You can reference a single string inside an array by specifying the number of the reference that you want to extract in square brackets.
Association One-To-Two public class Bicycle { Wheel[] w = new Wheel[2]; public Bicycle(Wheel[] w) { if (w.length==2) System.out.println(„1 bicycle has 2 wheels”); } public class Wheel { public Wheel() {} }
Association One-To-Many public class Person { Collection cars = new Vector(); public Person() {} public void addCar(Car c) { c.assignOwner(this); cars.add(c); } public class Car { Person p; public Car(Person p) { this.p = p; } public void assignOwner(Person p) { this.p = p; }
Association Many-To-Many class Person { public Vector collectionCompany = new Vector(); String name; public Person(String name) { this.name = name; } void addCompany(Company c){ collectionCompany.add(c); } class Company { String name; public Company(String name) { this.name = name; } public Vector collectionEmplyees = new Vector(); void addEmployees(Person employee){ collectionEmplyees.add(employee); }
Associative Array An associative array is a data structure that stores pairs of keys and values. A value may be inserted or retrieved based on its key.
Associative Array public static void main(String[]args){ Company company = new Company("NewTech"); Person employee1 = new Person("Person A"); Person employee2 = new Person("Person B"); Hashtable companyEmployees = new Hashtable(); Vector employeesCollection = new Vector(); employeesCollection.add(employee1); employeesCollection.add(employee2); companyEmployees.put(company.companyName,employeesCollection); }
Associative Array public static void main(String args[]) { Division division1 = new Division("Division 1"); Division division2 = new Division("Division 2"); Person employee1 = new Person("Jan Nowak"); Person employee2 = new Person("Beata Kowalska"); Hashtable divisionEmployee = new Hashtable(); Hashtable employeeDivision= new Hashtable(); Vector employeeVec = new Vector(); Vector divisionVec = new Vector(); employeeVec.add(employee1); employeeVec.add(employee2); divisionVec.add(division1); divisionVec.add(division2); divisionEmployee.put(division1.nameDiv, employeeVec); employeeDivision.put(employee2.namePerson, divisionVec); }
Exercise 1 A language school asked you to create an IT system that stores information about its students, the language centers they are attending and courses that they take. Each course may have maximum 6 students. One student must have at lease one course. Create associations between the classes and implement it.
Exercise 2 A music store asked you to make an system that reports on CD sales. Create class diagrams, assisiations and implement the assosiation.
Exercise 3 A big insurance company needs a system to store information about their signed contracts and clients. A client may have a husband or a wife.
The End INCLUDED FILES OneToOne.java Directional.java Attribute.java OneToTwo.java OneToMany.java ManyToMany.java AssociativeArray.java AssociativeArray2.java Questions?