集合类属性深克隆与浅克隆测试
This commit is contained in:
@@ -3,7 +3,9 @@ package top.fjy8018.designpattern.pattern.creational.prototype.clone;
|
|||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
import lombok.Setter;
|
import lombok.Setter;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.apache.ibatis.cache.CacheKey;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -12,6 +14,9 @@ import java.util.Date;
|
|||||||
* <p>
|
* <p>
|
||||||
* 邮件——深克隆实现
|
* 邮件——深克隆实现
|
||||||
*
|
*
|
||||||
|
* JDK源码样例:{@link ArrayList#clone()} 通过元素拷贝实现
|
||||||
|
* mybatis源码样例:{@link CacheKey#clone()} 通过重新初始化一个对象再进行数组拷贝实现
|
||||||
|
*
|
||||||
* @author F嘉阳
|
* @author F嘉阳
|
||||||
* @date 2018-09-25 20:39
|
* @date 2018-09-25 20:39
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -0,0 +1,43 @@
|
|||||||
|
package top.fjy8018.designpattern.pattern.creational.prototype.clone;
|
||||||
|
|
||||||
|
import lombok.Getter;
|
||||||
|
import lombok.Setter;
|
||||||
|
import org.apache.ibatis.cache.CacheKey;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Date;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 对集合类原型模式实现
|
||||||
|
*
|
||||||
|
* @author F嘉阳
|
||||||
|
* @date 2018-09-25 22:59
|
||||||
|
*/
|
||||||
|
@Getter
|
||||||
|
@Setter
|
||||||
|
public class ListCloneAfter implements Cloneable {
|
||||||
|
private String name;
|
||||||
|
private List<Date> dateList;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "ListCloneBefore{" +
|
||||||
|
"name='" + name + '\'' +
|
||||||
|
", dateList=" + dateList +
|
||||||
|
'}';
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 重新实现集合类属性值拷贝方法,模仿{@link CacheKey#clone()}
|
||||||
|
*
|
||||||
|
* @return
|
||||||
|
* @throws CloneNotSupportedException
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
protected Object clone() throws CloneNotSupportedException {
|
||||||
|
ListCloneAfter newInstance = (ListCloneAfter) super.clone();
|
||||||
|
newInstance.setDateList(new ArrayList<>(this.dateList));
|
||||||
|
return newInstance;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,33 @@
|
|||||||
|
package top.fjy8018.designpattern.pattern.creational.prototype.clone;
|
||||||
|
|
||||||
|
import lombok.Getter;
|
||||||
|
import lombok.Setter;
|
||||||
|
|
||||||
|
import java.util.Date;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 对集合类原型模式实现
|
||||||
|
*
|
||||||
|
* @author F嘉阳
|
||||||
|
* @date 2018-09-25 22:59
|
||||||
|
*/
|
||||||
|
@Getter
|
||||||
|
@Setter
|
||||||
|
public class ListCloneBefore implements Cloneable {
|
||||||
|
private String name;
|
||||||
|
private List<Date> dateList;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "ListCloneBefore{" +
|
||||||
|
"name='" + name + '\'' +
|
||||||
|
", dateList=" + dateList +
|
||||||
|
'}';
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected Object clone() throws CloneNotSupportedException {
|
||||||
|
return super.clone();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,78 @@
|
|||||||
|
package top.fjy8018.designpattern.pattern.creational.prototype.clone;
|
||||||
|
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Date;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@Slf4j
|
||||||
|
class ListCloneBeforeTest {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 由输出可知,集合类属性值传递的也是引用
|
||||||
|
* output
|
||||||
|
* ===
|
||||||
|
* 7f13d6e ListCloneBefore{name='初始', dateList=[Thu Jan 01 08:00:00 CST 1970, Tue Sep 25 23:05:17 CST 2018]}
|
||||||
|
* 1e965684 ListCloneBefore{name='初始', dateList=[Thu Jan 01 08:00:00 CST 1970, Tue Sep 25 23:05:17 CST 2018]}
|
||||||
|
* 7f13d6e ListCloneBefore{name='初始', dateList=[Tue Sep 25 23:05:17 CST 2018]}
|
||||||
|
* 1e965684 ListCloneBefore{name='初始', dateList=[Tue Sep 25 23:05:17 CST 2018]}
|
||||||
|
* ===
|
||||||
|
*
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
void cloneBefore() throws Exception {
|
||||||
|
List<Date> dates = new ArrayList<>();
|
||||||
|
dates.add(new Date(0L));
|
||||||
|
dates.add(new Date());
|
||||||
|
|
||||||
|
ListCloneBefore before = new ListCloneBefore();
|
||||||
|
before.setName("初始");
|
||||||
|
before.setDateList(dates);
|
||||||
|
ListCloneBefore after = (ListCloneBefore) before.clone();
|
||||||
|
|
||||||
|
log.info(Integer.toHexString(before.hashCode()) + " " + before);
|
||||||
|
log.info(Integer.toHexString(after.hashCode()) + " " + after);
|
||||||
|
|
||||||
|
dates.remove(0);
|
||||||
|
before.setDateList(dates);
|
||||||
|
|
||||||
|
log.info(Integer.toHexString(before.hashCode()) + " " + before);
|
||||||
|
log.info(Integer.toHexString(after.hashCode()) + " " + after);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 改造后输出符合预期
|
||||||
|
* output
|
||||||
|
* ===
|
||||||
|
* 7f13d6e ListCloneBefore{name='初始', dateList=[Thu Jan 01 08:00:00 CST 1970, Tue Sep 25 23:10:32 CST 2018]}
|
||||||
|
* 1e965684 ListCloneBefore{name='初始', dateList=[Thu Jan 01 08:00:00 CST 1970, Tue Sep 25 23:10:32 CST 2018]}
|
||||||
|
* 7f13d6e ListCloneBefore{name='初始', dateList=[Tue Sep 25 23:10:32 CST 2018]}
|
||||||
|
* 1e965684 ListCloneBefore{name='初始', dateList=[Thu Jan 01 08:00:00 CST 1970, Tue Sep 25 23:10:32 CST 2018]}
|
||||||
|
* ===
|
||||||
|
*
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
void cloneAfter() throws Exception {
|
||||||
|
List<Date> dates = new ArrayList<>();
|
||||||
|
dates.add(new Date(0L));
|
||||||
|
dates.add(new Date());
|
||||||
|
|
||||||
|
ListCloneAfter before = new ListCloneAfter();
|
||||||
|
before.setName("初始");
|
||||||
|
before.setDateList(dates);
|
||||||
|
ListCloneAfter after = (ListCloneAfter) before.clone();
|
||||||
|
|
||||||
|
log.info(Integer.toHexString(before.hashCode()) + " " + before);
|
||||||
|
log.info(Integer.toHexString(after.hashCode()) + " " + after);
|
||||||
|
|
||||||
|
dates.remove(0);
|
||||||
|
before.setDateList(dates);
|
||||||
|
|
||||||
|
log.info(Integer.toHexString(before.hashCode()) + " " + before);
|
||||||
|
log.info(Integer.toHexString(after.hashCode()) + " " + after);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user