[Open Source and Project Combat: Open Source Combat] 81 | Open Source Combat III (Part 1): Learn to discover and develop general function modules with Google Guava

In the last few lessons, we used the development of Unix, a super large open source software, as an introduction, and talked about how to deal with the development of large and complex projects from the perspectives of code design and development management. Next, let's talk about Google Guava, Google's open source Java development library.

Google Guava is a very successful and popular open source project. It has nearly 37,000 stars on GitHub. It is widely used in Java project development. Of course, we will not explain how to use each class and interface, but focus on explaining the design ideas behind them and the design patterns used. There is a lot of content, and I will explain it in three classes.

  • In the first lesson, we will give a brief introduction to Google Guava, and use this to talk about how to develop a general function module.

  • In the second lesson, we will talk about several design patterns used in Google Guava, and will supplement the Immutable pattern that was not mentioned before.

  • In the third lesson, we use Google Guava to explain the last of the three programming paradigms: functional programming.

Without further ado, let's officially start today's study!

Introduction to Google Guava

Considering that you may not be familiar with Google Guava, let me give it a brief introduction first.

Google Guava is an open source version of Google's internal Java development tools library. Many Java projects within Google use it. It provides functionality not provided by the JDK, as well as enhancements to functionality already available in the JDK. These include: Collections, Caching, Primitives Support, Concurrency Libraries, Common Annotation, Strings Processing, Math, I/O, Event Bus (EventBus), etc.

I intercepted the package structure diagram of Google Guava and posted it here, it will look more intuitive for you.

insert image description here
We know that the full name of JDK is Java Development Kit. It itself is a tool library provided by Java. Now please think about it, since there is a JDK, why does Google develop a new class library Google Guava? Is it repeating the early wheels? Where is the difference between the two?

With this question in mind, combined with Google Guava, let's learn how to discover common functional modules in business development, and how to develop them into class libraries, frameworks or functional components. When the study is over, I hope you can answer this question yourself.

How to discover common functional modules?

Many people think that there is no challenge in doing business development. In fact, doing business development also involves the development of many non-business functions, such as the ID generator, performance counter, EventBus, DI container we mentioned earlier, and the limit function mentioned later. Streaming framework, idempotent framework, and grayscale components. The key is that we must have the ability to be good at discovery and abstraction, and have solid design and development capabilities, be able to discover these non-business, reusable function points, and decouple them from business logic. Design And developed into an independent functional module.

In my opinion, in business development, there are generally three types of common functional modules that have nothing to do with business: library, framework, and component.

Among them, Google Guava belongs to the class library and provides a set of API interfaces. EventBus and DI containers belong to the framework and provide skeleton code, allowing business developers to focus on the business development part and fill in the reserved extension points with business code. ID generators and performance counters are functional components that provide a set of API interfaces with a special function. They are somewhat similar to class libraries, but are more focused and heavyweight. For example, ID generators may rely on external systems such as Redis, unlike Class library is as simple as that.

Whether the current limiting, idempotence, and grayscale mentioned above belong to the framework or functional components depends on the specific situation. If the business code is nested and developed within them, they can be called frameworks. If they only open API interfaces for business system calls, they can be called components. However, it doesn't matter much what it is called, and there is no need to delve too deeply into the concept.

So how do we discover these common functional modules in the project?

In fact, whether they are class libraries, frameworks or functional components, these general-purpose functional modules have two biggest characteristics: reuse and business-independent. Google Guava is a typical example.

If there is no reuse scene, then there is no need to extract it and design it as an independent module. If it is business-related and reusable, in most cases it will be designed as an independent system (such as microservices), rather than a class library, framework or functional component. Therefore, if the code you are responsible for development has nothing to do with the business and may be reused, then you can consider separating it and developing it into general functional modules such as class libraries, frameworks, and functional components.

To add a little bit, what we are talking about here is how to discover common functional modules in business development. In addition to business development teams, many companies also have some infrastructure teams and architecture development teams. In addition to developing class libraries, frameworks, and functional components, they will also develop some general systems and middleware, such as Google MapReduce, Kafka Message middleware, monitoring system, distributed call chain tracking system, etc.

How to develop general functional modules?

After we discover the development requirements of the general functional module, how to design and develop it into an excellent class library, framework or functional component? Today, we will not talk about specific development skills. We will talk about some specific development skills in the part of Spring open source practice later. I plan to talk about some more general development ideas today. I think with these first, it should be easier for you to understand the following content.

As a general class library, framework, and functional component, we hope that after development, it can be used not only in our own projects, but also in projects of other teams. It can even be open sourced for more people to use, so that it can be used to a greater extent. value and build their own influence.

Therefore, for the development of these class libraries, frameworks, and functional components, we cannot work behind closed doors, but develop them as "products". This product is a "technical product", and our target users are "programmers" to solve their "development pain points". We need to think more empathy, from the perspective of users, to think about what kind of functions they want.

For a technical product, although technical indicators such as few bugs and good performance are very important, but whether it is easy to use, easy to integrate, easy to plug, whether the documentation is comprehensive, whether it is easy to use, etc., these product qualities are also very important, and even can play a decisive role. It is often these things that are easily overlooked and not taken seriously will determine whether a technical product can stand out among many of its kind.

Specific to Google Guava, it is a development class library, the target users are Java development engineers, to solve the main pain point of users, compared with JDK, it provides more tool classes to simplify code writing, for example, it provides a method for judging null values Preconditions class; Splitter, Joiner, CharMatcher string processing class; Multisets, Multimaps, Tables and other richer Collections classes, etc.

Its advantages are as follows: First, it is managed by Google and maintained for a long time. After sufficient unit testing, the code quality is guaranteed; second, it is reliable, good in performance, and highly optimized. For example, Immutable Collections provided by Google Guava is better than JDK The unmodifiableCollection has good performance; third, comprehensive and complete documentation, easy to use, low learning cost, you can go to its Github Wiki.

We just talked about "product awareness", let's talk about "service awareness". I often say in the team that if you develop something for other teams, you must have a "service awareness". For programmers, this may be more lacking than "product awareness".

First of all, in terms of mentality, we have to learn to be grateful when other teams use the technical products we have developed. This is an important point. The mentality is different, and there will be subtle differences in doing things. Secondly, in addition to writing code, we also need to be mentally prepared to spend a lot of time answering questions and acting as customer service. With this mental preparation, you won't be annoyed when people from other teams ask you questions.

Compared with business code, the development of this kind of general-purpose code that is reused in multiple places requires higher code quality, because these projects have a greater impact, and once a bug occurs, it will implicate many systems or other projects. Especially if you want to open source the project, the impact will be even greater. Therefore, the code quality of this type of project is generally very good, and the development of this type of project has a greater exercise in coding ability. This is why I often recommend others to improve their skills by reading the code of famous open source projects and participating in open source projects.

Specific to Google Guava, it is developed by Google employees, the unit test is very complete, the comments are well written, and the code is well written. It can be said that it is the first-hand information for learning Google's development experience. If you have time, you can take it seriously. Read its code.

Although it is more technical to develop these general-purpose functional modules, we should not reinvent the wheel, and reuse as much as possible. Moreover, in the project, if you want to develop all common functions into independent class libraries, frameworks, and functional components, it will be a bit of a struggle, and you may not get the support of the leadership. After all, developing this part of the general function independently from the project will be more time-consuming than developing it as part of the project.

So, on balance, I recommend developing these common features as part of the project initially. However, during development, we do a good job of modularization, try to draw a clear line between them and other modules, and interact with other modes through loosely coupled methods such as interfaces and extension points. When the time is right, we will spin it out of the project. Because the modularization was done well before and the degree of coupling was low, the cost of stripping out would not be very high.

key review

Well, that's all for today's content. Let's summarize and review together, what you need to focus on.

Doing business development will also involve the development of many non-business functions. We must have the ability to be good at discovery and abstraction, and have solid design and development capabilities, be able to discover these non-business, reusable function points, and decouple them from business logic, design and develop them into Independent functional modules, such as class libraries, frameworks, and functional components.

In fact, whether it is a class library, a framework or a functional component, the two biggest features of these general-purpose functional modules are that reuse has nothing to do with business. If the code you develop is irrelevant to the business and may be reused, then you can consider separating it and developing it into a class library, framework, functional component, etc.

After we discover the development requirements of the general functional module, how to design and develop it into an excellent class library, framework or functional component? Here we talk about some more general development ideas, such as product awareness, service awareness, code quality awareness, and don't repeat the wheel.

In addition, I especially recommend that you read the open source code of Google Guava. Its code is not complicated, it is easy to read, and there will not be too much reading burden, but it is the first-hand information for you to obtain Google's development experience, especially in terms of unit testing and coding standards.

class disscussion

For the project you are participating in the development, think about it, what general functional modules can be abstracted, designed and developed into independent class libraries, frameworks, and functional components? What function points may they all include? Try to design it yourself!

Guess you like

Origin blog.csdn.net/qq_32907491/article/details/131365571