Java--设计模式之原型模式

原型模式,用原型实例指定创建对象的种类,并且通过拷贝这些原型创建新的对象。通俗的说法就是从一个对象再创建另外一个可定制的对象,而且不需要知道任何创建的细节。

先看一个“钱复制”的例子

1、简历类

public class Resume implements Cloneable{
	private String name;
	private String sex;
	private String age;
	private String timeArea;
	private String company;
	public Resume(String name){
		this.name = name;
	}
	public void setPersonInfo(String sex,String age){
		this.sex = sex;
		this.age = age;
	}
	public void setWorkExperience(String timeArea,String company){
		this.timeArea = timeArea;
		this.company = company;
	}
	public void display(){
		System.out.println(name+":"+sex+":"+age);
		System.out.println("工作经历"+timeArea+":"+company);
	}
	@Override
	protected Object clone(){
		Object o = null;
		try {
			o = super.clone();
		} catch (CloneNotSupportedException e) {
			e.printStackTrace();
		}
		return o;
	}
}
2、测试
public class Test {
	public static void main(String[] args) {
		Resume a = new Resume("大鸟");
		a.setPersonInfo("man", "22");
		a.setWorkExperience("2011-12-22", "XX公司");
		Resume b = (Resume)a.clone();
		b.setWorkExperience("2012-11-22", "YY公司");
		Resume c = (Resume)a.clone();
		c.setPersonInfo("man", "24");
		a.display();
		b.display();
		c.display();
	}
}
上面的例子是“浅复制”,让我们把工作经验用一个单独的类来表示,看看结果如何,更改简历类

3、更改后的简历类

public class Resume implements Cloneable{
	private String name;
	private String sex;
	private String age;
	private WorkExperience work;
	public Resume(String name){
		this.name = name;
		work = new WorkExperience();
	}
	public void setPersonInfo(String sex,String age){
		this.sex = sex;
		this.age = age;
	}
	public void setWorkExperience(String workData,String company){
		work.setWorkData(workData);
		work.setCompany(company);
	}
	public void display(){
		System.out.println(name+":"+sex+":"+age);
		System.out.println("工作经历"+work.getWorkData()+":"+work.getCompany());
	}
	@Override
	protected Object clone(){
		Object o = null;
		try {
			o = super.clone();
		} catch (CloneNotSupportedException e) {
			e.printStackTrace();
		}
		return o;
	}
}
浅复制,被复制对象的所有变量都含有与原来的对象相同的值,而所有的对其他对象的引用都仍然指向原来的对象。还有一种复制是深复制,深复制把引用对象的变量指向复制过的新对象,而不是原有的被引用的对象。下面我们来看看如何实现简历的深复制。

4、更改后的工作经验类

public class WorkExperience implements Cloneable{
	private String workData;
	private String company;
	public String getWorkData() {
		return workData;
	}
	public void setWorkData(String workData) {
		this.workData = workData;
	}
	public String getCompany() {
		return company;
	}
	public void setCompany(String company) {
		this.company = company;
	}
	@Override
	protected Object clone(){
		Object o = null;
		try {
			o = super.clone();
		} catch (CloneNotSupportedException e) {
			e.printStackTrace();
		}
		return o;
	}
}
5、更改后的简历类
public class Resume implements Cloneable{
	private String name;
	private String sex;
	private String age;
	private WorkExperience work;
	public Resume(String name){
		this.name = name;
		work = new WorkExperience();
	}
	private Resume(WorkExperience work){
		this.work = (WorkExperience)work.clone();
	}
	public void setPersonInfo(String sex,String age){
		this.sex = sex;
		this.age = age;
	}
	public void setWorkExperience(String workData,String company){
		work.setWorkData(workData);
		work.setCompany(company);
	}
	public void display(){
		System.out.println(name+":"+sex+":"+age);
		System.out.println("工作经历"+work.getWorkData()+":"+work.getCompany());
	}
	@Override
	protected Object clone(){
		Resume obj = new Resume(this.work);
		obj.name = this.name;
		obj.age = this.age;
		obj.sex = this.sex;
		return obj;
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值