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

Dependency – Relations Between Objects And Classes

  • Yasser Farahi
  • May 27, 2022May 27, 2022
  • Beginner, General, OOP, Programming, Swift

Recently I came to know how little many beginner software developers know about Class and Object Relations in Object Oriented Programming. That’s why, I decided to start writing short and manageable articles in regards to Relations Between Objects And Classes.

During my upcoming blog posts, I’ll be focusing on topics such as Dependency, Association, Aggregation, Composition, Implementation and Inheritance.

Starting with Dependency.

#1: What is Dependency?

We are speaking of a Dependency when changes in the definition of class A results to modifications in class B. A dependency between objects occurs when using concrete classes in another class.
For example, when instantiating objects via a constructor method, or specifying input types in method and function signatures.

Dependency on a UML (Unified Modelling Language) diagram

Note: class B can be modified by changes in class A.
A dependency is considered to be the most basic and weakest relations between objects.

Pseudo Code

class Registration is 
method signup( _: User): User is

Registration class has a dependency to class User, because its method uses User as input and as a return type. 

Note: We can make a dependency even weaker, if we depend our code on Interfaces (protocols ) instead of concrete classes.

interface UserInterface is
name: String
userName: String
password: String

class User implements UserInterface is
name: String
userName: String
password: String

method constructor(String name, String userName, String password) is
this.name = name
this.userName = userName
this.password = password

class Registration is
method signup( _: UserInterface): UserInterface is

Notice how Registration class now has a weaker dependency to the User class because now it uses an interface / protocol .

Swift Code – Dependency Using Concrete Class Name

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import Foundation
 
class User {
    
    let name: String
    let userName: String
    let password: String
    
    init( name: String, userName: String, password: String) {
        self.name = name
        self.userName = userName
        self.password = password
    }
    
}
 
class Registration {
    
    func signup( _ user: User ) -> User {
        let user = user
        return user
    }
    
}

Swift Code – Weaker Dependency Through Using An Interface / Protocol

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
import Foundation
 
//Defines a blue print
//for any class that implements this interface
//Multiple class can implement this interface
protocol UserInterface {
    var name: String { get set }
    var userName: String { get set }
    var password: String { get set }
}
 
//User class implements UserInterface Protocol
class User: UserInterface {
    var name: String
    var userName: String
    var password: String
 
    init( name: String, userName: String, password: String) {
        self.name = name
        self.userName = userName
        self.password = password
    }
    
}
 
//Registration class has a weaker dependency
//To User class
 
class Registration {
    func signup( _ user: UserInterface ) -> UserInterface {
        let user = user
        return user
    }
}
 
let user = User(name: "Robert", userName: "rob", password: "yusiyd87434")
let registration = Registration()
let output = registration.signup(user)
print(output.userName) //rob

 

Tags: Basics Classes Dependency Interfaces Object Relations OOP

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