In Hollywood, they say, ”Don’t call us, we’ll call you.” This is equally applicable to programming. Let’s set up callback methods through Inversion of Control.
The Hollywood Principle says:
This little sentence opens up a new viewpoint in software. This is one of the important principles every developer should know. In this article, we will try to discuss it.
In layman terms, you don’ t have to bother about when your turns come. When your turns come, they will call you.
But what do “you” and “they” denote here?
To explain “you” and “they” in technical terms, first we need to understand how software design works. When we design a software, we try to implement two things.
API
Framework
An API is used to publish some methods/functions, so the caller/user of the API calls this method to get some useful information. So, the caller does not have any action points to take — only call methods and outputs.
The framework is a little bit more critical than the API. The framework is maintaining an algorithm, but it expects the value to be produced by the caller of the framework. To put it simply, the framework takes the strategy or business implementation from the caller and calls it when required.
With the Hollywood Principle, we can feed our strategy or business implementation, denoting the framework engine/implementation, which calls the fed strategy when required.
Say Cognizant has a resume upload portal where job seekers upload their resumes. When the company does some on-campus recruiting, Cognizant will call them by sending an email to their inbox.
We can implement the same thing via the Hollywood principle. Job seekers upload their resumes in the job portal, and Cognizant sends mail to them when an on-campus event occurs.
Let see the implementation.
Here, I have not introduced any interface for the sake of simplicity, and I’ve not added any complex scenario like a priority, reset, GroupWise mail sending, etc., as I just wanted to show how the Hollywood Principle works.
Please note that in the CognizantJobPortal class, I maintain a list where uploaded resumes are added. When Cognizant triggers a campusing, the job portal/framework sends the mail to all job seekers who uploaded a CV to Cognizant.