How to achieve loose coupling IOC

Original: https://www.cnblogs.com/talentbuilder/archive/2010/04/25/1719646.html    

This blog is about the IOC (Inverse of control) and DI (Dependency Injection), the IOC and DI theme is to discuss how to help us build a loosely coupled software architecture.

    Question: tight coupling

    Before we understand the specific meaning of the IOC and DI, let us first understand the problem under tight coupling. First of all, consider this scenario: There is a ClsCustomer class that contains an address (type ClsAddress) objects, this design will lead to a coupling between classes. It can be said ClsCustomer this class depend address this object, if ClsAddress this class has changed, it will lead to ClsCustomer be recompiled. As shown below:

 

Figure 1: tightly coupled

    To summarize the above design Why would tightly coupled:
   (1) The first, and biggest problem, ClsCustomer class controls the creation process address this subject;

   (2) Objects of this class ClsAddress directly referenced object ClsCustomer this class, which leads to coupling between the class and the ClsCustomer ClsAddress class.

   (3) ClsCustomer this type of guidance to ClsAddress this type of creation, so if we add a new address type, for example: ClsHomeAddress, ClsOfficeAddress, which will lead to changes ClsCustomer this class.

    So, it is seen, when the object creation process ClsAddress there is a problem, then ClsCustomer this class constructor will fail during initialization, this mutual containment is coupled harm, which led to the expansion of the program can be reduced.


Solution:
    We understand the shortcomings of tightly coupled, we learn how to solve the case, in fact, the theory is very simple, to create a control object is about to be separated from ClsAddress ClsCustomer class, controlled by a third party, that is to say if we can control reversal to a third party, then we will be able to solve the problem. Solution below will introduce is the IOC (Inversion of control), that is, inversion of control.

 

IOC principle:
    the basic principles outlined in the IOC is Hollywood principle, namely Do not call us we will call you . With more vivid words of class ClsAddress would like ClsCustomer said: "Do not create me, I will create their own elsewhere."

    Here to emphasize the important role of the interface, the interface defines a kind of code of conduct, which is equivalent class list of features, it offers the advantage of hiding the details of the classes, interfaces to reduce coupling and improve the scalability of the program has a very important role . For this example, if the main class of the other classes of polymerization, that it should not have been achieved by direct polymerization class itself, but should be polymerized class interface, i.e. ClsCustomer the interface should be based polymerization IAddress ClsAddress this class.

Figure 2.IOC Framework architecture

    The figure shows the method for coupling understanding can be seen that the process of creating the process was entrusted to ClsAddress IOC Framework, such a design creates a separate process from ClsCustomer ClsAddress in order to achieve a decoupling of ClsAddress and ClsCustomer, decoupled Separable : two-step
    (1) IOC framework created ClsAddress objects
    (2) IOC framework ClsCustomer object reference to the class.

Way to achieve the IOC:
    after we understand the problem and solution ideas, I will introduce several IOC solutions, due to the IOC by DI (Dependency injection), namely dependency injection to achieve. So I will focus on several ways dependency injection.

Figure 3.IOC between the organization and DI

The figure above shows how DI between the IOC and the organization, so we can think IOC is a theory, and DI is the way to achieve the IOC. There are four ways in DI we realized:
    constructor method
    is injected through getter and setter
    implement the interface
    service locator
in the following section where we will focus on these methods through hands-on.

(1) constructor method
    using such a method, reference will be passed through ClsAddress ClsCustomer constructor, ClsAddress when the object is created, it will form a reference parameter of the incoming ClsCustomer constructor. This method is simple, but not suitable in the case of default constructor is often called. Briefly describes the implementation of this method in the figure, which is ClsAddress IAddress interface:

4. FIG constructor method implemented DI

(2) getter and setter
    of this method is the most common, and we often use, is through the get / set methods will depend on the object reference assignment. However, this method has the disadvantage that it destroys the principles of encapsulation in object-oriented programming, an object used as a get / set methods, internal details of the object exposed, so that the package is not very good, the code of the method of FIG was achieved :

FIG 5.get / set-implemented method DI

(3)以接口为基础的DI
    在这种方法中,IOC Framework中包含了一个接口,如下图所示,即其中的IAddressDI,接口定义了一个方法setAddress,而ClsCustomer实现了这个接口,所以这样外部进行依赖注入时只用调用接口IAddressDI的setAddress方法就可以了,这屏蔽了ClsCustomer内部的细节,使得封装性更好了。具体思想参照下图:

图6.接口为基础的DI

(4)service locator
通过service locator,IAddress的引用将由service locator获得,如下图所示。service locator类并没有创建ClsAddress实例,它仅仅是提供了注册和寻找services的方法,而services创建了ClsAddress实例。具体示例请参照下一章节关于widsor框架的部分。

图7:service locator的使用

 

为什么我们不使用DI Factory呢?
首先想象,我们是不是可以通过工厂实现以上的内容呢?但这存在了一些问题,由单独的类工厂类处理所有创建对象的过程,这显得过于复杂了,下边是对工厂类局限的一些解释:
    (1)硬编码:工厂类的最大问题是,它不能在应用程序之间进行重用,其中的内容都是硬编码,可复用性不高,而且随着类的增多工厂将变得复杂。
    (2)接口依赖性:工厂类的基础是建立在通用接口的基础上的,即其创建的对象都要实现各自的通用接口,接口实现了对象创建(create)过程和实现(implement)过程解藕。但由此也引入了一些问题,由于所有的类都要实现通用的接口,但这对于特殊的类来说又是一个限制。
    (3)所有的内容都是编译时确定的:工厂中,对象间的依赖关系必须在编译时确定下来,这极大限制了程序的灵活性。

 

Windsor框架

在这篇文章最后我将介绍一个非常有用的框架:Windsor
Windsor是一个IOC的开源项目,它的使用非常简单,它可以看做是service locator的一个应用,下面的图8到图10显示了Windsor使用的方法,在图11中介绍了使用配置文件实现DI的方法。在如下图所示编写完IAddress接口、ClsAdrress类以及ClsCustomer类后,在主类中首先创建Windsor container对象,第二步和第三步分别是在容器中注册类型(接口)和对象,第四步请求container创建customer对象,在这一步中container创建了ClsAdrress类的对象并通过ClsCustomer的构造方法实现了ClsCustomer对象的注入操作,最后一步释放了Customer对象。怎么样?这个框架的使用很简单把。本篇文章介绍的Windsor内容比较有限,它是一个非常强大的框架,希望大家能进一步学习。

图8.IAddress和ClsAddress的实现

 

图9:ClsCustomer的实现

 

图10:使用Windsor框架进行DI操作

 

    图11显示了windsor中常用的配置文件实现DI的方法,具体的实现在这里就省略了,希望大家自己多尝试。

 

图11:使用XML配置windsor的DI操作

    本篇文章介绍了IOC的概念和实现了方法,并且推荐了Windsor这个有用的框架,请大家多多尝试,最后花了大家这么长时间阅读,而且语句很不通顺,真是抱歉了,总之谢谢大家支持,欢迎大家和我多多进行交流,祝大家有个好心情!

Guess you like

Origin www.cnblogs.com/lsqblog/p/11293980.html