简历复印--原型模式

发布于:2024-04-05 ⋅ 阅读:(162) ⋅ 点赞:(0)

1.1 夸张的简历

        简历的打印。"对编程来说,简单的复制粘贴极有可能造成重复代码的灾难。我所说的意思你根本还没听懂。那就以刚才的例子,我出个需求你写写看,要求有一个简历类,必须要有姓名,可以设置性别和年龄,可以设置工作经历。最终我需要写三份简历。"

1.2 简历代码初步实现

package code.chapter9.prototype1;

public class Test {

	public static void main(String[] args){

		System.out.println("**********************************************");		
		System.out.println("《大话设计模式》代码样例");
		System.out.println();		

		Resume resume1 = new Resume("大鸟");
		resume1.setPersonalInfo("男","29");
		resume1.setWorkExperience("1998-2000","XX公司");
		
		Resume resume2 = new Resume("大鸟");
		resume2.setPersonalInfo("男","29");
		resume2.setWorkExperience("1998-2000","XX公司");
		
		Resume resume3 = new Resume("大鸟");
		resume3.setPersonalInfo("男","29");
		resume3.setWorkExperience("1998-2000","XX公司");

		resume1.display();
		resume2.display();
		resume3.display();


		System.out.println();
		System.out.println("**********************************************");

	}
}

//简历类
class Resume  {
	private String name;
	private String sex;
	private String age;
	private String timeArea;
	private String company;

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

	//设置个人信息
	public void setPersonalInfo(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(this.name +" "+this.sex +" "+this.age);
		System.out.println("工作经历 "+this.timeArea +" "+this.company);
	}
}




        "很好,这其实就是当年我手写简历的时代的代码。三份简历需要三次实例化。你觉得这样的客户端代码是不是很麻烦?如果要二十份,你就需要二十次实例化。""是呀,而且如果我写错了一个字,比如1998年改成1999年,那就要改二十次。""你为什么不这样写呢?

package code.chapter9.prototype2;

public class Test {

	public static void main(String[] args){

		System.out.println("**********************************************");		
		System.out.println("《大话设计模式》代码样例");
		System.out.println();		

		Resume resume1 = new Resume("大鸟");
		resume1.setPersonalInfo("男","29");
		resume1.setWorkExperience("1998-2000","XX公司");
		
		Resume resume2 = resume1;
		
		Resume resume3 = resume1;

		resume1.display();
		resume2.display();
		resume3.display();


		System.out.println();
		System.out.println("**********************************************");

	}
}

//简历类
class Resume  {
	private String name;
	private String sex;
	private String age;
	private String timeArea;
	private String company;

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

	//设置个人信息
	public void setPersonalInfo(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(this.name +" "+this.sex +" "+this.age);
		System.out.println("工作经历 "+this.timeArea +" "+this.company);
	}
}

        "哈,这其实是传引用,而不是传值,这样做就如同是在resume2纸张和resume3纸张上写着简历在resume1处一样,没有实际的内容。"

1.3 原型模式

原型模式(Prototype),用原型实例指定创建对象的种类,并且通过复制这些原型创建新的对象。[DP]

原型模式(Prototype)结构图

"原型模式其实就是从一个对象再创建另外一个可定制的对象,而且不需要知道任何创建的细节。我们来看看基本的原型模式代码。"

package code.chapter9.prototype0;

public class Test {

	public static void main(String[] args){

		System.out.println("**********************************************");		
		System.out.println("《大话设计模式》代码样例");
		System.out.println();		

		ConcretePrototype p1 = new ConcretePrototype("编号123456");
		System.out.println("原ID:"+ p1.getID());

		ConcretePrototype c1 = (ConcretePrototype)p1.clone();
		System.out.println("克隆ID:"+ c1.getID());

		System.out.println();
		System.out.println("**********************************************");

	}
}

//原型类
abstract class Prototype implements Cloneable {
	private String id;

	public Prototype(String id){
		this.id=id;
	}

	public String getID(){
		return this.id;
	}

	//原型模式的关键就是有这样一个clone方法
	public Object clone(){
		Object object = null;
		try {
			object = super.clone();
		}
		catch(CloneNotSupportedException exception){
			System.err.println("Clone异常。");
		}
		return object;
	}
}

//具体原型类
class ConcretePrototype extends Prototype{

	public ConcretePrototype(String id){
		super(id);
	}

}



        "哦,这样就可以不用实例化ConcretePrototype了,直接克隆就行了?
        "说得没错,就是这样的。但对于Java而言,那个原型抽象类Prototype是用不着的,因为克隆实在是太常用了,所以Java提供了Cloneable接口,其中就是唯一的一个方法clone(),这样你就只需要实现这个接口就可以完成原型模式了。现在明白了?去改我们的'简历原型'代码吧。"

1.4 简历的原型实现

package code.chapter9.prototype3;

public class Test {

	public static void main(String[] args){

		System.out.println("**********************************************");		
		System.out.println("《大话设计模式》代码样例");
		System.out.println();		

		Resume resume1 = new Resume("大鸟");
		resume1.setPersonalInfo("男","29");
		resume1.setWorkExperience("1998-2000","XX公司");
		
		Resume resume2 = resume1.clone();
		resume2.setWorkExperience("2000-2003","YY集团");
		
		Resume resume3 = resume1.clone();
		resume3.setPersonalInfo("男","24");

		resume1.display();
		resume2.display();
		resume3.display();


		System.out.println();
		System.out.println("**********************************************");

	}
}

//简历类
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 setPersonalInfo(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(this.name +" "+this.sex +" "+this.age);
		System.out.println("工作经历 "+this.timeArea +" "+this.company);
	}

	//实现了clone接口方法
	public Resume clone(){
		Resume object = null;
		try {
			object = (Resume)super.clone();
		}
		catch(CloneNotSupportedException exception){
			System.err.println("Clone异常。");
		}
		return object;
	}
}

        这样一来,客户端的代码就清爽很多了,而且你要是想改某份简历,只需要对这份简历做一定的修改就可以了,不会影响到其他简历,相同的部分就不用再重复了。不过不知道这样子对性能是不是有大的提高呢?"
        "当然是大大提高,你想呀,每new一次,都需要执行一次构造函数,如果构造函数的执行时间很长,那么多次执行这个初始化操作就实在太低效了。一般在初始化的信息不发生变化的情况下,克隆是最好的办法。这既隐藏了对象创建的细节,又对性能是大大的提高,何乐而不为呢?"
        "的确,我开始也没感觉到它的好,听你这么一说,感觉这样做的好处还真不少,它等于是不用重新初始化对象,而是动态地获得对象运行时的状态。这个模式真的很不错。

1.5 浅复制与深复制

        "别高兴得太早,如果我现在要改需求,你就又头疼了。你现在'简历'对象里的数据都是String型的,而String是一种拥有值类型特点的特殊引用类型,super.clone()方法是这样,如果字段是值类型的,则对该字段执行逐位复制,如果字段是引用类型,则复制引用但不复制引用的对象;因此,原始对象及其副本引用同一对象。什么意思呢?就是说如果你的'简历'类当中有对象引用,那么引用的对象数据是不会被克隆过来的。"
        "没太听懂,为什么不能一同复制过来呢?"

        "举个例子你就明白了,你现在的'简历'类当中有一个'设置工作经历'的方法,在现实设计当中,一般会再有一个'工作经历'类,当中有'时间区间'和'公司名称'等属性,'简历'类直接调用这个对象即可。

代码结构图

package code.chapter9.prototype4;

public class Test {

	public static void main(String[] args){

		System.out.println("**********************************************");		
		System.out.println("《大话设计模式》代码样例");
		System.out.println();		

		Resume resume1 = new Resume("大鸟");
		resume1.setPersonalInfo("男","29");
		resume1.setWorkExperience("1998-2000","XX公司");
		
		Resume resume2 = resume1.clone();
		resume2.setWorkExperience("2000-2003","YY集团");
		
		Resume resume3 = resume1.clone();
		resume3.setPersonalInfo("男","24");
		resume3.setWorkExperience("2003-2006","ZZ公司");

		resume1.display();
		resume2.display();
		resume3.display();


		System.out.println();
		System.out.println("**********************************************");

	}
}

//简历类
class Resume implements Cloneable {
	private String name;
	private String sex;
	private String age;
	private WorkExperience work;		//声明一个工作经历的对象
	public Resume(String name){
		this.name = name;
		this.work = new WorkExperience();//对这个工作经历对象实例化
	}
	//设置个人信息
	public void setPersonalInfo(String sex,String age){
		this.sex=sex;
		this.age=age;
	}
	//设置工作经历
	public void setWorkExperience(String timeArea,String company){
		this.work.setTimeArea(timeArea);//给工作经历实例的时间范围赋值
		this.work.setCompany(company);	//给工作经历实例的公司赋值
	}
	//展示简历
	public void display(){
		System.out.println(this.name +" "+this.sex +" "+this.age);
		System.out.println("工作经历 "+this.work.getTimeArea() +" "+this.work.getCompany());
	}
	public Resume clone(){
		Resume object = null;
		try {
			object = (Resume)super.clone();
		}
		catch(CloneNotSupportedException exception){
			System.err.println("Clone异常。");
		}
		return object;
	}
}

//工作经历类
class WorkExperience {

	//工作时间范围
	private String timeArea;
	public String getTimeArea(){
		return this.timeArea;
	}
	public void setTimeArea(String value){
		this.timeArea=value;
	}

	//所在公司
	private String company;
	public String getCompany(){
		return this.company;
	}
	public void setCompany(String value){
		this.company=value;
	}
}






        结果显示,实际结果与期望结果并不符合,前两次的工作经历数据被最后一次数据给覆盖了。

        "通过写代码,并且去查了一下Java关于Cloneable的帮助,我大概知道你的意思了,由于它是浅表复制,所以对于值类型,没什么问题,对引用类型,就只是复制了引用,对引用的对象还是指向了原来的对象,所以就会出现我给resume1、resume2、resume3三个引用设置'工作经历',但却同时看到三个引用都是最后一次设置,因为三个引用都指向了同一个对象。"
        "你写的和说的都很好,就是这个原因,这叫作'浅复制',被复制对象的所有变量都含有与原来的对象相同的值,而所有的对其他对象的引用都仍然指向原来的对象。但我们可能更需要这样的一种需求,把要复制的对象所引用的对象都复制一遍。比如刚才的例子,我们希望是resume1、resume2、resume3三个引用的对象是不同的,复制时就一变二,二变三,此时,我们就叫这种方式为'深复制',深复制把引用对象的变量指向复制过的新对象,而不是原有的被引用的对象。"
        "那如果'简历'对象引用了'工作经历','工作经历'再引用'公司','公司'再引用'职位'……这样一个引用一个,很多层,如何办?"
        "这的确是个很难回答的问题,深复制要深入到多少层,需要事先就考虑好,而且要当心出现循环引用的问题,需要小心处理,这里比较复杂,可以慢慢研究。就现在这个例子,问题应该不大,深入到第一层就可以了。"

1.6 简历的深复制实现

代码结构图

package code.chapter9.prototype5;

public class Test {

	public static void main(String[] args){

		System.out.println("**********************************************");		
		System.out.println("《大话设计模式》代码样例");
		System.out.println();		

		Resume resume1 = new Resume("大鸟");
		resume1.setPersonalInfo("男","29");
		resume1.setWorkExperience("1998-2000","XX公司");
		
		Resume resume2 = resume1.clone();
		resume2.setWorkExperience("2000-2003","YY集团");
		
		Resume resume3 = resume1.clone();
		resume3.setPersonalInfo("男","24");
		resume3.setWorkExperience("2003-2006","ZZ公司");

		resume1.display();
		resume2.display();
		resume3.display();


		System.out.println();
		System.out.println("**********************************************");

	}
}

//简历类
class Resume implements Cloneable {
	private String name;
	private String sex;
	private String age;
	private WorkExperience work;
	public Resume(String name){
		this.name = name;
		this.work = new WorkExperience();
	}
	//设置个人信息
	public void setPersonalInfo(String sex,String age){
		this.sex=sex;
		this.age=age;
	}
	//设置工作经历
	public void setWorkExperience(String timeArea,String company){
		this.work.setTimeArea(timeArea);//给工作经历实例的时间范围赋值
		this.work.setCompany(company);	//给工作经历实例的公司赋值
	}
	//展示简历
	public void display(){
		System.out.println(this.name +" "+this.sex +" "+this.age);
		System.out.println("工作经历 "+this.work.getTimeArea() +" "+this.work.getCompany());
	}
	public Resume clone(){
		Resume object = null;
		try {
			object = (Resume)super.clone();
			object.work = this.work.clone();
		}
		catch(CloneNotSupportedException exception){
			System.err.println("Clone异常。");
		}
		return object;
	}
}

//工作经历类
class WorkExperience implements Cloneable {
	//工作时间范围
	private String timeArea;
	public String getTimeArea(){
		return this.timeArea;
	}
	public void setTimeArea(String value){
		this.timeArea=value;
	}

	//所在公司
	private String company;
	public String getCompany(){
		return this.company;
	}
	public void setCompany(String value){
		this.company=value;
	}

	public WorkExperience clone(){
		WorkExperience object = null;
		try {
			object = (WorkExperience)super.clone();
		}
		catch(CloneNotSupportedException exception){
			System.err.println("Clone异常。");
		}
		return object;
	}
}






1.7 复制简历vs.手写求职信

"如果是写代码,我当然会鼓励你去应用原型模式简化代码,优化设计。但对于求职,你是愿意你的简历和求职信倍受重视呢?还是愿意和所有的毕业生一样千篇一律毫无新意地碰运气?"


网站公告

今日签到

点亮在社区的每一天
去签到