Through reading this short article you’ll be taking yet another big step towards familiarising yourself with Object Relations in Object Oriented Programming.
In this article you’ll be served with explanation about Composition.
To get a better grasp on today’s topic, before we go on with this article, I highly encourage you to read my article on Aggregation first. Then come back and read this article further.
As Aggregation is a specialised way of Association. Composition is considered to be a special kind of Aggregation. In a such relation a single object i.e container is always composed of one of multiple instances of other object(s).
Here is an example, think of a school which serves as a container for multiple departments. The main difference between this type of relation and other relation types, is that here objects can only exist as a parts of the main container. Below you will find some Swift code which explain Composition further.
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
}
}
class School {
private var designDepartment: DesignDepartment
private var softwareDepartment: SoftwareDepartment
init( _ designDepartment: DesignDepartment,
_ softwareDepartment: SoftwareDepartment){ //Dependency
self.designDepartment = designDepartment
self.softwareDepartment = softwareDepartment
}
func getDepartments() -> (design: DesignDepartment,
software: SoftwareDepartment){ //Dependency
return (design: self.designDepartment,
software: self.softwareDepartment)
}
}
let student = Student(name: "Joseph", lastName: "Doe", age: 25)
let teacher = Teacher(name: "Yasser", lastName: "Farahi", age: 34)
let softwareDepartment = SoftwareDepartment()
let designDepartment = DesignDepartment()
let school = School(designDepartment, softwareDepartment)
softwareDepartment.addTeacher(teacher)
designDepartment.addTeacher(teacher)
softwareDepartment.addStudent(student)
designDepartment.addStudent(student)
print(school.getDepartments().design.getTeacher().first!.name)
print(school.getDepartments().software.getStudent().first!.name)