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).

And A Filled Diamond/ Squared Tail.
————-
Object School knows about object Department, consists of Department, and manages Departments life cycle, for instance a school can closedown an entire department.
Class University depends on Department.
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
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) |