Blog
34

Managing Dependencies, Part 1

  • Pradyumn Sharma
  • August 15, 2017

Tags:

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.

36 responses to “Managing Dependencies, Part 1”

  1. suba me says:

    DphVsv Vale Flash O PORTAL MUTIMDIA DO VALE DO PARABA

  2. suba me says:

    mkW2k2 Looking forward to reading more. Great post.Much thanks again. Really Cool.

  3. This very blog is no doubt entertaining and besides diverting. I have picked helluva helpful tips out of this source. I ad love to go back every once in a while. Thanks a bunch!

  4. I truly appreciate this blog article.Thanks Again. Want more.

  5. This is my first time pay a quick visit at here and i am genuinely pleassant to read all at one place.

  6. Wow! This could be one particular of the most beneficial blogs We have ever arrive across on this subject. Basically Excellent. I am also a specialist in this topic so I can understand your hard work.

  7. Thanks so much for the blog. Really Great.

  8. Ridiculous story there. What occurred after? Thanks!

  9. I simply could not depart your website before suggesting that I extremely enjoyed the usual information an individual provide to your visitors? Is gonna be again continuously to check out new posts.

  10. Thanks again for the blog article.Really thank you! Much obliged.

  11. This is really interesting, You are a very skilled blogger. I ave joined your feed and look forward to seeking more of your magnificent post. Also, I ave shared your website in my social networks!

  12. This very blog is without a doubt educating as well as amusing. I have picked helluva helpful tips out of this source. I ad love to come back every once in a while. Thanks a lot!

  13. Thanks for sharing, this is a fantastic article post. Really Great.

  14. pretty valuable stuff, overall I feel this is worthy of a bookmark, thanks

  15. Interesting article. Were did you got all the information from?

  16. I think other web-site proprietors should take this web site as an model, very clean and fantastic user friendly style and design, as well as the content. You are an expert in this topic!

  17. LOUIS VUITTON PURSES LOUIS VUITTON PURSES

  18. name says:

    Some truly great posts on this internet site , regards for contribution.

  19. name says:

    Wow, wonderful blog layout! How long have you been blogging for? you made blogging look easy. The overall look of your web site is wonderful, let alone the content!

  20. It as not that I want to duplicate your web-site, but I really like the style and design. Could you tell me which theme are you using? Or was it especially designed?

  21. There is apparently a bundle to know about this. I suppose you made certain good points in features also.

  22. You made some nice points there. I did a search on the subject matter and found most people will agree with your site.

  23. Say, you got a nice blog.Much thanks again. Will read on

  24. sex toys says:

    This very blog is without a doubt educating as well as informative. I have discovered helluva helpful stuff out of this amazing blog. I ad love to go back every once in a while. Cheers!

  25. sex toys says:

    The account aided me a acceptable deal. I had been

  26. lingerie says:

    Your style is unique compared to other people I ave read stuff from. Many thanks for posting when you ave got the opportunity, Guess I will just bookmark this blog.

  27. lingerie nyc says:

    Very good article.Really looking forward to read more. Really Cool.

  28. nyc lingerie says:

    Wow! This could be one particular of the most helpful blogs We have ever arrive across on this subject. Basically Magnificent. I am also a specialist in this topic so I can understand your effort.

  29. nyc lingerie says:

    Wow! Thank you! I permanently wanted to write on my site something like that. Can I implement a fragment of your post to my website?

  30. nyc sex toys says:

    Very good post. I am experiencing a few of these issues as well..

  31. wow, awesome article post.Really looking forward to read more.

  32. Muchos Gracias for your article post. Great.

  33. You ave made some decent points there. I checked on the net to learn more about the issue and found most individuals will go along with your views on this website.

  34. Dispensary says:

    Very good blog.Really thank you! Awesome.

  35. Dispensary says:

    Your style is really unique in comparison to other people I have read stuff from. Thanks for posting when you have the opportunity, Guess I will just bookmark this page.

  36. Alcohol Rehab Near Me
    drug rehab near me

Leave a Reply

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

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

Enquiry

Pragatisoftware