Multithreaded Programming Learning (3) immutable pattern mode

Source: Internet
Author: User
Tags instance method

Immutable pattern "indestructible mode"

One: Participants in the immutable pattern
--->immutable (unchanging) participant
1.1:immutable Contributor is a class that cannot change the value of a field.
1.2:immutable also does not have any method to change the value of a field.
The 1.3:immutable participant method does not need to be set synchronized

II: When immutable pattern mode is used
---> When the state no longer changes when the instance is generated
2.1 Instance state no longer changes is the primary condition.
2.2 is the immutable state. Is that the value of the instance does not change.
---> Instances need to be shared and accessed frequently
--->string class is immutable class does not require synchronized protection
StringBuffer class non-immutable class requires synchronized protection


Three: Immutable pattern thinking
--->final keywords
2.1:final class: When the final class of life, the class cannot be extended, that is, the subclass cannot be defined.
2.2:final Method:
If the instance method (non-static) is declared final, the method cannot overwrite overrides of the class override.
If you declare a class method (static) as final, the method cannot be hidden by the class
2.3final variables
The final field can only be assigned one time.
Final (non-static) field assignment is divided into two methods: (1) assigning values at declaration (2) by constructors
There are two ways to assign a final (static) field: (1) Assign a value at declaration (2) by static block assignment
2.3 Concepts of covering and hiding
Instance methods When overridden by a quilt class method, the method that is actually called is determined at execution time.
When a class method is hidden by the quilt class method, the method that the instance is called is determined at compile time.

--->immutable invariance is subtle. Not set finnal, privatization, no assignment method, is permanent invariance.
Final only cannot be assigned again. If the final field is a mutable reference property, then the immutable is destroyed.

Class

1  Packagecom.yeepay.sxf.thread2;2 /**3 * Looks like a immutable4 * not really. 5 * Student is a mutable class6  * @authorSXF7  *8  */9  Public Final classThe hostel {Ten  One     Private FinalStudent Student; A     Private FinalString name; -  -      Publicname,student (String Student) { the         Super(); -          This. student=student; -          This. Name =name; -     } +  -      PublicStudent getstudent () { +Student.setname ("SXF"); A         returnstudent; at     } -  -      PublicString GetName () { -         returnname; -     } -      in}
View Code

Student class

1  Packagecom.yeepay.sxf.thread2;2 3  Public classStudent {4     PrivateString name;5     PrivateString address;6      PublicStudent () {7         Super();8     }9      PublicStudent (string name, address of string) {Ten         Super(); One          This. Name =name; A          This. Address =address; -     } -      PublicString GetName () { the         returnname; -     } -      Public voidsetName (String name) { -          This. Name =name; +     } -      PublicString getaddress () { +         returnaddress; A     } at      Public voidsetaddress (String address) { -          This. Address =address; -     } -      -      -      in}
View Code

Test class

1  Packagecom.yeepay.sxf.thread2;2 /**3 * Test Thread4  * @authorSXF5  *6  */7  Public classTest {8 9      Public Static voidMain (string[] args) {Ten          One          A          -Student student=NewStudent ("HSL", "HNLH"); -The room=New("DDD", student); theStudent student2=room.getstudent (); - System.out.println (Student2.getname ()); -          -         //Printed: SXF +     } -}
View Code

Immutable paradigm (immutable is just a conceptual pattern, not a fixed format)

1  Packagecom.yeepay.sxf.thread2;2 /**3 * Unbreakable mode. 4  * @authorSXF5  * 6 * A class is defined as a final class and is not allowed to have subclasses7 * A variable is defined as a final variable, meaning that once a value is first assigned, it cannot be changed. 8 * The fields are immutable9  * Ten  *  One * this immutability. Not necessarily by final decision.  A * The so-called immutability is a pattern of design. Immutable, is to prevent multithreading from destroying objects.  - * This immutable, do not need to protect the premise that can not be modified, once instantiated, only provide read operations - * But there are many ways: for example, privatization, no assignment operation, final retouching.  the  * -  */ -  Public Final classPersion { -     //declaring Variables +     Private FinalString name; -     Private FinalString address; +      A     //constructor Assignment at      Publicpersion (string name, address of string) { -          This. Name =name; -          This. Address =address; -     } -  -     //provides a get access method, but does not provide a set assignment method in      PublicString GetName () { -         returnname; to     } +  -      PublicString getaddress () { the         returnaddress; *     } $     Panax Notoginseng      -      the      +      A      the}
View Code

Multithreaded Programming Learning (3) immutable pattern mode

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.