Java class library design guide based on JCABI Immutable
Java class library design guide based on JCABI Immutable
Overview:
JCABI IMMUTable is an open source Java class library that is used to achieve the creation and operation of unsatisfactory objects.It provides a simple and easy -to -use API that helps developers to write more secure and efficient code.This article will introduce how to use JCABI IMMUTable to design and build an unsatisfactory Java class library, and provide some example code.
1. What is an unsatisfactory object?
Unchanged objects cannot be modified after its creation.Their state is determined during creation and is not allowed to be changed through any method.The benefits of unsatisfactory objects include thread safety, easy cache, and easier testing and debugging.By using JCABI Immutable, we can simplify the creation and operation process of unsatisfactory objects.
2. Introduce JCABI Immutable
To use JCABI Immutable, you must first add JCABI Immutable dependencies to the Maven or Gradle project.For example, you can add dependencies in Maven:
<dependency>
<groupId>com.jcabi</groupId>
<artifactId>jcabi-immutable</artifactId>
<version>1.7</version>
</dependency>
3. Definition uncharacteristic category
The first step with JCABI IMMUTable is to define an unavailable class.A non -mutinal class usually needs to meet the following conditions:
-All fields are private and have no public setter method.
-All fields are final and initialize in the constructor.
-Colin the public method that is not provided to modify the field status.
The following is an example of an unavailable code:
import com.jcabi.immutable.Array;
import com.jcabi.immutable.ListOf;
public final class ImmutablePerson {
private final String name;
private final int age;
private final ListOf<String> hobbies;
public ImmutablePerson(String name, int age, ListOf<String> hobbies) {
this.name = name;
this.age = age;
this.hobbies = hobbies;
}
public String getName() {
return this.name;
}
public int getAge() {
return this.age;
}
public ListOf<String> getHobbies() {
return this.hobbies;
}
}
4. Create an unsmorable object
Using JCABI IMMUTable, we can use the Builder mode to create unsatisfactory objects.We can set the attributes of the object through a chain call, and finally call the BUILD () method to build an unsatisfactory object.Below is an example of creating Immutableperson object:
ImmutablePerson person = new ImmutablePerson.Builder()
.name("Alice")
.age(25)
.hobbies(new ListOf<>("Reading", "Traveling"))
.build();
5. Operation of unsatisfactory objects
Because the unsatisfactory objects are not modified, we cannot perform other operations except access to its attributes.This means that we cannot directly modify the attributes of the unsatisfactory objects, but we need to achieve some operations by creating a new uncable object, such as adding/delete elements.The following is the code of some example operations:
ImmutablePerson person = new ImmutablePerson.Builder()
.name("Alice")
.age(25)
.hobbies(new ListOf<>("Reading", "Traveling"))
.build();
// Add new hobbies
ImmutablePerson updatedPerson = new ImmutablePerson.Builder(person)
.hobbies(person.getHobbies().append("Cooking"))
.build();
// Delete one hobby
ImmutablePerson updatedPerson = new ImmutablePerson.Builder(person)
.hobbies(person.getHobbies().remove("Reading"))
.build();
Summarize:
By using JCABI IMMUTable, we can create and operate unsatisfactory objects easier.It provides a simple and easy -to -use API that helps us design a safer and efficient Java class library.This article introduces how to use JCABI IMMUTable to define, create, and operate unsusable objects, and provide some example code for reference.I hope these guidelines will be helpful for developers using JCABI Immutable!