Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to decouple in Java

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

Xiaobian to share with you how to decouple Java, I believe most people do not know how, so share this article for everyone's reference, I hope you have a lot of harvest after reading this article, let's go to understand it together!

What is coupling?

Coupling, also known as coupling degree, is a measure of the degree of association between modules. The coupling degree between modules refers to the dependency relationship between modules, including control relationship, call relationship and data transfer relationship. The more connections between modules, the stronger their coupling, and the less independent they are (reducing coupling can increase their independence).

What is coupling between programs

If: When I go to new an object, and this object does not exist, this time the program will report a compile-time exception, which means that the program can not even run, we can understand that their coupling is high.

How to decouple

We can use Java reflection technology, through the class name, to reflect the creation of objects, this time we can successfully avoid compile-time exceptions, and ensure that the project can still run normally at this time.

factory mode decoupling

In actual development we can configure all three layers of objects using configuration files, and when the server application is started to load, let a method in a class create and store these objects by reading the configuration file. In the next use, just take it and use it. Then, the class that reads configuration files, creates and fetches three-tier objects is the factory.

case

In the early days of our JDBC operation, when registering drivers, why didn't we use DriverManager's register method instead of Class.forName?

public class JdbcDemo1 { public static void main(String[] args) throws Exception { //1. Register driver //DriverManager.registerDriver (new com.mysql.jdbc.Driver()); Class.forName("com.mysql. jdbc.Driver"); //2. Get connection//3. Get preprocessed sql statement object//4. Get result set//5. Traverse result set}} The reason is:

Our class depends on the specific driver class of the database (MySQL). If you change the database brand at this time (such as Oracle), you need to modify the source code to re-drive the database. This is obviously not what we want.

Solution:

When we explain jdbc, it is through reflection to register the driver, the code is as follows:

Class.forName("com.mysql.jdbc.Driver");//this is just a string

The advantage of this time is that our class no longer depends on specific driver classes. At this time, even if the driver jar package of mysql is deleted, it can still be compiled (don't think about running, it is impossible to run successfully without drivers). At the same time, also produced a new problem, mysql driver fully qualified class name string is written in java class dead, once to change or modify the source code. Solving this problem is also very simple, using configuration file configuration.

The above is just a kind of decoupling thinking, which is convenient for everyone to understand.

The core idea of decoupling: To reduce the coupling degree of code, we must try our best to make the code of each function not cross written, and write closed code.

decoupled

The majority of program ape compatriots often see "decoupling," and many people will use this word to install X, but not many people can actually understand it. Next, take everyone to walk through the in-depth, how to decouple.

First of all, we need to know why we want to decouple: usually, we do a project, will use a lot of basic functional blocks, such as xxx communication protocol, xxxView, etc., we will package this functional block into a library, if this library, can only run in this specified project, this is called high coupling, which leads to, if the next time you encounter a similar project, need to use the same functional blocks, you will have to do a lot of duplication of work. Assuming that every time you use json, you have to modify the json library, that would be a bolt from the blue.

However, contrary to expectations, in some cases, it is really not very good decoupling.

Here, let's start with a chestnut, like sorting.

A sorting function, for most rudimentary apes, might look something like this:

sort(List list)

This leads to a problem, this method can only sort int data, if the next project, need to use the String to sort, it is very embarrassing, feel obviously to succeed, but also a little worse. Right, that's the difference, that's the key to code decoupling.

We must first make it clear that what we need to do is the sorting function. In this process, we inevitably need to use the size comparison of two data, and this data may be any data, that is to say, the sorting algorithm, we can determine it, make it a stationary library, but there is a data size matching that we cannot do, or the coupling point of the library. What should we do?

Let's just let the people who use our function block tell us.

Below, we refer to the Android library, there is a sorted api

Collections.sort(List list, Comparator

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report