[Object Oriented Design Basics] unchanged objects

Source: Internet
Author: User

Author: gnuhpc
Source: http://www.cnblogs.com/gnuhpc/

1) Application Scenario: When data objects are modified, data may be disordered due to the order of changes. This can be avoided by using unchanged objects, you can also choose not to use the highly-available Synchronizing Method. The data held by an instance of the data model class remains unchanged throughout the lifecycle, and this class will be thread-safe.

2) design method:

A) All instance variables must be set in the constructor, because constructor is thread-safe.

B) set this class to final to prevent class method overwrites due to inheritance.

C) All instance variables must be set to final, so that they are only set once in the constructor.

D) if the instance variable contains references to other objects, the corresponding getter method should return a copy of the object,

 

3) instance:

Public finalClass employeemodel {
// State
Private final string firstname;
Private final string lastname;
Private final string SSN;
Private final string address;
Private final car;
// Constructor
Public employeemodel (String FN, string ln, string SSN, string ADDR, car C){
Firstname = FN;
Lastname = ln;
SSN = SSN;
Address = ADDR;
Car = C;
}
// Getters
Public String getfirstname (){
Return firstname;
}
Public String getlastname (){
Return lastname;
}
Public String getssn (){
Return SSN;
}
Public Car getcar (){
// Return a copy of the car object
Return (CAR) car. Clone ();
}
Public String getaddress (){
Return address;
}
}

Author: gnuhpc
Source: http://www.cnblogs.com/gnuhpc/

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.