Java factory method mode 4

Source: Internet
Author: User

3.2 factory method mode and IoC/DI

IoC -- Inversion of Control Inversion
DI -- Dependency Injection

1: How to understand IoC/DI
To understand the two concepts above, you must clarify the following issues:

  • Who are the participants?
  • Dependency: who depends on who? Why is dependency required?
  • Injection: who injects the data? What is injection?
  • Control reversal: Who controls who? What is the control? Why is it "reverse )?
  • Is dependency injection the same as control reversal?

    Next we will give a brief answer to the above questions. After understanding these questions, we will understand IoC/DI.
    (1) Who are the participants:

    Generally, there are third-party participants. One is an object, the other is an IoC/DI container, and the other is an external resource of an object.
    The term also explains that an object refers to any and common Java object. The IoC/DI container simply refers to a framework program used to implement the IoC/DI function; the external resource of an object refers to what the object needs, but is obtained from outside the object. All resources are collectively referred to as resources, such as other objects required by the object or file resources required by the object.

    (2) who depends on who:

    Of course, an object depends on the IoC/DI container.
    (3) Why dependency:

    The IoC/DI container is required for the object to provide the external resources required by the object.
    (4) who injects the data:

    Obviously, the IoC/DI container injects an object
    (5) What exactly is injected:

    Is the external resources required to inject an object.
    (6) who controls who:

    Of course, the IoC/DI container controls the object.
    (7) What to control:

    It mainly controls the creation of object instances.
    (8) Why is reverse:

    Reverse is relative to positive, so what is positive? What do you do if you want to use C in A conventional application? Of course, it is to directly create the C object, that is, actively obtain the required external resource C in Class A, which is called positive. So what is reverse? That is, Class A does not take the initiative to obtain C, but passively waits, waits for the IoC/DI container to obtain a c instance, and then injects it into Class A in reverse direction.
    In the example below, when IoC/DI is not available, the conventional Class A uses Class C, as shown in 7:


    Figure 7 General A uses C <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + primary + cq + o7o8L3A + CjxwPgo8aW1nIHNyYz0 = "http://www.bkjia.com/uploadfile/Collfiles/20140113/20140113100453221.gif" alt = "\">
    Figure 8 Class A no longer actively creates Class C

    Instead, wait passively, wait for the IoC/DI container to obtain a c instance, and then inject it to class A in reverse order, as shown in 9:


    Figure 9 program structure after IoC/DI container

    (9) is dependency injection the same concept as control reversal?
    According to the above description, we can see that dependency injection and control inversion are different descriptions of the same thing. In a certain aspect, they have different descriptions. Dependency injection is described from the application perspective. Dependency injection can be described as a complete point: the application depends on the container to create and inject the external resources required by it; the control inversion is described from the container perspective. The complete description is: The container controls the application, and the container reversely injects the external resources required by the application to the application.
    (10) summary:

    In fact, the biggest change that IoC/DI brings to programming is not from the code, but from the ideological point of view. The application was originally the boss and took the initiative to obtain any resources, but in the IoC/DI idea, the application became passive, passively wait for the IoC/DI container to create and inject the required resources.
    Such a small change is actually a major improvement in programming ideas. In this way, the objects and the external resources they need are effectively separated, making them loosely coupled and conducive to function reuse, more importantly, the entire architecture of the program becomes very flexible.

    2: What is the relationship between the factory method model and IoC/DI?

    From a certain point of view, they have similar ideas.
    As mentioned above, with IoC/DI, the application will no longer take the initiative, but passively wait for the container to inject resources. When writing code, once external resources are used, A window will be opened to allow containers to be injected, that is, the injection path provided for containers. Of course, this is not our focus, so we will not discuss it in detail, use setter injection as an example to see what the IoC/DI code looks like. The sample code is as follows:

    Public class {

    /**

    * Waiting for Injection

    */

    Private C c = null;

    /**

    * Method for injecting resource C

    * @ Param c the injected Resource

    */

    Public void setC (C c ){

    This. c = c;

    }

    Public void t1 (){

    // Here C is required, but C is not allowed to be created. What should I do?

    // Injection from the outside is required anyway, which is more worry-free,

    // You don't have to worry about how to get C, just use it directly.

    C. tc ();

    }

    }

    The sample code of Interface C is as follows:

    Public interface C {

    Public void tc ();

    }

    From the sample code above, we can see that when writing code in A, whenever an external resource is required, the injection method is provided, which requires injection from the outside, you only need to use these objects.
    Let's take a look at the factory method mode and how to implement the same functions above. to distinguish between them, the names are A1 and C1. In this case, C1 objects should be used in A1, instead of A1 taking the initiative to obtain C1 objects. Instead, a factory method should be created, which is similar to an injection method. Then, the class, for example, A2 is used to obtain the C1 object. when calling the C1 object, replace the corresponding method of A1, which is equivalent to reverse injection and returning to A1. The sample code is as follows:

    Public abstract class A1 {

    /**

    * The Factory method is used to create C1, which is similar to the method used to inject data from child classes.

    * @ Return C1 object instance

    */

    Protected abstract C1 createC1 ();

    Public void t1 (){

    // C1 class is required here, but I don't know which one is used

    // It will not take the initiative to create C1. What should I do?

    // It will be implemented in the subclass anyway. You don't have to worry about how to obtain C1 here, just use it directly.

    CreateC1 (). tc ();

    }

    }

    The sample code of the subclass is as follows:

    Public class A2 extends A1 {

    Protected C1 createC1 (){

    // Select the actual implementation and create an object

    Return new C2 ();

    }

    }

    The C1 interface is the same as the previous C interface, and the C2 implementation class is empty. It is just a demonstration, so it will not show their code.
    By carefully understanding the above examples and comparing their implementations, especially from the ideological level, we will find that the factory method mode is similar to the IoC/DI idea and is "active and passive ", the "master-slave transposition" is implemented to obtain a more flexible program structure.

    To be continued ......


    The original content of the Private School Online Learning Network follows the grinding design mode of the cc design series.

    Original content. For more information, see [http://sishuok.com/forum/blogpost/list/88.html]

    ---------------------------------------------------------------------------

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.