iOS Development Made Simple!
  • Home
  • Blog
  • Contact
  • Enroll Now
  • Enroll Now

Composition – Relations Between Objects And Classes

  • Yasser Farahi
  • June 13, 2022June 13, 2022
  • Beginner, Courses, Dart, General, Interview Challenges, OOP, Programming, Swift

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

Composition On A UML Diagram Is Shown With A Simple Arrow In front 
And A Filled Diamond/ Squared Tail.
————-
Object School knows about object Department, con­sists of Department, and man­ages 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 rela­tion and oth­er relation types, is that here objects can only exist as a parts of the main con­tain­er. 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)
Tags: Association Basics Composition Dependency Object Relations OOP Software Engineering Swift Xcode

Post navigation

Previous Post
Next Post

Categories

  • Algorithms
  • Beginner
  • Courses
  • Dart
  • General
  • Interview Challenges
  • OOP
  • Programming
  • Swift

New Posts

  • iOS Development Xcode Layout And Auto Layout Tools
    August 2, 2022
  • Composition – Relations Between Objects And Classes
    June 13, 2022
  • Aggregation – Relations Between Objects And Classes
    June 8, 2022

Tags

Aggregation algoritmes Association Basics Caesar Cipher Classes Composition Data Structure Dependency Implementation Inheritance Interfaces Interview Questions O(1) Runtime O(2n) Runtime Object Relations OOP Power Of N Reverse Values Shifting Characters Shifting Values Software Engineering String Manipulation Swift Value Swapping Xcode
© Copyright RISING GALAXY. All Rights Reserved | Read Our Privacy Policy  - Terms Of Use - Cookies Policy
Cookies Policy
We use cookies on our website to give you the most relevant experience by remembering your preferences and repeat visits. By clicking “Accept”, you consent to the use of ALL the cookies.
Do not sell my personal information.
Cookie SettingsAccept
Manage consent

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary
Always Enabled
Necessary cookies are absolutely essential for the website to function properly. These cookies ensure basic functionalities and security features of the website, anonymously.
CookieDurationDescription
cookielawinfo-checkbox-analytics11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Analytics".
cookielawinfo-checkbox-functional11 monthsThe cookie is set by GDPR cookie consent to record the user consent for the cookies in the category "Functional".
cookielawinfo-checkbox-necessary11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookies is used to store the user consent for the cookies in the category "Necessary".
cookielawinfo-checkbox-others11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Other.
cookielawinfo-checkbox-performance11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Performance".
viewed_cookie_policy11 monthsThe cookie is set by the GDPR Cookie Consent plugin and is used to store whether or not user has consented to the use of cookies. It does not store any personal data.
Functional
Functional cookies help to perform certain functionalities like sharing the content of the website on social media platforms, collect feedbacks, and other third-party features.
Performance
Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.
Analytics
Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics the number of visitors, bounce rate, traffic source, etc.
Others
Other uncategorized cookies are those that are being analyzed and have not been classified into a category as yet.
SAVE & ACCEPT