Spring Learning (2): interface-oriented programming ideas

Source: Internet
Author: User

I. INTRODUCTION

The entity of the spring core IOC uses an interface-oriented programming idea, so it is necessary to understand the next. In a nutshell, Spring is a lightweight control inversion (IOC) and aspect-oriented (AOP) container framework.

The concept of the definition of an interface: an abstraction that an entity provides itself to the outside world (which can be another entity) that separates external communication methods from internal operations so that they can be internally modified without affecting the way other entities interact with the outside world. Focus: abstraction, Interaction . (entities that provide external functionality are abstracted into an interface (Java concept) so that internal modifications do not affect external interactions with them.) (The focus is shifted from the specific implementation to the interface interaction).)

Two. Text 

(1) First look at an example

Cars and tires, suppose there's a car car,bridgestone tires, michelin tires

Bridgestone Rotation:

public class bridgestone{public    void Roll () {        System.out.print ("Bridgestone is rolling.");}    }

Michelin Rotation:

Class Michelin {public    void Roll () {        System.out.print ("Michelin is Rolling");}    }

Car Rotation:

public class Car {public    void Roll (Bridgestone Bridgestone) {        bridgestone.roll ()}    }

(extensibility) But if the car uses Michelin tires, then the car's code cannot be implemented and needs to be modified, then it becomes the following:

public class Car {public    void Roll (Michelin Michelin) {        michelin.roll ()}    }

But if there are a,b,c and other brands of tires (analogy: business needs change), it will be changed countless times. It is obvious that the code coupling between the design classes is too tight, difficult to reuse (car code can not be reused under each of the tires, all need to be modified), writing unit testing difficult (?). : It is necessary to ensure that the roll method of car is called when the Bridgestone roll method is called. Unit test this block is temporarily not understood. )

Unit Test concept: refers to the software in the smallest testable unit to check and verify. If the module has strong coupling to other modules, it will be difficult to test the unit. 1. The unit test code requires more "piles" because the individual modules will not compile correctly, and testers must write "pile" code to make the module run. 2. Testing becomes more complex and full coverage is more difficult. Modules with complex dependencies have more internal status and cannot be fully covered by unit tests, reducing the quality of testing.

If the common characteristics of the two tires are extracted, the car to rotate the tires, do not need to know the specific type of tyre (brand, the corresponding sub-category), as long as the tyre has the customer's desired interface (roll) on the line, so car code extensibility, can accept any trie interface to implement the tires, Unit tests are simple (injected into the implementation class), and the code coupling is loosely coupled relative to the previous.

Interface Tire {public    void Roll ();} Class Bridgestone implements Tire {public    void Roll () {        System.out.print ("Bridgestone is rolling.");    } Class Michelin implements Tire {public    void Roll () {        System.out.print (' Michelin is rolling ');}    }
public class Car {    
}

The above is an example of interface-oriented programming.

Interface-oriented Programming: interface-oriented means that the coupling between the various entities is promoted to the interface level by the coupling between the components, and the interface is used as the binder to maintain the relationship, so that an entity relies on another entity to become dependent on an interface, and the change of the dependent entity is isolated from the interface. (The interaction between all classes or modules in an object-oriented system is done by the interface.) )

(1) interface is the standard specification , a normative constraint, with the standard to adhere to easy to expand ! We only need to target standard programming , not specific implementation classes! Interface-Oriented Programming benefits: (1) abstraction, (2) high cohesion, low coupling, reduced development costs/maintenance costs (developers of car and tyre developers can develop in parallel, independently test, replace tires, do not need to modify car's code).

(2) Coordinator (interface), caller, implementation, interface is essentially the facilitator to maintain the relationship between the creator and the caller . Both the implementation and the caller must follow the rules of the interface (standard).

Example: JDBC, database drivers are developed according to the standard specifications of JDBC, and programmers use Access databases in accordance with JDBC standards. Benefit: Driver writers do not have to learn the interface documentation to explain the driver usage to the programmer, and the programmer does not care about the specific database.

(3) High cohesion, low coupling concept

Cohesion Poly-low coupling is a basic principle of software design, that is, in each module of the program, as far as possible to make each module independent, related processing as far as possible in a single module to complete, is the saying goes: why do go! Advantages: It can reduce the connection between each module, reduce the "reaching" probability, improve the development efficiency, reduce the upgrade maintenance cost, also facilitate unit testing, improve software quality.

Coupling is also known as inter-block linkage. A measure of the degree of connection between modules in a software system structure. The tighter the connection between the modules, the stronger the coupling, the less the module's independence. The coupling between modules depends on the complexity of the interface between modules, the mode of invocation and the information transmitted.

Cohesion Keyword: The most adequate use of each element of the function of the module to achieve maximum function, the stronger the better, with the smallest resources to do the biggest thing!

Low-COUPLING keywords: The association between each module in the project should be as small as possible, the lower the coupling (inter-relationship), the better, reduce the possibility of "reaching"!

High Coupling example: A program has 50 functions, the program executes very well, but once you modify one of the functions, the other 49 functions need to be modified, which is the consequence of high coupling.

Resources:

csdn:51252003

9927225

Baidu know: Https://baike.baidu.com/item/%E5%8D%95%E5%85%83%E6%B5%8B%E8%AF%95/1917084?fr=aladdin

Https://zhidao.baidu.com/question/271744863.html

Book: Spring Combat version 4th

Spring Learning (2): interface-oriented programming ideas

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.