正在浏览一个遗留代码并发现使用克隆方法,但不确定它在这种情况下的帮助。
以下是相同的代码段:
记录HashMap在下面的类中是不可感知的,并且将在另一个StudentInformation类中访问。
public class LoadStudentRecord{
private static HashMap<String,StudentRecord> record = new HashMap<String,StudentRecord>();
private final static ReentrantReadWriteLock accessLock = new ReentrantReadWriteLock(true);
private final static Lock readLock = accessLock.readLock();
private final static Lock writeLock = accessLock.writeLock();
public static void initialize(){
//initializes the hashmap record
}
public static getRecord(String keyVal){
readLock.lock();
try{
return record.get(keyVal);
}finally{
readLock.unlock();
}
}
现在有一个StudentInformation.class,如下面的代码片段所示,它从上面的类访问HashMap并使用clone方法:
public class StudentInformation{
private StudentRecord getStudentRecord(String studentId){
StudentRecord record = LoadStudentRecord.getRecord(studentId).clone();
return record;
}
那么克隆在上面的例子中是如何帮助的?