Introduction IOC
Author: ice clouds icecloud (AT) sina.comBLOG: http://icecloud.51.net Time: 2004.02.15
Copyright Notice:
This article is completed by ice cloud, started 9CBS, the author retains Chinese copyright. No commercial use is not permitted without permission. Welcome to the reprint, but please keep the article and copyright statement complete. For contact, please send an email: Icecloud (at) sina.com
First, what is IOC
IOC is Inversion of Control, controlled reversal. In Java development, IOC means handing over the class to the system to control, not in your class internal control. This is called control reversal.
Below we will explain what is IOC in several examples.
Suppose we want to design a Girl and a BOY class, where Girl has Kiss method, which girl wants to kiss a BOY. So, our question is how Girl can meet this BOY?
In our China, the common method of common MM and GG has the following
1 青 梅竹马; 2 relatives and friends introduction; 3 parents package
So, which one is the best?
Qingmeizhu Ma: Girl knows his BOY from a child.
Public class girl {void kiss () {boy boy = new boy ();}}
However, the BOY disadvantage from the beginning of the start is not available. And to be responsible for the entire life cycle of BOY. What if we want to change one? (The author does not support Girl frequently change BOY)
Friends and friends introduction: The middleman is responsible for providing BOY to meet
Public class girl {void kiss () {boy boy = boyfactory.createBoy ();}}
Friends and friends introduction, although it is good. If you are not satisfied, even if you change it. However, relatives and friends Boyfactory often appears in the form of Singleton, otherwise, existing in Globals, everywhere, everywhere. It is too cumbersome, not flexible enough. Why do I have to blend this relatives and friends? Why do you have to pay her introduction? What if the best friend falls in love with my boyfriend?
Parents Package: Everything is given to parents, they don't have to expense the power, just wait KISS.
Public class girl {void kiss (boy boy) {// kiss boy boy.kiss ();}}
Well, this is the best way to girl, as long as you want to bribe Girl's parents and give him BOY. Then we can easily come to Kiss and Girl. It seems that it is really useful for thousands of years of traditional parents. At least Boy and Girl don't need to be busy.
This is IOC, extracts the object's creation and acquisition to the outside. The required components are provided by the external container.
We know Hollywood Principles: "Do Not Call US, We will call you." Means, you, girlie, do not call the boy. We will feed you a boy.
We should also know that dependent Inversion Princinple, DIP.
Eric Gamma said that it is necessary to face abstract programming. Interface programming is an object-oriented core.
Components should be divided into two parts, namely
SERVICE, the function of the function
Implementation, service is the benefit of: Multi-implementation can be switched to prevent "Everything Depends on Everything" problem. That is, it depends specifically.
So, our BOY should be a kissable interface. This way Girl doesn't want KISS to be evil BOY, you can also kiss cute kitten and kind grandmother.
Second, IOC TYPE
IOC's Type refers to several different ways of Girl get BOY. Let's explain one by one.
IOC TYPE 0: No IOC
Public class girl imports servicable {private kissable kissable; public girl () {kissable = new boy ();} public void kissyourkissable () {kissable.kiss ();}}
Girl builds your own BOY, it is difficult to replace, it is difficult to share it with others, and can only be used alone and are responsible for the full life cycle.
IOC TYPE 1, first look at the code:
public class Girl implements Servicable {Kissable kissable; public void service (ServiceManager mgr) {kissable = (Kissable) mgr.lookup ( "kissable");} public void kissYourKissable () {kissable.kiss ();}}
This situation appears in Avalon Framework. A component implements the Servicable interface, you must implement the service method and passed into a serviceManager. It will contain other components required. It is only necessary to initialize the required BOY in the service method.
In addition, the object from context from context is also TYPE 1 in J2EE.
It relies on configuration files
IOC TYPE 2:
Public class girl {private kissable kissable; public void setkissable (kissable kissable) {this.kissable = kissable;} public void kissyourkissable () {kissable.kiss ();}}
Type 2 appears in Spring Framework, is passed to Girl to Girl through the SET method of JavaBean. It must rely on profiles.