Summary of "Design Patterns Learned in Java Language (Multithread Edition)" (Part 4)

javathread.jpeg

Immutable pattern

The String class does not provide a method to change the contents of a string. The content of the string represented by the String instance never changes. For this reason, the methods of the String class do not need to be synchronized. ** Immutable ** means "immutable" or "unchanged". In the Immutable pattern, a class (** immutable class **) that guarantees that the state of the instance does not change appears. Since exclusive control is not required, performance improvement can be expected if used well.

(See this manual for the entire code)

public final class Person {
    private final String name;
    private final String hobby;

    public Person(String name, String hobby) {
        this.name = name;
        this.hobby = hobby;
    }

    public String getName() {
        return name;
    }

    public String getHobby() {
        return hobby;
    }
}

In this class, the value of the field of Person class can be set only in the constructor. There is also no method to change the value of the field. Therefore, once an instance of the Person class is created, the value of the field does not change. At this time, the Person class is safe even if it is accessed from multiple threads at the same time. Neither method needs to be synchronized.

Character section

** Immutable role ** appears. The Immutable role is a class that cannot change the value of a field and does not have a method to change the contents of the field.

When to use

--When the state does not change after the instance is created. It is the following time. ** The field is final, the setter method does not exist, and the instance referenced by the field does not change **.

Note that the following classes are not immutable.

public final class Hoge {
     private final StringBuffer hoge;

     public Hoge(String a, String b) {
         this.hoge = new StringBuffer("a=" + a + ", b=" + b);
     }

     public StringBuffer getHoge() {
         return hoge;
     }
}

The instance that the hoge field obtained by the getHoge method has is not a String but an instance of StringBuffer. Since the StringBuffer class has a method to change the internal state, the contents of the hoge field can be rewritten from the outside. Since the hoge field is declared final, the value of the field itself does not change, but the state of the instance to which the field (think of it as a pointer) points may change.

--When the instance is shared and frequently accessed.

mutable and immutable classes

Some Java standard class libraries are a pair of mutable and immutable classes. For example, the StringBuffer and String classes. StringBuffer is properly synchronized when rewriting. On the other hand, the String class does not use Synchronized, so high-speed reference is possible. Use StringBuffer if you want to change the content frequently, and use String if you don't need to change the content and just refer to it.

To protect immutability

If you remove synchronized on the assumption that it is an Immuable pattern, the safety of the class will be lost as soon as the immutability is lost. Therefore, invariance should be clearly stated in the program comments and API documentation.


Relation Summary of "Design Patterns Learned in Java Language (Multithreaded Edition)" (Part 1) Summary of "Design Patterns Learned in Java Language (Multithreaded Edition)" (Part 2) Summary of "Design Patterns Learned in Java Language (Multithreaded Edition)" (Part 3)

Recommended Posts

Summary of "Design Patterns Learned in Java Language (Multithread Edition)" (Part 10)
Summary of "Design Patterns Learned in Java Language (Multithread Edition)" (Part 7)
Summary of "Design Patterns Learned in Java Language (Multithread Edition)" (Part 3)
Summary of "Design Patterns Learned in Java Language (Multithread Edition)" (Part 6)
Summary of "Design Patterns Learned in Java Language (Multithread Edition)" (Part 4)
Summary of "Design Patterns Learned in Java Language (Multithread Edition)" (Part 5)
Summary of "Design Patterns Learned in Java Language (Multithread Edition)" (Part 2)
Summary of "Design Patterns Learned in Java Language (Multithread Edition)" (Part 1)
Summary of "Design Patterns Learned in Java Language (Multithread Edition)" (Part 11)
Summary of "Design Patterns Learned in Java Language (Multithread Edition)" (Part 12)
Summary of "Design Patterns Learned in Java Language (Multithread Edition)" (Part 8)
[Java] Summary of design patterns
A quick review of Java learned in class part4
Summary of Java language basics
A quick review of Java learned in class part3
A quick review of Java learned in class part2
I read Hiroshi Yuki "Introduction to Design Patterns Learned in Java Language" (SB Creative)
Java Design Patterns
What I learned in Java (Part 2) What are variables?
A quick review of Java learned in class
Summary of what I learned in Spring Batch
Try design patterns in C language! Memento pattern-Let's memorize the memories of the data
[Java] Basic summary of Java not covered by Progate ~ Part 1 ~
What I learned in Java (Part 3) Instruction execution statement
Summary of how to implement default arguments in Java
Summary of Java support 2018
Java design pattern summary
What I learned in Java (Part 4) Conditional branching and repetition
[Java] Basic summary of Java not covered by Progate ~ Part 2 ยท List ~
Road to Java Engineer Part2 What kind of language is Java?
Read design patterns in Ruby
[Java11] Stream Summary -Advantages of Stream-
[Java] Summary of regular expressions
[Java] Summary of operators (operator)
[Java] Summary of for statements
Summary of Java Math class
Implementation of gzip in java
[Java] Summary of control syntax
Implementation of tri-tree in Java
Summary of java error processing
[Java] Summary of mathematical operations
What I have learned in Java (Part 1) Java development flow and overview
Summary of ORM "uroboroSQL" that can be used in enterprise Java
[For beginners] Summary of java constructor
Summary of [Java silver study] package
Basic usage of java Optional Part 1
thread safe process in java language
AtCoder 400 points algorithm summary (Java edition)
List of members added in Java 9
Creating lexical analysis in Java 8 (Part 2)
List of types added in Java 9
Summary of object-oriented programming using Java
Implementation of like function in Java
Creating lexical analysis in Java 8 (Part 1)