原型模式

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.浅拷贝与深拷贝:浅拷贝只是将基本类型进行拷贝,引用类型变量还是指向被拷贝对象内存地址。不管是修改被拷贝对象还是拷贝产生对象的引用类型变量,都会对这两个对象造成影响。如果想将两个对象的引用类型变量完全隔离,需要使用深拷贝,也就是将引用类型的变量也中的属性进行一次拷贝。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值