java:
package prototype;
import java.util.List;
public class ExamPaper implements Cloneable{
String subject;
int maxScore;
List<String> questions;
public ExamPaper(String subject, int maxScore, List<String> questions) {
super();
this.subject = subject;
this.maxScore = maxScore;
this.questions = questions;
System.out.println("调用构造方法");
}
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
public String getSubject() {
return subject;
}
public void setSubject(String subject) {
this.subject = subject;
}
public int getMaxScore() {
return maxScore;
}
public void setMaxScore(int maxScore) {
this.maxScore = maxScore;
}
public List<String> getQuestions() {
return questions;
}
public void setQuestions(List<String> questions) {
this.questions = questions;
}
@Override
public String toString() {
return "ExamPaper [subject=" + subject + ", maxScore=" + maxScore + ", questions=" + questions + "]";
}
}
package prototype;
import java.util.ArrayList;
import java.util.List;
public class PrototypeTest {
public static void main(String[] args) throws CloneNotSupportedException {
String subject="数学";
int maxScore=100;
String question1="1+1=?";
String question2="1+2=?";
List<String> questions=new ArrayList<>();
questions.add(question1);
questions.add(question2);
normalConstruction(subject,maxScore,questions);
shallowCopy(subject,maxScore,questions);
}
/**
* 生成新对象通过构造方法
* @param subject
* @param maxScore
* @param questions
*/
private static void normalConstruction(String subject,int maxScore,List<String> questions) {
for(int i=0;i<10;i++) {
ExamPaper examPaper=new ExamPaper(subject,maxScore,questions);
System.out.println(examPaper.toString());
}
}
/**
* 浅拷贝:生成新对象不需要通过构造方法
* @param subject
* @param maxScore
* @param questions
* @throws CloneNotSupportedException
*/
private static void shallowCopy(String subject,int maxScore,List<String> questions) throws CloneNotSupportedException {
ExamPaper examPaper1=new ExamPaper(subject,maxScore,questions);
for(int i=0;i<10;i++) {
ExamPaper examPaper2=(ExamPaper) examPaper1.clone();
System.out.println(examPaper2.toString());
}
}
}
打印结果:
调用构造方法
ExamPaper [subject=数学, maxScore=100, questions=[1+1=?, 1+2=?]]
调用构造方法
ExamPaper [subject=数学, maxScore=100, questions=[1+1=?, 1+2=?]]
调用构造方法
ExamPaper [subject=数学, maxScore=100, questions=[1+1=?, 1+2=?]]
调用构造方法
ExamPaper [subject=数学, maxScore=100, questions=[1+1=?, 1+2=?]]
调用构造方法
ExamPaper [subject=数学, maxScore=100, questions=[1+1=?, 1+2=?]]
调用构造方法
ExamPaper [subject=数学, maxScore=100, questions=[1+1=?, 1+2=?]]
调用构造方法
ExamPaper [subject=数学, maxScore=100, questions=[1+1=?, 1+2=?]]
调用构造方法
ExamPaper [subject=数学, maxScore=100, questions=[1+1=?, 1+2=?]]
调用构造方法
ExamPaper [subject=数学, maxScore=100, questions=[1+1=?, 1+2=?]]
调用构造方法
ExamPaper [subject=数学, maxScore=100, questions=[1+1=?, 1+2=?]]
调用构造方法
ExamPaper [subject=数学, maxScore=100, questions=[1+1=?, 1+2=?]]
ExamPaper [subject=数学, maxScore=100, questions=[1+1=?, 1+2=?]]
ExamPaper [subject=数学, maxScore=100, questions=[1+1=?, 1+2=?]]
ExamPaper [subject=数学, maxScore=100, questions=[1+1=?, 1+2=?]]
ExamPaper [subject=数学, maxScore=100, questions=[1+1=?, 1+2=?]]
ExamPaper [subject=数学, maxScore=100, questions=[1+1=?, 1+2=?]]
ExamPaper [subject=数学, maxScore=100, questions=[1+1=?, 1+2=?]]
ExamPaper [subject=数学, maxScore=100, questions=[1+1=?, 1+2=?]]
ExamPaper [subject=数学, maxScore=100, questions=[1+1=?, 1+2=?]]
ExamPaper [subject=数学, maxScore=100, questions=[1+1=?, 1+2=?]]
小结:
1.原型模式一般用于大量创建对象的场景,节省系统内存;
2.浅拷贝与深拷贝:浅拷贝只是将基本类型进行拷贝,引用类型变量还是指向被拷贝对象内存地址。不管是修改被拷贝对象还是拷贝产生对象的引用类型变量,都会对这两个对象造成影响。如果想将两个对象的引用类型变量完全隔离,需要使用深拷贝,也就是将引用类型的变量也中的属性进行一次拷贝。