Introduce Ioc

xiaoxiao2021-03-06  17

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 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

The benefit is: 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 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 kissyourkissable () {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 Main illustrations of this article 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 Data

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 to add some Chinese links)

This entry was posted on Wednesday, February 18th, 2004 at 12:50 am and is filed under IOC inversion of control. You can follow any responses to this entry through the RSS 2.0 feed. You can skip to the end and leave a response. Pinging is currently not allow.

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

New Post(0)