What is IoC control inversion?

Source: Internet
Author: User
The use of static classes is a controversial topic. some people even advocate not to use the scope qualifier on the class name. The debate on static features focuses on a design principle called IoC control inversion. The IoC design principle tries to remove all dependencies in object-oriented programming. This principle is very important for complex systems. It makes objects have better polymorphism and encapsulation. The less interdependent, the easier it is to test a component separately.

The use of static classes is a controversial topic. some people even advocate not to use the scope qualifier on the class name. The debate on static features focuses on a design principle called IoC control inversion.

The IoC design principle tries to remove all dependencies in object-oriented programming. This principle is very important for complex systems. It makes objects have better polymorphism and encapsulation. The less interdependent, the easier it is to test a component separately.

The problem between static classes and IoC lies in the static access feature. in essence, this feature defines the binding relationship between two classes because the class name is hard-coded. This means that this class is not easy to simulate when testing a class separately.

That is to say, the use of static classes will limit the IoC design principles. This is because the use of static classes will cause the classes to be bound together by names, which makes it more difficult to test a component separately.

Introduction to IoC concepts

The IOC mode (DI: Dependency Injection) is the Inversion of Control. In Java development, IoC intends to give the class you designed to the system for control, rather than internal control in your class. This is called control inversion.

IoC (Inversion of Control) is an idea that has emerged in recent years. it is not just a programming idea. It mainly coordinates the dependencies between components, greatly improves the portability of components, and increases the chances of component reuse. In the traditional implementation, the internal code of the program controls the relationship between programs. We often use the new keyword to realize the combination of key-to-key relationships between two groups. this implementation will lead to coupling between components (a good design not only requires code reuse, also, decouple the relationship between components ). IoC solves this problem, and manages the relationship between components from external containers mentioned in the program. That is to say, the container dynamically injects a dependency between components into the component at runtime. The relationship between control programs is implemented by external containers. That is to say, the Hollywood Principle "Don't call us, we'll call you ".

Ioc, also known as DI (Dependecy Injection dependent Injection), is proposed by Martin Fowler's Inversion of Control Containers and the Dependency Injection pattern.

Separation of attention (Separation of Concerns: SOC) is the most primitive motivation for Ioc mode and AOP generation. through functional decomposition, you can get attention. these Concerns can be component Components, Aspects, or service Services.

From the GOF Design model, we are used to a way of thinking programming: Interface Driven Design Interface-Driven, Interface-Driven has many advantages and can provide different flexible subclass implementations, the code stability and robustness are increased, but the interface must be implemented, that is, the following statement will be executed sooner or later:

AInterface a = new AInterfaceImp();

AInterfaceImp is a subclass of the AInterface interface. the Ioc mode can delay the implementation of the interface. it can be implemented as needed. there is a metaphor: the interface is like an empty model set. when necessary, in order to become a model entity, we need to inject plaster into the model. Therefore, we turn the implementation of human control interfaces into "injection ".

Ioc is an Inversion of Control, that is, the reverse mode. here is the famous Hollywood theory: You stay and don't move, I will look for you later.

In fact, the Ioc mode also solves the relationship between the caller and the called. the preceding AInterface implementation statement indicates that the called AInterfaceImp is currently called. because the called name is written into the caller's code, this leads to the original sin of interface implementation: mutual connection, the caller and the called are closely related, and Dependency is used in UML.

However, this Dependency is intolerable in the separation of concerns. it must be cut to decouple callers and callers. the new Ioc mode Dependency Injection mode is generated, the Dependency Injection mode refers to Dependency Injection, that is, the Dependency is stripped first, and then injected as appropriate.

An IoC example

Suppose we want to design a Girl and a Boy class, where Girl has a kiss method, that is, Girl wants to Kiss a Boy. So our question is, how can Girl know this Boy?

In China, common methods of understanding MM and GG are as follows: 1. Introduction to friends and friends; 3. arrangement by parents

Which one is the best?

Childhood Girl knows her own Boy.

public class Girl {      void kiss(){       Boy boy = new Boy();    }}

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 our Girl wants to change one? (I seriously do not support frequent replacement of Boy by Girl)

Introduction to friends and friends: a man-in-the-middle is responsible for providing boys to meet.

public class Girl {    void kiss(){       Boy boy = BoyFactory.createBoy();          }}

It is good to introduce 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 boy){       // kiss boy        boy.kiss();    }}

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

Address of this article: http://www.nowamagic.net/librarys/veda/detail/393,welcome to the original release.

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.