简体   繁体   中英

I don't understand the Hollywood Principle

sorry for my English.

I stumble upon this principle when reading the "Head First Design Patterns" book. I see other peoples ask on StackOverFlow the same question and a few articles but I still don't understand it.

Can someone explain it in the simplest term for me?

Most of the example I've read is assuming that the reader already uses a framework supporting IoC like Spring. I never use those frameworks before because I'm still a beginner.

Thank you.

It's been a while but due to the number of viewers, i explain it.

The Hollywood Principle which sometimes referred to as Inversion of Control says "Don't call us, we'll call you" Which in terms of software engineering it states that an object which depends on another one should not call it but should wait for the other to call it.

This principle defines the IOC with more abstraction. As you know IoC defines inversing the control of dependencies. So first of all you should have a control center responsible for controlling dependencies ie for making instance of them. This control center then inverses the flow of making instance of classes. So when you want to use ClassA in ClassB , you won't create instance of ClassB then Call ClassA . You make instance of ClassA and pass it to ClassB using different techniques and use it.

For more information about IoC have some invest on the internet as i just explained some basic concepts.

Hollywood Principle - What does it mean?

It means that high level modules should call the low level modules.

By following this principle, our code tends to be loosely coupled. Loosely coupled code means that we don't have unnecessary references to other classes or objects. It helps in maintainability of the software.

Implementation

Hollywood Principle can easily be implemented using events or callbacks.

Observer Pattern is a well known example that implements Hollywood Principle.

Another design pattern which makes use of Hollywood Principle is Template Pattern. In Template Pattern, the super class defines the flow of control. The sub classes then implements abstract methods, or plug-in the code.

Frameworks are also well known example of Hollywood Principle. In frameworks, we inject our code. We either subclass or plug-in our classes, which are called by frameworks to complete the job.

Example

Suppose we are creating a word processor application. We will have two classes, Document and Page .

Here, Document is high level module and Page is a low level module. This is because a Document can have one or more Page .

If we want to re-render our page, Page should not call Document object. For any change in Document , it should call Page to re-render it. Document can notify Page objects to re-render using events or callbacks.

Also, if we follow Dependency Inversion Principle here, Document should not depend on Page directly, but using some abstractions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM