Java中的原型模式
原型模式简介
- 模式属于创建型模式,它提供了一种创建对象的最佳方式。
- 用于创建重复的对象,同时又能保证性能。
- 这种模式是实现了一个原型接口,该接口用于创建当前对象的克隆。当直接创建对象的代价比较大时,则采用这种模式。
- 例如,一个对象需要在一个高代价的数据库操作之后被创建。我们可以缓存该对象,在下一个请求时返回它的克隆,在需要的时候更新数据库,以此来减少数据库调用。
浅克隆
类Book实现Cloneable接口并重写clone方法
public class Book implements Cloneable {
private String name;//书名
private Date publishDate;//发布日期
public Book() {
}
public Book(String name, Date publishDate) {
this.name = name;
this.publishDate = publishDate;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Date getPublishDate() {
return publishDate;
}
public void setPublishDate(Date publishDate) {
this.publishDate = publishDate;
}
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
@Override
public String toString() {
return "Book{" +
"name='" + name + '\'' +
", publishDate=" + publishDate +
'}';
}
}
测试类
public class Test {
public static void main(String[] args) throws CloneNotSupportedException {
String name = "Java 设计模式";
Date publishDate = new Date();
Book book = new Book(name, publishDate);
//克隆出对象
Book clone = (Book) book.clone();
System.out.println(book);
System.out.println(book.hashCode());
System.out.println(clone);
System.out.println(clone.hashCode());
System.out.println("==================");
publishDate.setTime(publishDate.getTime() + 2000);//修改时间
System.out.println(book);
System.out.println(book.hashCode());
System.out.println(clone);
System.out.println(clone.hashCode());
/**
* 测试结果:修改了时间,源对象和克隆的对象的时间,都发生了改变
* Book{name='Java 设计模式', publishDate=Wed Dec 29 12:17:33 CST 2021}
* 2093631819
* Book{name='Java 设计模式', publishDate=Wed Dec 29 12:17:33 CST 2021}
* 2074407503
* ==================
* Book{name='Java 设计模式', publishDate=Wed Dec 29 12:17:35 CST 2021}
* 2093631819
* Book{name='Java 设计模式', publishDate=Wed Dec 29 12:17:35 CST 2021}
* 2074407503
*/
}
}
测试结果
Book{name='Java 设计模式', publishDate=Wed Dec 29 12:17:33 CST 2021}
2093631819
Book{name='Java 设计模式', publishDate=Wed Dec 29 12:17:33 CST 2021}
2074407503
==================
Book{name='Java 设计模式', publishDate=Wed Dec 29 12:17:35 CST 2021}
2093631819
Book{name='Java 设计模式', publishDate=Wed Dec 29 12:17:35 CST 2021}
2074407503
深克隆
类Book实现Cloneable接口并重写clone方法
public class Book implements Cloneable {
private String name;//书名
private Date publishDate;//发布日期
public Book() {
}
public Book(String name, Date publishDate) {
this.name = name;
this.publishDate = publishDate;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Date getPublishDate() {
return publishDate;
}
public void setPublishDate(Date publishDate) {
this.publishDate = publishDate;
}
@Override
protected Object clone() throws CloneNotSupportedException {
Object obj = super.clone();
Book book = (Book) obj;
//将源对象的属性也进行克隆,即深克隆
book.publishDate = (Date) book.publishDate.clone();
return obj;
}
@Override
public String toString() {
return "Book{" +
"name='" + name + '\'' +
", publishDate=" + publishDate +
'}';
}
}
测试类
public class Test {
public static void main(String[] args) throws CloneNotSupportedException {
String name = "Java 设计模式";
Date publishDate = new Date();
Book book = new Book(name, publishDate);
//克隆出对象
Book clone = (Book) book.clone();
System.out.println(book);
System.out.println(book.hashCode());
System.out.println(clone);
System.out.println(clone.hashCode());
System.out.println("==================");
publishDate.setTime(publishDate.getTime() + 2000);
System.out.println(book);
System.out.println(book.hashCode());
System.out.println(clone);
System.out.println(clone.hashCode());
/**
* 测试结果:修改了时间,源对象的时间,发生了改变,但克隆的对象的时间没有改变
* Book{name='Java 设计模式', publishDate=Wed Dec 29 12:41:47 CST 2021}
* 2093631819
* Book{name='Java 设计模式', publishDate=Wed Dec 29 12:41:47 CST 2021}
* 2074407503
* ==================
* Book{name='Java 设计模式', publishDate=Wed Dec 29 12:41:49 CST 2021}
* 2093631819
* Book{name='Java 设计模式', publishDate=Wed Dec 29 12:41:47 CST 2021}
* 2074407503
*/
}
}
测试结果
Book{name='Java 设计模式', publishDate=Wed Dec 29 12:41:47 CST 2021}
2093631819
Book{name='Java 设计模式', publishDate=Wed Dec 29 12:41:47 CST 2021}
2074407503
==================
Book{name='Java 设计模式', publishDate=Wed Dec 29 12:41:49 CST 2021}
2093631819
Book{name='Java 设计模式', publishDate=Wed Dec 29 12:41:47 CST 2021}
2074407503
程序员地瓜哥的小屋 文章被收录于专栏
IT相关技术学习