Java deep adventure (4) in-depth introduction ----- IOC AND AOP

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 (container) for control and implementation, rather than internal control within your class. This is called control inversion.


I understand that the object you created and used is now made by someone else and managed by Constructors (containers ), the setter method or method (the method of this object here) parameter is passed to the user, and is used by the user.



Instance Description Image Understanding IOC


To design a Girl and a Boy class, Girl has the recognize method, that is, Girl wants to recognize a Boy. So our question is, how can Girl know this Boy?


Common Methods of understanding MM and GG are as follows:

1. qingmei bamboo; 2. Introduction from friends and friends; 3. Which one is the best?



Childhood Girl knows her own Boy.



<Span style = "font-family: Microsoft YaHei;"> <span style = "font-size: 18px;"> <span style = "font-size: 18px; "> <span style =" font-size: 18px; "> code: public class Girl {void recognize () {Boy boy = new Boy ();}} </span>

However, the disadvantage of creating a Boy from the beginning is that it cannot be changed. And is responsible for the entire life cycle of Boy. What if a Girl wants to change one?

Disadvantages: the first method is interface injection, which requires that our class must implement an interface given by the container, and then the container will use this interface to inject the class it depends on to us.



Introduction to friends and friends: the intermediary is responsible for providing boys to meet

<Span style = "font-family: Microsoft YaHei;"> <span style = "font-size: 18px;"> <span style = "font-size: 18px; "> <span style =" font-size: 18px; "> code: public class Girl {void recognize () {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? Wait? </Span>



Parents: give everything to parents. You don't have to pay for it. You just have to wait.Recognize.


<Span style = "font-size: 18px;"> <span style = "font-family: Microsoft YaHei;"> <span style = "font-size: 18px; "> <span style =" font-size: 18px; "> <span style =" font-size: 18px; "> code: public class Girl {void recognize (Boy boy) {// <span style = "font-family: Microsoft YaHei;"> recognize </span> boy. <span style = "font-family: Microsoft YaHei;"> recognize </span> ();}} </span>

Well, this is the best method for Girl, as long as you want to bribe her parents and give the Boy to him. Then we can easily recognize with Girl. 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.


It is very simple, that is, what you should do is that you do not let the system do it. For example, when you get an object, you often need to create a new instance. If you use control inversion, you do not need to do this. You only need to configure it in the configuration file xml, and the system will help you with new features. Control inversion is also called dependency injection, it is to inject the things used in advance. It will be used directly next time instead of every new

IoC, in the vernacular, is the relationship between control programs by containers, rather than in non-traditional implementations, directly controlled by program code. This is the concept of "control reversal": control is transferred from application code to external containers, and control is transferred.


Instance USB interface Example


A laptop is connected to a peripheral storage device through a pre-specified interface (USB). For a laptop, only user-specified data is sent to the USB interface, it is determined by the USB device currently connected. Before the USB device is loaded, it is impossible for the notebook to predict which device the user will connect to on the USB interface. Only after the USB device is connected will the dependency between the devices become.
Corresponding to the above description about the dependency injection mechanism, at runtime (system boot, USB device loading) by the container (running in the Windows operating system in The Notebook) inject dependencies (laptops rely on USB devices for data access) into components (Windows File Access Components ).


Personal Understanding:


In the traditional mode, classes are called directly, so there is a strong coupling degree, the dependency between programs is strong, and there are many problems involved in later maintenance.
IOC uses a configuration file (XML) to describe the relationship between classes, which is managed by containers, reducing the coupling between programs, program modification can be implemented through simple configuration file modification.


Aspect-oriented programming (AOP)


Aspect Oriented Programming (for Aspect-Oriented Programming ), you can use the pre-compilation method and the runtime dynamic proxy to dynamically and uniformly add functions to the program without modifying the source code (learn from the example ).


Summary

These concepts were first introduced when the teacher gave eight lectures. They accumulated, searched for materials, learned in depth, summed up, and listened to the teacher's lessons, A great increase in thinking affects your way of thinking and work, and influences and changes ......


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.