Introduce Ioc

xiaoxiao2021-03-06  107

Introduce Ioc

IOC is a kind of thinking in recent years, not just programming ideas.

It is often said that the Hollywood Principle "Don't call us, we'll call you" is this manifestation, reversing control

In the programming, the effect of reversing control often does not think about it. In particular, the development of Framework or Container will give flow or processing to Server will greatly reduce programmers' development time.

Here is the article I published in 9cbs: Introduction IOC

Introduction IOC

Author: ice clouds icecloud (AT) sina.com Time: 2004.02.15 First, what is the IOC IoC is Inversion of Control, inversion of control. 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 how IOC assumes that IOC assumes that we want to design a Girl and a BOY class, where Girl has Kiss method, which is a BOY of KISS. So, our question is how Girl can meet this BOY? In our China, the common method of common MM and GG has the following one of the following 1 of Qingmeizhu Ma; 2 relatives and friends introduction; 3 parents package, which 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 replace BOY) relatives 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 implementation of the function of the function, the implementation of the implementation of the service: 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's Type IoC's Type refers to Girl gets a few different ways of BOY. Let's explain one by one. IOC TYPE 0: No IOCPUBLIC CLASS GIRL IMPLEMENTS 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.

IOC TYPE 3

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. The author: Well, these theoretical parts of the above have some new definitions. 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! References 1 This paper main illustrations and text originated from Jon Tirsén from ThoughtWorks 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/presentations/javapolis2003.pdf 2 DIP, Robert C Martin, Bob Uncle's excellent papers http: //www.objector. 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 frame http: // picoContainer. Org / avarache.org/ Spring framework http://www.springframework.org/ hivemind http://jakarta.apache.org/commons/hivemind 5 Chinese Data Somed: Domestic research PICO pioneer http://douleimi.vicp.net/space/start jdon: 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 (February 20 Supplementary Chinese Link) Published by Icecloud on February 18, 2004 00:50

转载请注明原文地址:https://www.9cbs.com/read-103790.html

New Post(0)