Spring IoC concept and role of the

Couple and decouple procedures [appreciated] 

 

First, what is the coupling procedure

 

Coupling (Coupling), also called the degree of coupling between the modules is a measure of degree of association.

Coupling strength depends on the complexity of the interface between the modules, and the calling module way how much data is transmitted through the interface.

In software engineering, coupling refers to dependencies between objects. The higher the degree of coupling between objects, higher maintenance costs. Therefore, the design of the object
should be to minimize the coupling between classes and components. Software design and the degree of coupling is generally used as a measure of the cohesion degree of independence of the standard module.

 

A division criteria module is coupled to highly cohesive. 

 

If it has a Category:

(1) Content coupling.

When a module or directly modify the operating data of another module, or a module without other modules and proceeds through the normal entry, such a coupling is coupled referred to as content. Content coupling is coupled to the highest degree, it should be avoided. 

(2) common coupling.

Two or more than two modules collectively reference a global data item, this coupling is called common coupling. In the structure with a large number of common coupling in which module to determine whether the global variable assigned a specific value is very difficult.  

(3) an external coupling.

A set of modules are simple to access the same global variable instead of the same global data structures, and not transmitting the information through the global variable parameter table, it is called external coupling.

 (4) control the coupling.

A module transmitting a control signal via the interface, a signal receiving module to perform an appropriate operation based on the signal value, this coupling is referred to as a control module coupled to another.

 (5) coupled tag.

If a module A via the interface passing parameters to a common two modules B and C, the presence of a marker coupled between said modules B and C.  

(6) coupled to the data.

To pass data between the parameter module, the data is referred coupled. Data coupling is the lowest form of coupling, the coupling system of this general type are present, since in order to complete some meaningful function, often require some modules output data as input data to other modules.  

(7) a non-direct coupling.

There is no direct relationship between the two modules, the connections between them are entirely controlled by the main module and the calling to achieve.

to sum up:

 Coupling is an important factor in software complexity and design quality in the design that we should adopt the following principles: If it is necessary there is a coupling between modules, to try to use data coupling, less control coupling, to limit the scope of common coupling, try to avoid the use of content coupling. 

Cohesion and coupling.  

A flag module cohesion within individual elements coupled to each other close degree, which is a natural extension of information concealment and localization concepts. Cohesion is measured from a functional point of contact within the module, a good cohesive module should just do one thing. It describes a functional relationship within the module.

Is a measure of the coupling between the various software modules interconnected structure , the coupling strength depends on the complexity of the interface between the modules, a module entry or access point and pass
through the data interface. Coupling procedure is a low stress, high cohesion. It is the same between the various elements within a module height close to, but the interdependence between the various modules have to be less tight.

Coupling and cohesion are closely related, the presence of high coupling modules with other modules mean low cohesion, and the high cohesion of the module with other modules means
between the modules are loosely coupled. Making software design, should strive to achieve high cohesion, low coupling. 

We develop, some dependencies are required, some dependencies can be released by optimizing the code.

/** 
 * 账户的业务层实现类  
 * @author wupeng 
 * @Version 1.0  */ 
public class AccountServiceImpl implements IAccountService {  
    private IAccountDao accountDao = new AccountDaoImpl(); 
}

The above code represents:  

It calls business layer persistence layer, and at this time the service layer interface and implementation class dependent persistence layer. If at this time there is no persistence implementation class, the compiler will not pass. This compile dependencies, should put an end to our development. We need to optimize the code to solve. 

Another example is our operating JDBC, when the registration drive, why do not we use the register DriverManager method, instead of using Class.forName way?

public class JdbcDemo1 {  
public static void main(String[] args) throws Exception {   
    //1.注册驱动   
    //DriverManager.registerDriver(new com.mysql.jdbc.Driver());                   
     Class.forName("com.mysql.jdbc.Driver"); 
    //2.获取连接       
    //3.获取预处理 sql 语句对象   
    //4.获取结果集   
    //5.遍历结果集  
    } 
} 


The reason is this:

 Our class relies on a specific driver class database (MySQL), if this time to replace the brand of database (such as Oracle), you need to modify the source code to re database-driven. This is obviously not what we want. 

 

Second, the idea of ​​coupling settlement procedures 

 

When we explain JDBC when, driven by reflecting the register, as follows:

Class.forName("com.mysql.jdbc.Driver");//此处只是一个字符串 


Benefits at this time, our class is no longer dependent on a particular drive class, this time even delete mysql driver jar package, you can still compile (run not in order, not impossible to run a successful drive).

It also created a new problem, the fully qualified class name string mysql driver is written in java class dead, once to change or to modify the source code.

Solution to this problem is simple, using the configuration file configuration. 


Factory mode decoupling 


In the actual development, we can put three layers of objects with a profile configured up when starting the server application loading, so a class
method by reading the configuration file, to create out of these objects coexist together.

When the next use directly take over with a fine. So, this reads the configuration file and create an object of class is acquiring three factories. 


 Inversion of Control -Inversion Of Control 


The idea of ​​decoupling the previous section have two questions:  

1, which keep going?  

analysis:

Since we are many objects, we must find a collection to exist. At this time there Map and List are available. 

In the end choose Map or List to see if we find there is no demand.

There needs to find, select Map.  

So our answer is in the application is loaded, create a Map, used to store the three objects. We call this map container .  

2, still did not explain what the factory?  

Our factory is responsible for acquiring the specified object from the container class. This way we get the object has changed.  

original:      

When we get the object, it is based on the new approach. It is active

 

right now:      

When we get the object, while with the factory to, or find a factory to create an object for us. It is passive

 

 

这种被动接收的方式获取对象的思想就是控制反转,它是 spring 框架的核心之一。 

明确 ioc的作用:  

削减计算机程序的耦合(解除我们代码中的依赖关系)。 

IP.
发布了2 篇原创文章 · 获赞 0 · 访问量 19

Guess you like

Origin blog.csdn.net/weixin_46184650/article/details/104560400