What is IOC?

Source: Internet
Author: User
IOC is the inversion of control, and the control is reversed. In Java Development, IOC means that the classes you have designed are handed over to the system for control, rather than being controlled within your class. This is called control inversion. Here are a few examples to illustrate what IOC is. Suppose we want to design a girl and a boy class. Girl has the kiss method, that is, girl wants to kiss a boy. So our question is, how can girl know this boy? In China, the common methods of understanding mm and GG are as follows: 1. qingmei bamboo; 2. Introduction to friends and friends; 3. Which of the following is the best solution for parents? Childhood girl knows her own boy. Public class girl {void kiss () {Boy boy = new boy () ;}} however, the disadvantage of the boy created since the beginning is that it cannot be changed. And is responsible for the entire life cycle of boy. What if our girl wants to change one? (In severe cases, it is not supported that girls often change boys, #_#) Introduction to friends and friends: The man in the middle is responsible for providing boys to meet public class girl {void kiss () {Boy boy = boyfactory. createboy () ;}} is a good introduction to friends and friends. If you are not satisfied, try another one. However, friends and friends boyfactory often appear in the form of singleton. Otherwise, it exists in globals and is everywhere. It is too tedious and inflexible. Why do I need such friends and friends to be involved? Why must I pay her a referral fee? What if my best friend falls in love with my boyfriend? Parents: give everything to parents. You don't have to worry about it. You just have to wait for Kiss. Public class girl {void kiss (boy) {// kiss boy. kiss () ;}} well, this is the best method for girl, as long as you want to bribe girl's parents and give the boy to him. Then we can easily kiss with girl. It seems that the fate of traditional parents for thousands of years is really useful. At least boys and girls don't have to worry about themselves. This is IOC, which extracts object creation and retrieval to the external. The required components are provided by external containers. 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 the dependency inversion principle is dependence inversion princinple, which is oriented to abstract programming, said dieric Gamma. Object-Oriented Programming is the core of object-oriented programming. The component should be divided into two parts: Service, the declared Implementation of the provided functions. The benefits of service implementation are that multiple implementations can be switched at will to prevent the problem of "Everything depends on everything. specifically, it depends on the specific. Therefore, our boy should implement the kissable interface. In this way, if the girl doesn't want to kiss the nasty boy, she can kiss the cute kitten and the kind grandmother. Ii. IOC type refers to the different ways in which girl gets boy. Let's explain it one by one. IOC Type 0: IOC public class girl implements servicable {private kissable; Public girl () {kissable = new boy ();} public void kissyourkissable () {kissable. kiss () ;}} girl builds her own boy, which is difficult to replace and is difficult to share with others. She can only use it independently and is responsible for the full life cycle. IOC type 1 first Code : Code public class girl implements servicable {kissable; Public void Service (servicemanager MGR) {kissable = (kissable) Mgr. lookup ("kissable");} public void kissyourkissable () {kissable. kiss () ;}} this situation occurs in aveon framework. When a component implements the servicable interface, it must implement the service method and pass in a servicemanager. It contains other required components. You only need to initialize the desired boy in the service method. In addition, the object obtained from context in J2EE also belongs to type 1. It depends on the configuration file. IOC type 2: public class girl {private kissable; Public void setkissable (kissable) {This. kissable = kissable;} public void kissyourkissable () {kissable. kiss () ;}} type 2 appears in Spring framework. It uses the Set Method of Javabean to pass the desired boy to girl. It must depend on the configuration file. IOC Type 3: Public class girl {private kissable; Public girl (kissable) {This. kissable = kissable;} public void kissyourkissable () {kissable. kiss () ;}} is the component of picocontainer. Pass boy to girl picocontainer Container = new defaultpicocontainer (); container through the constructor. registercomponentimplementation (boy. class); container. registercomponentimplementation (girl. class); girl = (girl) container. getcomponentinstance (girl. class); girl. kissyourkissable (); References 1 http://www.picocontainer.org/presentations/JavaPolis2003.ppt http://www.picocontainer.org/presentations/JavaPolis2003.pdf 2 dip, Robert c Martin, Bob uncle's excellent paper http://www.objectmentor.com/resources/articles/dip.pdf 3 dependency injection dependent injection, matrin Fowler extension http://www.martinfowler.com/articles/injection.html for dip 4 IOC framework picocontainer excellent IOC framework http://picocontainer.org/aveon http://avalon.apache.org/Spring framework http://www.springframework.org/hivemind http://jakarta.apache.org/commons/hivemind

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.