Mode 2-Immutable

Source: Internet
Author: User

Come to Java multi-thread design mode

I. Method: the synchronized method is not required and remains unchanged. No method can be modified by the outside world. Only the read method is provided.

 Public   Final   Class Person { Private FinalString name;Private FinalString address;      Public Person (string name, string address ){ This . Name = Name; This . Address = address ;} Public String getname (){ Return Name ;} Public String getaddress (){ Return Address ;} Public String tostring (){ Return " [Person: Name = "+ Name +" , Address = "+ Address +" ] ";}}
 
Ii. uneasily discovered errors:
 Public   Final   Class Userinfo { Private   Final Stringbuffer Info; Public Userinfo (string name, string address ){ This . Info = New Stringbuffer (" <Info name = \" "+ Name +" \ "Address = \" "+ Address +" \ "/> ");}  PublicStringbuffer getinfo (){ReturnInfo ;}      Public String tostring (){ Return " [Userinfo: "+ Info +" ] ";}}
 Public   Class Main { Public   Static   Void Main (string [] ARGs ){ // Create an object Userinfo = New Userinfo (" Alice "," Alaska "); // Display System. Out. println (" Userinfo = "+ Userinfo );// Status change          Stringbuffer info = userinfo. getinfo (); Info. Replace (12, 17 ,"Bobby");   // "Alice" found 12... 16          // Display again System. Out. println (" Userinfo = "+ Userinfo );}}
 
Result:
 
Userinfo = [userinfo: <info name ="Alice"Address ="Alaska"/>] Userinfo = [userinfo: <info name ="Bobby"Address ="Alaska"/>]

 

 Public   Final   Class Immutableperson { Private   Final String name; Private   Final String address; Public Immutableperson (string name, string address ){ This . Name = Name; This . Address = address ;} PublicImmutableperson (mutableperson person ){This. Name = person. getname ();This. Address = person. getaddress ();}      Public Mutableperson getmutableperson (){ Return   New Mutableperson ( This );} Public String getname (){ Return Name ;} Public String getaddress (){ Return Address ;} Public String tostring (){ Return " [Immutableperson: "+ Name +" , "+ Address +"] ";}}
 
 
 
 Public Synchronized VoidSetperson (string newname, string newaddress){Name = newname; address = newaddress ;}
 
When immutableperson (mutableperson person) is executed, the name and address of the person may not match.

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.