Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Confusion between Inversion of Control and Hollywood Principle

I am reading Head First Design pattern and just stuck on the Hollywood Principle. Earlier I read about Inversion of Control and what I understood was, it's a design principle (some also call it pattern) through which conventional flow of program changes from "Higher level module calling Lower level module" to "Lower level module calling Higher level module" (generally through abstractions), so we can have very low dependency on specific low-level module and changing low-level modules does not have any impact on our higher level or near to business modules.

But I got confused when the author said the following line regarding the Hollywood principle:-

On page 296

With the Hollywood Principle, we allow low-level components to hook themselves into a system, but the high-level components determine when they are needed, and how. In other words, the high-level components give the low-level components a “don’t call us, we’ll call you” treatment.

In the last line, it is said that The high-level components give the low-level components a “don’t call us, we’ll call you” treatment. This means our high-level components are actually calling low-level components and hence this seems to break the Inversion of control principle and also Dependency Inversion principle.

Please clarify on this.

like image 421
OldSchool Avatar asked May 04 '17 14:05

OldSchool


People also ask

What is difference between IoC and DI?

Dependency Injection is the method of providing the dependencies and Inversion of Control is the end result of Dependency Injection. IoC is a design principle where the control flow of the program is inverted. Dependency Injection is one of the subtypes of the IOC principle.

What is the main principle of Inversion of Control?

Inversion of control is a software design principle that asserts a program can benefit in terms of pluggability, testability, usability and loose coupling if the management of an application's flow is transferred to a different part of the application.

Is Inversion of Control and Dependency Inversion the same?

Inversion of Control(IoC) is also known as Dependency injection (DI). The Spring container uses Dependency Injection (DI) to manage the components that build up an application and these objects are called Spring Beans.

What is the difference between dependency injection and Dependency Inversion principle?

The Inversion of Control is a fundamental principle used by frameworks to invert the responsibilities of flow control in an application, while Dependency Injection is the pattern used to provide dependencies to an application's class.


1 Answers

When we design software we implement two things API and Framework.

An API publish some endpoint so that caller uses those endpoints to get some useful information, so the caller doesn't have any action point to take, only endpoints and outputs.

The framework takes the strategy or business implementation from the caller and calls it when required.

In Hollywood Principle, we can feed our strategy or business implementation, denoting the framework implementation, which calls the fed strategy when required.

Inversion of control and Dependency Injection is to remove dependencies of an application. This makes the system more decoupled and maintainable.

If you go back to old computer programming days, program flow used to run in its own control.

if you analyze the program flow closely, it’s sequential. The program is in control of himself. Inversion of the control means the program delegates control to someone else who will drive the flow.

like image 131
Sanjay Dwivedi Avatar answered Sep 23 '22 20:09

Sanjay Dwivedi