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.
Public class girl {private kissable kissable; public girl (kissable kissable) {this.kissable = kissable;} public void kissable () {kissable.kiss ();}}
This is the component of the PicoContainer. Pass the BOY to Girl by constructor.
PicoContainer container = new DefaultPicoContainer (); container.registerComponentImplementation (Boy.class); container.registerComponentImplementation (Girl.class); Girl girl = (Girl) container.getComponentInstance (Girl.class); girl.kissYourKissable ();
About PicoContainer, the authors will be detailed in detail.
Author language:
Well, these theoretical parts of these theories have been newly defined. I will write some articles in detail. For example, the original three Type structures have now been redefined as many levels of relying on injections.
IOC is very young and growing. Along with the development of IOC, AOP, COP, SOP, etc. are constantly developing. As a programmer, it is a very relaxing thing to pay attention to the development of new ideas. Does anyone want to explore my work together with me!
Reference
1 The main illustrations of this article are from ThoughtWorks Jon Tirsén and Aslak Hellesøy (two developers of PicoContainer), 2003 in Java Polis speech PPT. There is a deletion.
http://www.picocontainer.org/pesentations/javapolis2003.ppt
Http://www.picocontainer.org/presentation/javapolis2003.pdf
2 DIP, Robert C Martin, Bob Uncle Excellent Paper
Http://www.objectmentor.com/resources/articles/dip.pdf
3 Dependency Injection Dependency, Matrin Fowler's extension of DIP
http://www.martinfowler.com/articles/injection.html
4 Ioc Framework
PicoContainer excellent IOC framework
http://picocontainer.org/
Avalon
http://avalon.apache.org/
Spring Framework
http://www.springframework.org/
Hivemind
Http://jakarta.apache.org/commons/hivemind
5 Chinese information
Programmaker: Domestic research PICO pioneer
http://douleimi.vicp.net/space/startjdon: Bankey is also studying
http://www.jdon.com/design.htm
Spring Framework Chinese Forum
http://xglw.51.net/5team/springframework/index.php
Avalon Chinese information http://www.huihoo.org/apache/avalon/introduction.html erproad http://www.erproad.org/index.asp?vt=bycat&cat_id=37 open heart http://blogbus.com/ Blogbus / blog / index.php? blogId = 2529 & cat = 5