The Hollywood Principle
Hollywood.jpg

Hollywood Principle says:  “Don’t call us, we’ll call you”

This little sentence opens up a new viewpoint in Software industries. This is one of the important principles every developer should know.

In this article, we will try to discuss this principle.

“Don’t call us, we’ll call you” what does it mean?

In layman terms,  you don’t have to bother about when your turns come, when your turns come they will call you.

But What you and they denote here?

To explain “you” and “they” in technical terms first we need to understand How a Software design works.

When we design a software we try to implements two things.

  1. API
  2. Framework.
  1. API:  API used to publish some methods/functions so the caller/user of the API call this method to get some useful information. So caller does not have any action points to take only call methods and get output.
  2. Framework: The Framework is a little bit critical than API. The framework is maintaining an algorithm but it expects the value to be produced by the caller of the Framework. To put it simple way, Framework takes Strategy or Business implementation from the caller and call it when it requires.

By Hollywood principle, we can make Framework works, where you means the Strategy or business implementation, which needs to be fed, and they denote framework engine/implementation which calls fed strategy when required.

Real time Example:

Spring DI: Think about Spring where we declare beans in XML, and Spring container call these beans create Spring beans, inject other beans into it and returns fully configured bean.  So by the help of XML, we feed the strategy and Spring container calls them when required. We often called it Dependency Injection so Hollywood Principle’s another name is IOC(Inversion of Control).

Struts 1.x: Pay attention to Struts 1.x implementation where caller of Struts extends ActionClass and provide  Business implementations in the Action class and Struts framework call those Action class based on the URL mentioned in Struts config file. So here Action class is Strategy and Struts framework invokes it.

Observer pattern/Listener in Swing:  Think about Swing’s actionListener we subscribe to an event like button click, on Blur etc and when this event occurs Swing call our code written in the actionPerformed method.
Apart from this Servlet / EJB all maintains lifecycles so underlying server calls appropriate life cycle method when the servlet or EJB state changed like init, service, destroy or ejbActivate ejbPassivate etc.

We call those methods as Callback methods because Framework call this method we don’t have to call them but we may provide the implementations of those methods if we want to push some strategies in Framework.

Let see a use case where we can use Hollywood principle

Say Cognizant has a Resume upload portal where job seeker uploads their resumes. When there is an On campusing happens in Cognizant they will call them by sending a mail to their inbox.

We can implement the same by Hollywood principle,
Jobseeker uploads their resumes in Job portal and Cognizant send mail to them when an on campusing occurs.
cognizant says : Don’t call me I will call you. 🙂Hollywood (2).jpg

Let see the implementation.

Assumption : Here I have not introduced any interface for sake of simplicity and not add any complex scenario like a priority, reset, GroupWise mail sending etc. as I just wanted to show How Hollywood principle works.
Resume.java

/**
*
*/
package com.example.hollywood;

/**
* @author Shamik Mitra
*
*/
public class Resume {
    private String email;
    private String name;
    private String content;
    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getContent() {
        return content;
    }
    public void setContent(String content) {
        this.content = content;
    }
    @Override
    public String toString() {
        return “Resume [email=” + email + “, name=” + name + “, content=” + content + “]”;
    }
   
   

}
CognizantJobPortal.java

/**
*
*/
package com.example.hollywood;

import java.util.ArrayList;
import java.util.List;

/**
* @author Shamik Mitra
*
*/
public class CognizantJobPortal {
   
    private static CognizantJobPortal portal = new CognizantJobPortal();
   
    public static CognizantJobPortal get(){
        return portal;
    }
   
    private CognizantJobPortal(){
       
    }
   
   
    private List<Resume> resumeList = new ArrayList<Resume>();
   
    public void uploadContent(String mail ,String name,String content)
    {
        Resume resume = new Resume();
        resume.setName(name);
        resume.setEmail(mail);
        resume.setContent(content);
        resumeList.add(resume);
    }

   
    public void triggercampusing(){
        for(Resume resume : resumeList){
            System.out.println(“Sending mail to ” + resume.getName() + ” at ” + resume.getEmail());
        }
    }
}
Test

/**
*
*/
package com.example.hollywood;

/**
* @author Shamik Mitra
*
*/
public class HollywoodTest {
   
    public static void main(String[] args) {
       
        CognizantJobPortal.get().uploadContent(“[email protected]”, “Shamik Mitra”, “A java developer”);
        CognizantJobPortal.get().uploadContent(“[email protected]”, “Ajanta Mitra”, “A PHP developer”);
        CognizantJobPortal.get().uploadContent(“[email protected]”, “Swastika Mitra”, “A Microservice developer”);
        CognizantJobPortal.get().uploadContent(“[email protected]”, “Mayukh Mitra”, “A Network engineer”);
        CognizantJobPortal.get().uploadContent(“[email protected]”, “Samir Mitra”, “A java Architect”);   
        // Now trigger campusing
        CognizantJobPortal.get().triggercampusing();
    }

}

Output:

Sending mail to Shamik Mitra at [email protected]
Sending mail to Ajanta Mitra at [email protected]
Sending mail to Swastika Mitra at [email protected]
Sending mail to Mayukh Mitra at [email protected]
Sending mail to Samir Mitra at [email protected]
Please note that in CognizantJobPortal class I maintain a list where uploaded resumes are added. When cognizant triggers a campusing, job portal/framework send the mail to all jobseekers who uploaded the CV to Cognizant.

Source link

The Hollywood Principle

Leave a Reply

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