Wednesday, November 26, 2014

In this case, we must actively opt in code which body (


Inversion of Control (IoC) is actually an umbrella term for a variety of patterns. These patterns tai chi chuan often occur in the framework. The main point is that it is not the custom application code that controls the program flow without being handled by the framework. IoC is also a bit informally known as the "Hollywood Principle" - "do not call us, We'll call you."
Traditionally, in imperative programming languages have the application code explicitly controlled program flow. The program flow is defined tai chi chuan by the application code controls the entire tai chi chuan flow that is either custom code or calling the library code. Library code can invoke another library code, but typically never own code. Inversion of Control occurs then when the library code calls the code. Examples of IoC Event-driven programming (event-driven programming). A main loop defines a clear flow in which one's own application code can be connected. (Eg Swing) JUnit. When you write a test case according JUnit 3.8+, you create a test class that inherits framework class Test Case. You develop one or more test methods test something and testing something else. The framework provides setup - and teardown methods tai chi chuan in which you can put initiation and uppstädningskod. But it is not you who run the test without this making framework. IoC and dependency
The definition tai chi chuan of IoC above is much broader than what one nowadays mean when we use the concept of IoC. Usually we talk about the outside via a container, manage the creation of components and their dependencies.
Traditionally, the creation of components and their dependencies tai chi chuan have occurred in the actual components as in this example where the class Foo create an instance of type Bar and also executes the logical program flow through method executeSomething. 01: public class Foo {02: public void doSomething () {03: Bar bar = new BarImpl (); 04: bar.executeSomething (); 05:} 06:}
The purpose of using IoC to manage the creation of components are to: Minimize the "glue code" that is, code that only exists tai chi chuan to "glue together" application. Creating components in a uniform and controlled manner. Managing Dependencies externally so that they need not be known by the actual Java code. Improving testability. By creating objects handled externally so can implementations of different tai chi chuan objects easily replaced when testing using mock objects. Forcing a good application design. Dependency lookup
Dependency lookup is a form of IoC that has long existed and been used in many different ways. A factory or a register (factory / Registry) provides an application to connect a variety of items / components without exposing too much about how these objects stuck together or what dependencies the constituent objects have. Instead of spreading this "create code" throughout the application you can create modularized. The client invokes simply a method on a factory to obtain a complete paired and configured items. Client code has only dependent of the factory and the object being created.
There are drawbacks to using factories which one is that you must write their Default PIN. It may seem trivial but it will be many classes that are written in vain and there is always a risk of error. If we look at a typical use of a factory. Bar ins = (Bar) MyObjectFactory.getInstance ("myBar");
It is worth noting that we have a hard reference to the factory, which is a bit disappointing. To suede factory is difficult tai chi chuan when we use a static method. Changing implementing class in their test cases can be different difficulty depending on how you implemented the factory. Have we done the factory a little clever so that the tex choose which class it should instantiate based on an XML file, you can change the implementation without its own factory implementation for testing.
In this case, we must actively opt in code which body ("myBar") we want (and correct spelling), maybe check that it is not null, and to beware of any ClassCastExceptions. Although the complexity of this is not so great, it would be much better tai chi chuan if we did it to avoid possible errors and boiler tai chi chuan plate code. Anyone who has done JNDI lookups can note that the pattern tai chi chuan is the same, both are examples of Dependency Pull, a variant of Dependency Lookup.
Another case in point of how to do before DI's use of factory methods tai chi chuan in Singleton pattern (also known under the synonym Evil singleton pattern). One of the main problems in the use of this pattern is the poor testability due to static methods which are difficult to replace during the test. Dependency Injection
With DI write less code and there is almost no hard dependencies to the frame. When it comes to Spring can be simplistic to say that the framework already provides all the factory and lookup code you need.
DI container tai chi chuan (eg Spring) will want to inject instances either in a constructor or getter / setter metode

No comments:

Post a Comment