`

大话设计模式九:原型模式(简历复印)

 
阅读更多

浅复制:被复制对象的所有变量都含有与原来的对象相同的值,而所有的其它对象的引用都仍然指向原来的对象。

//工作经历类
public class WorkExperience
{
	private String	workDate;
	private String	company;

	public String getWorkDate()
	{
		return workDate;
	}

	public void setWorkDate(String workDate)
	{
		this.workDate = workDate;
	}

	public String getCompany()
	{
		return company;
	}

	public void setCompany(String company)
	{
		this.company = company;
	}
}
//简历类
public class Resume implements Cloneable
{
	private String			name;
	private String			sex;
	private String			age;
	private WorkExperience	work	= new WorkExperience();

	public Resume(String name)
	{
		this.name = name;
	}

	public void setPersonalInfo(String sex, String age)
	{
		this.sex = sex;
		this.age = age;
	}

	public void setWorkExperience(String workDate, String company)
	{
		work.setWorkDate(workDate);
		work.setCompany(company);
	}

	public void display()
	{
		System.out.println(name + "		" + sex + "		" + age);
		System.out.println("工作经历:" + work.getWorkDate() + "	"
				+ work.getCompany());
	}

	public Resume clone() throws CloneNotSupportedException
	{
		return (Resume) super.clone();
	}
}
//客户端代码
public class Main
{
	public static void main(String[] args) throws CloneNotSupportedException
	{
		Resume a = new Resume("大鸟");
		a.setPersonalInfo("男", "29");
		a.setWorkExperience("1998-2000", "XX公司");

		Resume b = a.clone();
		b.setWorkExperience("1998-2006", "YY企业");

		Resume c = a.clone();
		c.setWorkExperience("1998-2003", "ZZ企业");

		a.display();
		b.display();
		c.display();
	}
}

结果显示:
大鸟		男		29
工作经历:1998-2003	ZZ企业
大鸟		男		29
工作经历:1998-2003	ZZ企业
大鸟		男		29
工作经历:1998-2003	ZZ企业


深复制:把所有引用对象的变量指向复制过的新对象,而不是原有的被引用对象。

//工作经历
public class WorkExperience implements Cloneable
{
	private String	workDate;
	private String	company;

	public String getWorkDate()
	{
		return workDate;
	}

	public void setWorkDate(String workDate)
	{
		this.workDate = workDate;
	}

	public String getCompany()
	{
		return company;
	}

	public void setCompany(String company)
	{
		this.company = company;
	}

	public WorkExperience clone()
	{
		try
		{
			return (WorkExperience) super.clone();
		}
		catch (CloneNotSupportedException e)
		{
			e.printStackTrace();
		}
		return null;
	}
}
//简历类
public class Resume implements Cloneable
{
	private String			name;
	private String			sex;
	private String			age;
	private WorkExperience	work	= new WorkExperience();

	public Resume(String name)
	{
		this.name = name;
	}

	public void setPersonalInfo(String sex, String age)
	{
		this.sex = sex;
		this.age = age;
	}

	public void setWorkExperience(String workDate, String company)
	{
		work.setWorkDate(workDate);
		work.setCompany(company);
	}

	public void display()
	{
		System.out.println(name + "		" + sex + "		" + age);
		System.out.println("工作经历:" + work.getWorkDate() + "	"
				+ work.getCompany());
	}

	public Resume clone()
	{
		Resume obj = new Resume(name);
		obj.sex = this.sex;
		obj.age = this.age;
		obj.work = work.clone();

		return obj;
	}
}
//客户端代码保持不变
public class Main
{
	public static void main(String[] args)
	{
		Resume a = new Resume("大鸟");
		a.setPersonalInfo("男", "29");
		a.setWorkExperience("1998-2000", "XX公司");

		Resume b = a.clone();
		b.setWorkExperience("1998-2006", "YY企业");

		Resume c = a.clone();
		c.setWorkExperience("1998-2003", "ZZ企业");

		a.display();
		b.display();
		c.display();
	}
}

结果显示为:
大鸟		男		29
工作经历:1998-2000	XX公司
大鸟		男		29
工作经历:1998-2006	YY企业
大鸟		男		29
工作经历:1998-2003	ZZ企业


一般在初始化的信息不发生变化的情况下,克隆是最好的办法。这既隐藏了对象创建的细节,又对性能是大大的提高。


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics