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

Association – Relations Between Objects And Classes

  • Yasser Farahi
  • May 30, 2022June 1, 2022
  • Beginner, General, OOP, Programming, Swift

To pick up where we left off our Class/ Object Relations discussion, we are going to continue with Association. 

Association

We are speaking of an Association when one object, say Object B uses or has an interaction with Object A. There may be cases where Object B interacts with Object A and vice versa. In such a scenario we are dealing with a Bidirectional Association.

Note: Association is used to represent fields (constants, variables) in a class

One Way Association On A UML Diagram
Teachers Communicates With Students

One Way Association
Teacher Object Knows About Student Object
Class Teacher Depends On Class Student

Bi-directional Two Way Association On A UML Diagram
Teachers Communicates With Students
Students Communicate With Teachers

Bi-directional Association
Teacher Object Knows About Student Object
Class Teacher Depends On Class Student

Student Object Knows About Teacher Object
Class Student Depends On Class Teacher



Disclaimer: The following code examples display combined example of Dependency and Association trying to solidify your understanding of the difference between those topics.

Pseudo Code
//Course Class
class Course is
courseTitle: String

method constructor(String courseTitle) is
this.courseTitle = courseTitle

method courseId(): String is
return this.courseTitle

//Student Class
class Student is
name: String 
lastName: String
age: Integer

method constructor(String name, String lastName, Integer age) is
this.name = name
this.latsName = lastName
this.age = age

method knows(Course course): String is //Student has a Dependency with class Course
print(course.courseId())

//Teacher Class
class Teacher is

//class Teacher has an association with class Student
//A Teacher object knows about Student Objects
//class Professor depends on class Student
private students: Student //Association

method examen(Course course) is //Professor has a Dependency with class Course
this.student.knows(course)

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
import Foundation
 
class Course {
 
    private var courseTitle: String
 
    init( _ courseTitle: String) {
        self.courseTitle = courseTitle
    }
    
    func courseId() -> String {
        return courseTitle
    }
}
 
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
    }
 
    func knows( _ course: Course) { //Dependency
        print(course.courseId())
    }
    
}
// Teacher ––––––––> Student
class Teacher {
    //class Teacher has an association with class Student
    //A Teacher object knows about Student Objects
    //class Professor depends on class Student
    
    private var student: Student //Association
 
    init( _ student: Student) {
        self.student = student
    }
    
    func examen( _ course: Course ) { //Dependency
        self.student.knows(course)
    }
}
 
 
let newStudent = Student(name: "Joseph", lastName: "Doe", age: 24)
let teacher = Teacher(newStudent)
let course = Course("Algebra")
teacher.examen(course) //Algebra

Tags: Association Basics Data Structure Object Relations Software Engineering

Post navigation

Previous Post
Next Post

Categories

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

New Posts

  • Composition – Relations Between Objects And Classes
    June 13, 2022
  • Aggregation – Relations Between Objects And Classes
    June 8, 2022
  • Association – Relations Between Objects And Classes
    May 30, 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