Managing Dependencies, Part 1

  • Pradyumn Sharma
  • August 15, 2017


You have a system with object-oriented design. But the complex dependencies among classes are becoming hard to manage. What to do?

When you apply the Single Responsibility Principle (“a class should have only one reason to change”) in your application, you might end up with a lot of classes in the system. The objects of these classes would need to interact with each other, in order to accomplish the desired behaviour of the system. This can lead to complex dependencies among classes. How do we deal with dependencies?

When class A uses class B, we say that class A is dependent on class B. Another term for dependency is coupling; class A becomes coupled with class B.

Are Dependencies Bad?

Yes, and no. There are drawbacks of dependencies, but there are also benefits of these. Dependencies are like cholesterol. There is good cholesterol, and there is bad cholesterol. While bad dependencies may hamper reuse, and affect the stability of a system, well-crafted dependency relationships can:

  • provide clean separation of concerns
  • make it easier to change the behaviour in a system
  • provide plug-and-play replaceability of classes or components.

Let us look at some drawbacks of dependencies. We’ll consider two classes, A and B, with class A being dependent on class B.

  • If you want to reuse class A from the current application in another application, you need to reuse class B as well, because there is a reference to class B somewhere in the source code of class A. If you don’t need any behaviour of class B in the target application, then class B just becomes extra baggage.
  • If, for some reason, you need to change the interface of class B, then you may need to change the implementation in class A (if somewhere in class A, you are invoking the function of class B that is being changed). Of course, you don’t generally go around changing the interfaces of classes, but sometimes, you may need to do so, for example, in order to fix a faulty design or incorporate changes in requirements).

But that does not mean that all dependencies are bad, and should be eliminated. You cannot eliminate dependencies from a system unless you are willing to write all the code in one single class!

So how do we work with dependencies such that we get the desired benefits, but we minimize the costs of using these?

Strong Coupling vs Weak Coupling

When a class is dependent on another class, it is called strong coupling (also tight coupling). But when a class is dependent on an interface, it is weak coupling (or loose coupling); the reason being that the class is not aware of, and therefore dependent on or coupled with, any concrete class. It is only aware of the interface type, and any concrete class providing implementation of the interface can be supplied in place of the interface.

Let us look at the following design:

In this design, the class BankLoan is only dependent on the interface InterestStrategy, but is not aware of any concrete implementation of the interface. As a result of this:

  • We can reuse the BankLoan class in some other application, if required, without necessarily reusing any of the implementations of the InterestStrategy interface.
  • New classes implementing the InterestStrategy interface can be added, without any impact on the BankLoan class.

(For a description of the complete BankLoan example, look at the post on Open-Closed Principle):

Erich Gamma, in his landmark book “Design Patterns”, presents this concept as: Program to an interface, not to an implementation.

When a class refers to another object only by its interface type, when required, the other object can even be dynamically selected or changed.

Closing Remarks

Dependencies are needed to organize our code into meaningful classes that group closely related features. This lets us model the behaviour of a complex application. However, it’s important to pay close attention and manage the dependencies well; otherwise we may have a system that is brittle and hard to change.

Using interfaces makes the design more flexible. But that is just a starting point for managing dependencies. A more involved, and related concept, is the D in SOLID Principles, “The Dependency Inversion Principle”.

We’ll look at the Dependency Inversion Principle in a follow-up to this post, very soon. Stay tuned.

16 responses to “Managing Dependencies, Part 1”

  1. What i don’t understood is actually how you are not really much more well-liked than you may be now. You’re very intelligent. You realize thus significantly relating to this subject, produced me personally consider it from a lot of varied angles. Its like women and men aren’t fascinated unless it is one thing to accomplish with Lady gaga! Your own stuffs nice. Always maintain it up!

  2. Perfectly written subject matter, regards for entropy. “The last time I saw him he was walking down Lover’s Lane holding his own hand.” by Fred Allen.

  3. blog school says:

    Its like you read my mind! You seem to know so much about this, like you wrote the book in it or something. I think that you could do with a few pics to drive the message home a bit, but other than that, this is magnificent blog. An excellent read. I’ll certainly be back.

  4. campuzhit ng says:

    Some genuinely select content on this web site , saved to bookmarks .

  5. Thanks a lot for providing individuals with an extraordinarily marvellous opportunity to read from this site. It is often very sweet and also full of a good time for me and my office co-workers to search your website no less than 3 times a week to read the fresh guides you have. Not to mention, I am also certainly happy with the outstanding tactics served by you. Selected 2 points in this posting are certainly the most suitable I have ever had.

  6. Darphin 朵法 【潔膚調理系列】清新淨藍化妝水 Refreshing Toner的商品介紹 Darphin 朵法,潔膚調理系列,清新淨藍化妝水 Refreshing Toner

  7. Wonderful work! This can be the sort of details that you should embraced on the internet.. vlogger terbaik Waste on the search engines because of not location the following upload bigger! Can occur above and speak with this web page. Many thanks Implies)

  8. 防曬粉 BB粉底霜 亮麗柔滑打底乳液 亮麗柔滑控油打底乳液 有機幹細胞修護CC霜 潤澤防曬底霜 礦物質潤澤慕斯 礦物質奇幻粉餅 礦物質蜜粉 完美礦物粉底 控油定妝蜜粉 高清控油粉餅 控油蜜 保濕滋潤噴霧 有機抗氧爽膚噴霧 有機抗敏保濕

  9. Venus Viva™對所有皮膚類型都是安全的,並使用革命性的Nano Fractional Radio Frequency™(納米點陣射頻™)和Smart Scan™(智能掃描™)技術,通過選擇性真皮加熱,從而提供優異的治療效果。使用Nano Fractional RF™將能量透過表皮傳遞至真皮,從而產生熱量,並啟動膚膚的生理機制,重建膠原蛋白及刺激纖維母細胞,最終刺激導致組織重塑。功效:✔改善膚質✔肌膚緊緻✔減淡妊娠紋✔痤瘡及暗瘡疤痕✔減淡細紋及皺紋✔面部肌膚賦活再生 適合面部及頸部

  10. Sculptra 含有由含有源於植物果酸的 PLLA 聚左乳酸(poly-L-lactic acid),它能促進天然膠原蛋白大量增生,令容顏重現年輕,一個療程的效果能持續逾2年。Sculptra 乃全球唯一獲美國食物及藥物管理局(美國FDA)認可的PLLA聚左乳酸美容品牌。Sculptra 塑然雅會被人體自然分解吸收,同時促進天然膠原蛋白再生1。療程效果可以漸進地給你更富清新朝氣的容頻,效果自然,更不會影響面部表情。SCULPTRA 塑然雅 Sculptra 由含有源於植物果酸的PLLA聚左乳酸(poly-L-lactic acid),它可以被新陳代謝成二氧化碳及水,最終自體內排出。PLLA聚左乳酸早已被廣泛應用於醫療用品超過30年,自2009年起 Sculptra 及全球唯一獲美國FDA認可的PLLA聚左乳酸美容醫學品牌。Sculptra ® 塑然雅能促進天然膠原蛋白大量增生1,改善皺紋,令容顏重現年輕。全球唯一獲美國FDA認可PLLA聚左乳酸。臨床研究證實於3個月間,肌膚的膠原蛋白密度 顯著增加67%2。療程效果能持續愈2年3。主要成份PLLA聚左乳酸源於植物果酸,醫學界廣泛使用超過30年。

  11. 自動回覆 says:


  12. Ion Magnum 是一種最新的減脂儀器,可以快速燃燒掉你身體的脂肪 , 讓你不需要做大量的體育鍛煉 , 輕鬆方便的達到專業美體的效果。 效果: 通過將你體內的脂肪轉化成肌肉,輕鬆减掉你的體重和腰圍。25分鐘的理療比健身房幾個小時的運動效果還要好! 副作用/風險: 該設備已在美國食品藥品監督管理局注册,注册分類爲醫療器械I類,I類醫療器械就是指普通人使用安全性非常好且沒有任何風險的器械。 减脂過程: 减脂板會直接固定安放於你身體的目標减肥部位,通過特殊的信號,Ion Magnum會將該部位的脂肪燃燒轉化成肌肉。(注意:該設備與你在其他沙龍中瞭解的肌肉轉化設備是完全不同的。)療程: 要想取得而保持完美的效果,一個療程需要進行10次理療,每3-7天進行一次。維持理療可以每兩周進行一次。

  13. 除皺特點 純度高,效果好 見效迅速,無創無痛 安全精準,表情自然 唯一在臨床有20多年的注射材料,全球超過11000000人使用 適用範圍 魚尾紋、額頭紋、眉間紋、鼻紋和頸部皺紋都可以祛除; 最適合於早期的、不太明顯的皺紋 不須使用任何的鎮靜劑或局部麻醉劑,且Botox除皺治療後可立即繼續進行正常的活動。 Botox除皺可以利用午休時間約診即可,完全不影響工作的安排。BOTOX肉毒桿菌素是一種高度純化的蛋白質,經過注射之後,能夠使導致動態皺紋的肌肉得到放鬆,它能夠阻斷導致肌肉收縮的神經細胞,使面部線條變得平滑並防止新皺紋形成。全球銷量第一品牌 在中國唯一獲得SFDA、FDA批准用於醫療美容的肉毒桿菌素 唯一一個擁有40年安全記錄的肉毒桿菌素 在70個國家獲得批准使用 BOTOX肉毒杆菌素

  14. pranali says:

    hello pragati

  15. pragati says:


Leave a Reply

Your email address will not be published. Required fields are marked *

© 2017 Pragati Software Pvt. Ltd. All Rights Reserved.