This is the 17th day of my participation in the August Text Challenge.More challenges in August

define

Some information about a class is hidden inside the class, not allowed to be accessed directly by external members, and if necessary, exposed. For private class members, for example, get/set is considered

disadvantages

There would be a lot of boilerplate code, and the class would be very redundant. The good news is that ides provide one-click generation

advantages

For some private members, you can choose whether to expose them or not. At the same time, you can also process the output data and verify the input data in this step

Lombok plug-in

Due to conventional wisdom in Java development, when we write entity classes, we define attributes as private and then provide the full set of GET /set. Of course, you can take advantage of lombok’s @data annotation to get rid of this hassle

advantages

Using annotations can simplify a lot of Java boilerplate code

disadvantages

Forcing project participants to use all of them is the problem with strong coupling

If it is a new project, it is suggested that the team use this when planning. It is a real fragrance

Commonly used annotations

@data, NoArgsConstructor, @toString

@Data

Provide standard get/set methods for all attributes

@NoArgsConstructor

Provides a no-parameter constructor

@ToString

Override toString() in class to output jSON-like, easy-to-read data

@AllArgsConstructor

A constructor that provides full arguments, the order of which depends on the order in which the attributes are defined

This is not recommended if the class changes frequently, as it may affect other code that uses older full-parameter constructors

@EqualsAndHashCode

Rewrite equals(), hashCode()

Just a few of the most commonly used items in the project are listed here on Lombok’s website

The principle of

Lombok uses apis provided by Java’s plug-in annotation processor to read, modify, and add arbitrary elements to the abstract syntax tree. If the syntax tree is modified because of these annotations, the compiler reanalyzes the syntax and syntax until all of the plug-in annotations have not modified the syntax tree.

So, if we need to use Lombok, we also need to configure our IDE so that it can be associated with the corresponding methods provided by annotations as we code

scope

Third party: can also be called consumer of library class, consumer of service

We are generally developers of library classes and service providers

public

Global scope: a variable or method is declared to support global calls

default

When we need to develop a slightly more complex library class, we usually put all the related files in a package directory for other components to call each other, but we don’t want to expose them to third parties

Public is the default scope for non-scoped methods and variables declared in interfaces

protected

Properties and methods in a parent class need to be available to subclasses of different packages without being exposed to arbitrary access

private

Private, available to you only

For example, if we want to implement a simple singleton pattern, we need to declare the constructor private, so that someone else does not inadvertently initialize a new instance that overwrites our carefully written instance

The benefits of controlling scope

One purpose of strictly limiting the scope of services and preventing services that are used internally from being used by third parties is that we need to have the right to iterate and upgrade the internal services, which are then assembled and packaged to become higher level services for external invocation. For services provided externally, it is strictly required that method names, outgoing parameters and incoming parameters cannot be changed (to avoid any impact on third-party callers).

If it is necessary to optimize and rewrite the methods serving third parties due to the defects of library class design, it is recommended to use the annotation @deprecated to indicate method/class expiration provided by Java on the relevant methods first, to provide several versions of buffer time for third parties, and then remove it