[FITYMI] A Dip in the Dependency Inversion Principle – SOLID OOP

By this time you should have already landed that job you were interviewing for. You should have also (hopefully) realized that this series is not just about faking interviews, but about learning. Learning does not stop after you graduate from school. It does not stop after getting a job. Your boss will surely squeeze the most out of you, so you better make sure you’re up to snuff. It’s time to get trippy with the Dependency Inversion Principle and complete the L. S. D. of SOLID OOP.

In recent times I’ve noticed a disturbing trend in Filipino shows: the prevalence of love teams. There was the era of Marvin and Jolina, followed by John Lloyd and Bea, Kathryn and Daniel, Liza and Enrique, and finally James and Nadine. But love teams are popular in international media as well, though to a far lesser extent. You have your Brad and Angelina, Taylor and Tom, and Sheri Vi and Nikolas. Aside from the last two, though, international couples have achieved success as individuals – they can work with whomever they want and projects generally turn out well and without any turmoil.

This is not the case with Filipino love teams, though. I don’t know which came first, the rabid fans, or the media cultivating and exploiting the rabid potential of fans, but what’s certain is splitting up these love teams can explode as violently as splitting the nucleus of an atom. Fans will feel betrayed, the media will sensationalize this news, and it will be a bigger story than extrajudicial killings or government corruption. In the end, trying to split them up normally leads to disaster. You might say they’ve become tightly coupled.

That right there is a computer science analogy for you.

The problem with these love teams, as with many computer programs, is that they are very specific in how they should behave and who they should be working with. Imagine for a minute that James Reid is a class and his manager is the programmer. When his manager was just starting out with James, he tried fitting him in a program with Nadine and found out that they worked well together. Because of this success, he just focused on using James with Nadine over and over. Whenever he made more movies, did mall tours, and TV appearances with James, it was always with Nadine. You can say now that James and Nadine have become dependent upon one another, which is all well and good if nothing ever changes in the industry.

What would happen if one day, a super-billionaire comes in and demands that a movie be made starring James as the leading man, with Kathryn as his leading lady? He tells the manager “I’ll give you 12 billion pesos if you can make a successful movie with James and Kathryn as the leads. The film must be made in absolute secrecy, with no publicity whatsoever. Upon completion of the film, you will have one week to promote it. You’ll get the money only if the film becomes so successful that JaDine gets replaced by JaKat – or KatAmes if you prefer – as the top love team in the Philippines.”

As you can imagine, this would be a very tough venture for our dear manager. JaDine fans will be furious. Not only that, but KathNiel fans will not sit idly by. They will defend their territory – with virtual blood if need be. Social media will explode, the news will fan the flames, an all-out war will be waged between JaDine and KathNiel fans, and the movie will probably not fly given its suddenness.

[SIDE NOTE] In this case the suddenness is very important because based on the behavior of Filipino viewers, given a long enough time such a controversy would most likely produce a big box office hit.

 If you think about it, a good actor should perform well enough no matter who he acts with. James should not be tied to Nadine and only Nadine, otherwise you may get into nasty situations. This is the same in object-oriented programming.

What could the manager have done to avoid this? Well, he should have made sure that James was built well-enough such that he can work with any actor/actress. It would have taken the manager more effort to package James in this manner and still make sure he is viable in the box office, but the effort will pay off if he needs James to work with other people.

Breaking it down further, instead of saying that James has to work with Nadine, you can say that James has to work with someone who can act. In the same manner, Nadine should be able to work with someone who can act, instead of just James.

“Someone who can act” is what we can refer to as an abstraction. James and Nadine are actual implementations of that abstraction. This illustrates the first point of the Dependency Inversion Principle.

High-level modules should not depend on low-level modules. Both should depend on abstractions.

Just to make it clear, I am not saying that James is the high-level module and Nadine is the low-level one. In my opinion they can both bow down to the greatness of Marvin and Jolina, and that is not the point of this illustration. The key takeaway is that they should not be tied down so tightly with each other that they could not work with anyone else who can act.

[SIDE NOTE] Whether or not James or Nadine could act still remains to be seen. Literally. I haven’t seen any of their work so I have no idea if they really do have acting chops or if their success is just a result of the clever fabrication and marketing of their love team.

The abstraction “someone who can act” – let’s call it ICanAct from now on – can be represented as an interface in the object-oriented programming world. An interface defines what something can do, not what it is or how it does things. Here are some examples of things that can act.

As you can see, many different things can act. Aside from James, Nadine and Kathryn, you can include my dog and my butt in that list – and I really couldn’t say which among those can act the best. That does not matter, though. How my butt acts does not matter as well. All that matters is, if implemented correctly, you can make a hit movie with James and my butt because all you require are things that can act. This leads to the second point of the Dependency Inversion Principle.

Abstractions should not depend on the details. Details should depend on the abstractions.

If you really want to put this into practice but you find it hard to remember, for me the following proves sufficient for most cases:

Program to an interface, not to an implementation.

If you want to remember it easily, just look at this:

DIP

Advertisements

3 thoughts on “[FITYMI] A Dip in the Dependency Inversion Principle – SOLID OOP

  1. Pingback: [FITYMI] Interface Segregation Principle (ISP) and a Purely Hypothetical Social Commentary | Error in Compilation

  2. Pingback: [FITYMI] Open and Closed Principle – Okay, now I have to Refactor | Error in Compilation

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s