共计 3815 个字符,预计需要花费 10 分钟才能阅读完成。
原型模式简介
- 模式属于创立型模式,它提供了一种创建对象的最佳形式。
- 用于创立反复的对象,同时又能保障性能。
- 这种模式是实现了一个原型接口,该接口用于创立以后对象的克隆。当间接创建对象的代价比拟大时,则采纳这种模式。
- 例如,一个对象须要在一个高代价的数据库操作之后被创立。咱们能够缓存该对象,在下一个申请时返回它的克隆,在须要的时候更新数据库,以此来缩小数据库调用。
浅克隆
类 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
正文完