In this article we are going to continue our journey on getting a better grasp about Object Relations in Object Oriented Programming.
Let’s explore Aggregation.
We are speaking of an Aggregation when an Object is served as a container for a collection of other objects. For example, Imagine how a school department is served as a container for one or many teachers. At the same time, the very same school department can also be served as a container for one or many students.
The previous example helps us to get a better grasp on the following assumption:
Aggregation represents one to many or many to many relationships between objects.
However, in an Aggregation relationship objects can exist without their container object, for example a teacher and a student can exist without a specific school department. Keep in mind, objects of a container can be linked to multiple containers at the same time, for instance a teacher object can be an object of multiple departments.
Pseudo Code
//Student Class
class Student is
name: String
lastName: String
age: Integer
method constructor(String name, String lastName, Integer age) is
this.name = name
this.lastName = lastName
this.age = age
//Teacher Class
class Teacher is
name: String
lastName: String
age: Integer
method constructor(String name, String lastName, Integer age) is
this.name = name
this.lastName = lastName
this.age = age
//Software Department Class
class SoftwareDepartment is
private teachers: Array [Teacher]
private students: Array [Student]
method addTeacher(Teacher teacher) is
this.teachers.add(teacher)
method addStudent(Student student) is
this.students.add(student)
method getTeachers(): [Teacher] is
return this.teachers
method getStudents(): [Students] is
return this.students
//Design Department Class
class DesignDepartment is
private teachers: Array [Teacher]
private students: Array [Student]
method addTeacher(Teacher teacher) is
this.teachers.add(teacher)
method addStudent(Student student) is
this.students.add(student)
method getTeachers(): [Teacher] is
return this.teachers
method getStudents(): [Students] is
return this.students
Swift Code
import Foundation
class Student {
let name: String
let lastName: String
let age: Int
init(name: String, lastName: String, age: Int) {
self.name = name
self.lastName = lastName
self.age = age
}
}
class Teacher {
let name: String
let lastName: String
let age: Int
init(name: String, lastName: String, age: Int) {
self.name = name
self.lastName = lastName
self.age = age
}
}
class SoftwareDepartment {
private var teachers: [Teacher] = []
private var students: [Student] = []
func addTeacher( _ teacher: Teacher) {
self.teachers.append(teacher)
}
func addStudent( _ student: Student) {
self.students.append(student)
}
func getStudent() -> [Student] {
return self.students
}
func getTeacher() -> [Teacher] {
return self.teachers
}
}
class DesignDepartment {
private var teachers: [Teacher] = []
private var students: [Student] = []
func addTeacher( _ teacher: Teacher) {
self.teachers.append(teacher)
}
func addStudent( _ student: Student) {
self.students.append(student)
}
func getStudent() -> [Student] {
return self.students
}
func getTeacher() -> [Teacher] {
return self.teachers
}
}
//Object can exist without the container
let student = Student(name: "Joseph", lastName: "Doe", age: 25)
let teacher = Teacher(name: "Yasser", lastName: "Farahi", age: 34)
//Objects can be linked to several containers at the same time
let softwareDepartment = SoftwareDepartment()
let designDepartment = DesignDepartment()
//Adding Teacher to several departments
softwareDepartment.addTeacher(teacher)
designDepartment.addTeacher(teacher)
//Adding Student to several departments
softwareDepartment.addStudent(student)
designDepartment.addStudent(student)
print(softwareDepartment.getTeacher().first!.name,
softwareDepartment.getTeacher().first!.lastName,
softwareDepartment.getTeacher().first!.age)
print(designDepartment.getTeacher().first!.name,
designDepartment.getTeacher().first!.lastName,
designDepartment.getTeacher().first!.age)
print(softwareDepartment.getStudent().first!.name,
softwareDepartment.getStudent().first!.lastName,
softwareDepartment.getStudent().first!.age)
print(designDepartment.getStudent().first!.name,
designDepartment.getStudent().first!.lastName,
designDepartment.getStudent().first!.age)