Introduction IOC

zhaozj2021-02-16  66

First, what is IOC IOC is Inversion of Control, and the control 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 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.

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

Code: 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.

Code: 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 Dependence Inversion Princinple, DIP Eric Gamma, to be 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 IOC Code: Public Class Girl Implements Servicable {Private Kissable Kissable; Public Girl () {Kissable = New Boy ();} PUBLIC VOID KIISSYOURKISSABLE () {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:

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

Code:

IOC TYPE 2:

Code: 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. Code:

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

New Post(0)