Hi, When I look at something like Drupal, WordPress, and many PHP web frameworks, I see hooks and modules. In Java I see lots on Inversion of Control and Dependency Injection. I understand what these things are, particularly the latter. But what I don't get is how they are different. Are there any examples of a program with "hooks" in some very small understandable framework or maybe some example somewhere? Are they interchangeable in Java? In PHP I see them as almost the same because of the weak typing in the language (I know the newest has type hinting, interfaces, etc.). I have yet to understand it in Python implementation. Java I got.
I want to build a small web framework that uses hooks. I can do it with DI. I see that as something like I might want to switch out my security interface (ISecurity, maybe). Someone may want LDAP, some a built in system in a database, flatfile, etc. Are hooks more events like before x is rendered, after some other event. Thanks for any comments. I'd be using Java 7, some container, JSP and EL JSTL, or maybe something else. I have no idea at the moment. I only respectfully request not to tell me not to build my own framework. I will eventually move to something else more than likely, but I'd like to do on my own and understand things. Plus it is way more fun.
John Chumley wrote:I want to build a small web framework that uses hooks. I can do it with DI. I see that as something like I might want to switch out my security interface (ISecurity, maybe). Someone may want LDAP, some a built in system in a database, flatfile, etc. Are hooks more events like before x is rendered, after some other event. Thanks for any comments.
Simple answer: Dunno, because I have no idea what a "hook" is in this context; and it sounds like something very specific. If you'd care to provide a description, or a link, that would help.
I also reckon that we may be able to move this thread to a more targeted forum; but at the moment, I'm not quite sure which one that would be.
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Hooks, inversion of control and dependency injection are not exactly the same thing, but there is some relation between these terms.
When you write a simple program, then often your program has a main loop somewhere, that for example does something like this: (1) wait for input, (2) process the input and produce output, (3) go back to step 1. The main loop is the main path of control of what the program does.
Inversion of control means that you don't program the main loop yourself, but that you instead let some existing software framework do that for you. You give the framework one or more callback methods (hooks), and the framework will call them whenever some event happens. It's called "inversion of control" because the main control loop is now in a framework or library instead of in your own code - instead of your own code calling library methods, you let the library call your code (hence, "inversion").
Depencency injection is slightly different. When you write a program, it often consists of different components working together. To make the program work, you need to connect these components together. The simple way to do this is, is to just instantiate the component that you need using the 'new' operator. For example, suppose that your program needs to interact with a database. You could create a class that does all the database interaction, and in your main program class you create a new instance of this database class, and you call methods on it to work with the database.
The problem with that approach is that you tightly couple the main program class to the database class. Suppose that at some point you decide that you want to use some other database, for which you create a second implementation of the database class. You would need to modify the main program too if you want to use the other database class.
With dependency injection you let some framework handle connecting the components of the program together. If you use the Spring framework, for example, you can use annotations, something like this:
You then tell the Spring framework to resolve the dependencies, and it will automatically initialize in the variable 'database' in MainProgram with a new instance of OracleDatabase.
The coupling between MainProgram and Database is now not so tight, and you can easily let Spring insert something else than an OracleDatabase by configuring Spring differently. For example, for testing, you can insert a mock database object instead of the real one.