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/