Back to Asp.net series-basics (6)

Source: Internet
Author: User

 

With the development of Object-Oriented Analysis and Design, a good design, one of the core principles is to isolate changes, so that when the change part changes, the unchanged part is not affected (this is also the purpose of OCP ). To achieve this, we need to use polymorphism in the object-oriented model. After polymorphism is used, the customer class no longer directly depends on the service class, but on an abstract interface, the customer class cannot directly instantiate a specific service class internally.

However, the customer class objectively needs a specific service class to provide services during operation, because the interface cannot be instantiated to provide services. There is a conflict between "the customer class is not allowed to instantiate a specific service class" and "the customer class needs a specific service class.

Dependency Injection is a process in which a customer class only depends on an interface of a service class and does not depend on a specific service class. Therefore, the customer class only defines one Injection point. In the process of running the program, the customer class does not directly instantiate a specific service class instance, but is responsible for instantiating the service class by the running Context Environment or specialized components of the customer class, and then injecting it into the customer class, ensure the normal operation of the customer class.

Setter Injection refers to setting a data member of the Service class interface type in the customer class and setting a Set method as the Injection point, this Set method accepts a specific service class instance as a parameter and assigns it to a data member of the Service class interface type. Constructor Injection refers to setting a data member of the Service class interface type in the customer class and taking the Constructor as the Injection point, this constructor accepts a specific service class instance as a parameter and assigns it to a data member of the Service class interface type.

The injection methods mentioned above are customer-Dependent Services passively accepted, which is also in line with the word "injection. However, there is another way to achieve the same purpose as dependency injection, that is, dependency retrieval.

As you can see, this method becomes passive and active, so that the customer class can actively obtain the service class as needed, and encapsulate the implementation of polymorphism into the get point. There are many implementations for getting points. Perhaps the easiest thing to think of is to create a Simple Factory as the getting point. The customer class passes in a specified string to get the corresponding service class instance. If the dependent service class is a series of classes, the dependency acquisition usually uses the Abstract Factory mode to construct the acquisition point, and then transfers the service class polymorphism to the Factory polymorphism, the factory type depends on an external configuration, such as an XML file.

However, whether using Simple Factory or Abstract Factory, you cannot determine the service type or Factory type. In this way, there is always a place in the system that does not conform to the OCP if... Else Or switch... Case structure, which cannot be eliminated by Simple Factory, Abstract Factory, and dependency acquisition. In some languages that support reflection (such as C #), this problem is completely solved by introducing the reflection mechanism.

 

Dependency Locate refers to an interface that provides a collection point in the system and the customer class still depends on the service class. When a customer class needs a service class, It proactively retrieves the specified service class from the get point. The specific service class type is determined by the configuration of the get point.

 


The author stays up late

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.