Clean code
  • Overview
    • Introduction
    • Why is clean code so important?
    • What Is Clean Code?
    • How to write clean code?
    • Conventions
  • The key principles of clean code
    • Meaningful Names
    • Functions
    • Classes
    • Comments
    • Error Handling
      • Exception handling best practices
    • Unit Tests
    • Formatting
    • Objects and Data Structures
    • Simple Design Rules
    • Concurrency
    • Code Smells
  • Building Maintainable Software
    • Write Short Units of Code
    • Write Simple Units of Code
    • Write Code Once
    • Keep Unit Interfaces Small
    • Write Clean Code
    • Automate Tests
  • Bonus
    • SOLID Principle
      • SRP - Single Responsibility Principle
      • OCP - Open-Closed Principle
      • LSP - Liskov Substitution Principle
      • ISP - Interface Segregation Principle
      • DIP - Dependency Inversion Principle
    • LoD Principle
    • YAGNI Principle
    • DRY Principle
    • Fail Fast principle
    • Hollywood Principle
    • Library vs Framework
    • Coupling and Cohesion
    • AOP - Aspect-Oriented Programming
      • Building an AOP framework
    • OOP Design Pattern
    • Technical Dept
    • How to learn software Design and Architecture - Roadmap
    • Microservcies
      • Defining the scope of a microservice
      • Step-by-Step: How to Identify Over-Scoped Microservices
      • Benefits of Grouping or Consolidating Microservices
      • A practical step-by-step plan to consolidate microservice
Powered by GitBook
On this page
  • What's Hollywood principle?
  • Real time Example
  • Use Case
  • References
  1. Bonus

Hollywood Principle

Don't Call Us, We'll Call You.

PreviousFail Fast principleNextLibrary vs Framework

Last updated 2 years ago

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.

What's Hollywood principle?

The Hollywood Principle states, "Don't Call Us, We'll Call You." It's closely related to the , and illustrates a different way of writing software from the more traditional form of programming in which one's own code dictates the flow of control. When following the Hollywood Principle, code is written to respond to external events, such as from an existing framework.

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

API

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.

Framework

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.

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).

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.

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.

Use Case

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.

Cognizant says: Don’t call me, I will call you.

Let see the implementation.

Assumption

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.

public class Resume {
    private String email;
    private String name;
    private String content;

    /* getters and setters */

    @Override
    public String toString() {
        return "Resume [email=" + email + ", name=" + name + ", content=" + content + "]";
    }
}

public class CognizantJobPortal {
    private static CognizantJobPortal portal = new CognizantJobPortal();

    private List<Resume> resumeList = new ArrayList<Resume>();

    public static CognizantJobPortal get(){
        return portal;
    }

    private CognizantJobPortal() {}

    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());
        }
    }
}

public class HollywoodTest {
    public static void main(String[] args) {
        CognizantJobPortal.get().uploadContent("shamik@xyz.com", "Shamik Mitra", "A java developer");
        CognizantJobPortal.get().uploadContent("Ajanta@vvv.com", "Ajanta Mitra", "A PHP developer");
        CognizantJobPortal.get().uploadContent("Swastika@vvv.com", "Swastika Mitra", "A Microservice developer");
        CognizantJobPortal.get().uploadContent("Mayukh@vvv.com", "Mayukh Mitra", "A Network engineer");
        CognizantJobPortal.get().uploadContent("Samir@123.com", "Samir Mitra", "A java Architect");   
        
        // Now trigger campusing
        CognizantJobPortal.get().triggerCampusing();
    }
}

Output

Sending mail to Shamik Mitra at shamik@xyz.com
Sending mail to Ajanta Mitra at Ajanta@vvv.com
Sending mail to Swastika Mitra at Swastika@vvv.com
Sending mail to Mayukh Mitra at Mayukh@vvv.com
Sending mail to Samir Mitra at Samir@123.com

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.

Read more: Library vs Framework

References

Dependency Inversion Principle
https://dzone.com/articles/the-hollywood-principle