Double-threaded Learning two impregnable mode immutable pattern

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.

Student class

1 /**2  * 3  */4  Packagecom.benxq.thread3;5 6 /**7 * Created by QUCF on October 22, 2015.8  */9  Public classStudent {Ten  One     PrivateString name; A     PrivateString address; -      PublicString GetName () { -         returnname; the     } -      Public voidsetName (String name) { -          This. Name =name; -     } +      PublicString getaddress () { -         returnaddress; +     } A      Public voidsetaddress (String address) { at          This. Address =address; -     } -     /** -      * @paramname -      * @paramAddress -      */ in      PublicStudent (string name, address of string) { -         Super(); to          This. Name =name; +          This. Address =address; -     } the}
View Code

Class

/** *  */ Packagecom.benxq.thread3;/*** Seemingly immutable class * actually not, student is mutable class * Created by QUCF on October 22, 2015. */ Public Final classThe hostel {Private FinalStudent Student; Private FinalString name;  Publicname,student (String Student) { This. name=name;  This. student=student; }         PublicStudent getstudent () {Student.setname ("Benxq"); returnstudent; }     PublicString GetName () {returnname; }        }
View Code

Test class

/** *  */ Packagecom.benxq.thread3;/*** Created by QUCF on October 22, 2015. */ Public classTest { Public Static voidMain (string[] args) {Student Student=NewStudent ("Zhangsan", "Beijing"); the=New("Room1", student); Student S2=room.getstudent ();        System.out.println (S2.getname ()); //Print Benxq    }}
View Code

Model of the infallible model

/** *  */ Packagecom.benxq.thread3;/*** A class is defined as a final class and is not allowed to have a subclass * A variable is defined as a final variable, meaning that once the first assignment is made, it cannot be changed. * The fields are also immutable * this immutability. Not necessarily by final decision. * The so-called immutability is a pattern of design. Immutable, is to prevent multithreading from destroying objects. * This immutable, no need to protect the premise, is not to be modified, once instantiated, only provide read operations * but can be in many ways: for example, privatization, do not provide assignment operation, final decoration.  * Created by QUCF on October 22, 2015. */ Public Final classThe hostel {Private FinalStudent Student; Private FinalString name;  Publicname,student (String Student) { This. name=name;  This. student=student; }         PublicStudent getstudent () {returnstudent; }     PublicString GetName () {returnname; }        }

Double-threaded Learning two impregnable mode immutable pattern

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.