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

Aggregation – Relations Between Objects And Classes

  • Yasser Farahi
  • June 8, 2022June 8, 2022
  • Beginner, General, OOP, Programming, Swift

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.

Aggregation On A UML Diagram Is Shown With A Simple Arrow In front
And An Empty Diamond/ Squared Tail.

Object Department Knows About Object Teacher
Object Department Knows About Object Student
———-
Class Department Consists Of Teacher Object(s)
Class Department Consists Of Student Object(s)

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

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
101
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)
Tags: Aggregation Association Basics Composition Dependency Implementation Inheritance Object Relations OOP Swift

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