Learn what the same mode

The same pattern

A) an overview of mode unchanged

Usage scenarios: When multiple threads to read and write the same object, in order to ensure consistency and correctness of the data subject, it is necessary to synchronize the object.

Role: In a multithreaded environment to ensure that there is no synchronization operation, the internal state of an object to maintain consistency and correctness

The core idea: Once an object is created, its internal state will never change, while its internal state will never change on their own.

Two), the difference between the change pattern of the read-only attribute

Read-only attribute: the object itself can not be modified by other threads, but their state of the object may modify.

The same pattern: a thread can not modify its internal data structure and, at the same time, its own internal state will not change, that is, since the object is created, the internal state data and to maintain absolute stability.

Read-only property Example:

The survival time of an object (object creation time the time difference and current time) is read-only, because a third party any thread can not modify this property, because over time, survival time are changing, not satisfied It requires constant mode, for whatever reason, since the object is created, its internal state and data to maintain absolute stability.

C), the same pattern of usage scenarios

1). When the object is created, its internal state and data is no longer any change.

2) The object needs to be shared, accessed frequently by multiple threads.

D) to achieve the same mode

1) Removal of all modifications setter methods and method attributes itself

2). All property is set to private, and with a final mark

3) Make sure there is no subclass can override modify its behavior

4) There can create a complete object constructor

product categories:

/**
 * 不变模式的实现:
 *
 *    Product: 产品类
 *
 *    属性:
 *    no: 序列号
 *    name: 名称
 *    price:价格
 *
 *    不变模式的要求:
 *    1.类由final修饰,没有子类重载可以改变它的行为
 *    2.属性由final修饰,确保不可改
 *    3.去除setter以及所有修改属性的方法
 *    4.有一个可以创建完整对象的构造函数
 *
 * */
//1.类名由final修饰
public final class Product {
    //2.属性由private final修饰
    private final String no;
    private final String name;
    private final double price;
    //3.没有设置setter方法

    //4.可以创建完整对象的构造函数
    public Product(String no, String name, double price) {
        this.no = no;
        this.name = name;
        this.price = price;
    }
}

FIVE), the same model in the JDK

JDK, the application of the same model is widely used.

java.lang.String class and all metadata packaging classes, are implemented using the same pattern.

The main types of mode unchanged JDK as follows:

java.lang.Stirng

java.lang.Boolean

java.lang.Byte

java.lang.Character

java.lang.Double

java.lang.Float

java.lang.Integer

java.lang.Long

java.lang.Short

Note: In a multithreaded environment, the same subject is not required for synchronous operations.

Guess you like

Origin www.cnblogs.com/Auge/p/11703374.html