不来了解一下程序的CV大法吗?——原型模式(Prototype)

目录

原型模式介绍:

浅克隆小案例

疑问:

不使用clone方法

结论:

应用场景

问题:

实现深克隆方法1

实现深克隆方法2


原型模式介绍:
 

程序的CV大法就是原型模式

 

在我们写代码,重复率高的时候,应该经常运用CV大法。

其中一种程序CV大法就是JDK里边专门为我们提供的方法——clone()。

这是JDK里封装的用C写的方法,直接在硬盘里边复制。

浅克隆小案例

下边咱们来看一个小案例:

假设我是“天网”组织的头头,可以复制“于氏一剑”。

首先写处“于氏一剑”的武器“鱼肠剑”

package 原型模式.使用原型.浅克隆;

public class 鱼肠剑 {
	String name;

	public 鱼肠剑(String name) {
		this.name = name;
	}
}

再写个“于氏一剑”的原型,并复写clone方法

package 原型模式.使用原型.浅克隆;

public class 于氏一剑 implements Cloneable {
	String name;
	int age;
	int sex;
	
	
	String nationality;
	String skinColour;
	int height;
	int weight;
	String hairstyle;
	String hairColor;
	String eye;
	String nose;
	String ear;
	String most;
	String neck;
	鱼肠剑 sword;
	
	public 于氏一剑() {
		
	}
	public 于氏一剑(String name, int age, int sex, String nationality, String skinColour, int height, int weight,
			String hairstyle, String hairColor, String eye, String nose, String ear, String most, String neck,
			鱼肠剑 sword) {
		super();
		this.name = name;
		this.age = age;
		this.sex = sex;
		this.nationality = nationality;
		this.skinColour = skinColour;
		this.height = height;
		this.weight = weight;
		this.hairstyle = hairstyle;
		this.hairColor = hairColor;
		this.eye = eye;
		this.nose = nose;
		this.ear = ear;
		this.most = most;
		this.neck = neck;
		this.sword = sword;
	}
	
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public int getSex() {
		return sex;
	}
	public void setSex(int sex) {
		this.sex = sex;
	}
	public String getNationality() {
		return nationality;
	}
	public void setNationality(String nationality) {
		this.nationality = nationality;
	}
	public String getSkinColour() {
		return skinColour;
	}
	public void setSkinColour(String skinColour) {
		this.skinColour = skinColour;
	}
	public int getHeight() {
		return height;
	}
	public void setHeight(int height) {
		this.height = height;
	}
	public int getWeight() {
		return weight;
	}
	public void setWeight(int weight) {
		this.weight = weight;
	}
	public String getHairstyle() {
		return hairstyle;
	}
	public void setHairstyle(String hairstyle) {
		this.hairstyle = hairstyle;
	}
	public String getHairColor() {
		return hairColor;
	}
	public void setHairColor(String hairColor) {
		this.hairColor = hairColor;
	}
	public String getEye() {
		return eye;
	}
	public void setEye(String eye) {
		this.eye = eye;
	}
	public String getNose() {
		return nose;
	}
	public void setNose(String nose) {
		this.nose = nose;
	}
	public String getEar() {
		return ear;
	}
	public void setEar(String ear) {
		this.ear = ear;
	}
	public String getMost() {
		return most;
	}
	public void setMost(String most) {
		this.most = most;
	}
	public String getNeck() {
		return neck;
	}
	public void setNeck(String neck) {
		this.neck = neck;
	}
	public 鱼肠剑 getSword() {
		return sword;
	}
	public void setSword(于氏一剑 master) {
		this.sword = sword;
	}
	@Override
	public String toString() {
		return "People [name=" + name + ", age=" + age + ", sex=" + sex + ", nationality=" + nationality
				+ ", skinColour=" + skinColour + ", height=" + height + ", weight=" + weight + ", hairstyle="
				+ hairstyle + ", hairColor=" + hairColor + ", eye=" + eye + ", nose=" + nose + ", ear=" + ear
				+ ", most=" + most + ", neck=" + neck + ", sword=" + sword.hashCode() + "]";
	}
	// 克隆该实例,使用默认的clone方法来完成
	@Override
	protected Object clone()  {
		
		于氏一剑 people = null;
		try {
			people = (于氏一剑)super.clone();
		} catch (Exception e) {
			// TODO: handle exception
			System.out.println(e.getMessage());
		}
		// TODO Auto-generated method stub
		return people;
	}
	
	
}

接下来,就开始让我这个“天网”头头复制他吧。

package 原型模式.使用原型.浅克隆;

public class 我 {

	public static void main(String[] args) {
		于氏一剑 p1 =new 于氏一剑(
				"于氏一剑一", 
				20, 
				1, 
				"中国", 
				"黄色", 
				172, 
				170, 
				"平头", 
				"黑色", 
				"棕色眼睛", 
				"大鼻子", 
				"小耳朵", 
				"厚嘴唇", 
				"粗脖子", 
				new 鱼肠剑("鱼肠剑"));
		System.out.println(p1.toString());
		
		于氏一剑 p2 = (于氏一剑)p1.clone();
		p2.setName("于氏一剑二");
		System.out.println(p2.toString());

		于氏一剑 p3 = (于氏一剑)p1.clone();
		p3.setName("于氏一剑三");
		System.out.println(p3.toString());
		
		于氏一剑 p4 = (于氏一剑)p1.clone();
		p4.setName("于氏一剑四");
		System.out.println(p4.toString());
		
		于氏一剑 p5 = (于氏一剑)p1.clone();
		p5.setName("于氏一剑五");
		System.out.println(p5.toString());
		
		于氏一剑 p6 = (于氏一剑)p1.clone();
		p6.setName("于氏一剑六");
		System.out.println(p6.toString());
		
		于氏一剑 p7 = (于氏一剑)p1.clone();
		p7.setName("于氏一剑七");
		System.out.println(p7.toString());
		
		于氏一剑 p8 = (于氏一剑)p1.clone();
		p8.setName("于氏一剑八");
		System.out.println(p8.toString());
		
		于氏一剑 p9 = (于氏一剑)p1.clone();
		p9.setName("于氏一剑九");
		System.out.println(p9.toString());
		
		于氏一剑 p10 = (于氏一剑)p1.clone();
		p10.setName("于氏一剑十");
		System.out.println(p10.toString());
		
	}
	
}

运行结果:

People [name=于氏一剑一, age=20, sex=1, nationality=中国, skinColour=黄色, height=172, weight=170, hairstyle=平头, hairColor=黑色, eye=棕色眼睛, nose=大鼻子, ear=小耳朵, most=厚嘴唇, neck=粗脖子, sword=366712642]
People [name=于氏一剑二, age=20, sex=1, nationality=中国, skinColour=黄色, height=172, weight=170, hairstyle=平头, hairColor=黑色, eye=棕色眼睛, nose=大鼻子, ear=小耳朵, most=厚嘴唇, neck=粗脖子, sword=366712642]
People [name=于氏一剑三, age=20, sex=1, nationality=中国, skinColour=黄色, height=172, weight=170, hairstyle=平头, hairColor=黑色, eye=棕色眼睛, nose=大鼻子, ear=小耳朵, most=厚嘴唇, neck=粗脖子, sword=366712642]
People [name=于氏一剑四, age=20, sex=1, nationality=中国, skinColour=黄色, height=172, weight=170, hairstyle=平头, hairColor=黑色, eye=棕色眼睛, nose=大鼻子, ear=小耳朵, most=厚嘴唇, neck=粗脖子, sword=366712642]
People [name=于氏一剑五, age=20, sex=1, nationality=中国, skinColour=黄色, height=172, weight=170, hairstyle=平头, hairColor=黑色, eye=棕色眼睛, nose=大鼻子, ear=小耳朵, most=厚嘴唇, neck=粗脖子, sword=366712642]
People [name=于氏一剑六, age=20, sex=1, nationality=中国, skinColour=黄色, height=172, weight=170, hairstyle=平头, hairColor=黑色, eye=棕色眼睛, nose=大鼻子, ear=小耳朵, most=厚嘴唇, neck=粗脖子, sword=366712642]
People [name=于氏一剑七, age=20, sex=1, nationality=中国, skinColour=黄色, height=172, weight=170, hairstyle=平头, hairColor=黑色, eye=棕色眼睛, nose=大鼻子, ear=小耳朵, most=厚嘴唇, neck=粗脖子, sword=366712642]
People [name=于氏一剑八, age=20, sex=1, nationality=中国, skinColour=黄色, height=172, weight=170, hairstyle=平头, hairColor=黑色, eye=棕色眼睛, nose=大鼻子, ear=小耳朵, most=厚嘴唇, neck=粗脖子, sword=366712642]
People [name=于氏一剑九, age=20, sex=1, nationality=中国, skinColour=黄色, height=172, weight=170, hairstyle=平头, hairColor=黑色, eye=棕色眼睛, nose=大鼻子, ear=小耳朵, most=厚嘴唇, neck=粗脖子, sword=366712642]
People [name=于氏一剑十, age=20, sex=1, nationality=中国, skinColour=黄色, height=172, weight=170, hairstyle=平头, hairColor=黑色, eye=棕色眼睛, nose=大鼻子, ear=小耳朵, most=厚嘴唇, neck=粗脖子, sword=366712642]

疑问:

现在克隆用是用了,但是有什么好处呢?
对比一下不使用克隆时的方法就知道了。

不使用clone方法

我复制“于氏一剑”时:
 

package 原型模式.不使用原型;

public class 我 {

	public static void main(String[] args) {
		于氏一剑 p1 =new 于氏一剑(
				"于氏一剑一", 
				20, 
				1, 
				"中国", 
				"黄色", 
				172, 
				170, 
				"平头", 
				"黑色", 
				"棕色眼睛", 
				"大鼻子", 
				"小耳朵", 
				"厚嘴唇", 
				"粗脖子", 
				new 鱼肠剑("鱼肠剑"));
		System.out.println(p1.toString());
		
		于氏一剑 p2 =new 于氏一剑(
				"于氏一剑二", 
				20, 
				1, 
				"中国", 
				"黄色", 
				172, 
				170, 
				"平头", 
				"黑色", 
				"棕色眼睛", 
				"大鼻子", 
				"小耳朵", 
				"厚嘴唇", 
				"粗脖子", 
				new 鱼肠剑("鱼肠剑"));
		
		System.out.println(p2.toString());
		
		于氏一剑 p3 =new 于氏一剑(
				"于氏一剑三", 
				20, 
				1, 
				"中国", 
				"黄色", 
				172, 
				170, 
				"平头", 
				"黑色", 
				"棕色眼睛", 
				"大鼻子", 
				"小耳朵", 
				"厚嘴唇", 
				"粗脖子", 
				new 鱼肠剑("鱼肠剑"));
		System.out.println(p3.toString());
		
		于氏一剑 p4 =new 于氏一剑(
				"于氏一剑四", 
				20, 
				1, 
				"中国", 
				"黄色", 
				172, 
				170, 
				"平头", 
				"黑色", 
				"棕色眼睛", 
				"大鼻子", 
				"小耳朵", 
				"厚嘴唇", 
				"粗脖子", 
				new 鱼肠剑("鱼肠剑"));
		System.out.println(p4.toString());
		
		于氏一剑 p5 =new 于氏一剑(
				"于氏一剑五", 
				20, 
				1, 
				"中国", 
				"黄色", 
				172, 
				170, 
				"平头", 
				"黑色", 
				"棕色眼睛", 
				"大鼻子", 
				"小耳朵", 
				"厚嘴唇", 
				"粗脖子", 
				new 鱼肠剑("鱼肠剑"));
		System.out.println(p5.toString());
		
		于氏一剑 p6 =new 于氏一剑(
				"于氏一剑六", 
				20, 
				1, 
				"中国", 
				"黄色", 
				172, 
				170, 
				"平头", 
				"黑色", 
				"棕色眼睛", 
				"大鼻子", 
				"小耳朵", 
				"厚嘴唇", 
				"粗脖子", 
				new 鱼肠剑("鱼肠剑"));
		System.out.println(p6.toString());
		
		于氏一剑 p7 =new 于氏一剑(
				"于氏一剑七", 
				20, 
				1, 
				"中国", 
				"黄色", 
				172, 
				170, 
				"平头", 
				"黑色", 
				"棕色眼睛", 
				"大鼻子", 
				"小耳朵", 
				"厚嘴唇", 
				"粗脖子", 
				new 鱼肠剑("鱼肠剑"));
		System.out.println(p7.toString());
		
		于氏一剑 p8 =new 于氏一剑(
				"于氏一剑八", 
				20, 
				1, 
				"中国", 
				"黄色", 
				172, 
				170, 
				"平头", 
				"黑色", 
				"棕色眼睛", 
				"大鼻子", 
				"小耳朵", 
				"厚嘴唇", 
				"粗脖子", 
				new 鱼肠剑("鱼肠剑"));
		System.out.println(p8.toString());
		
		于氏一剑 p9 =new 于氏一剑(
				"于氏一剑九", 
				20, 
				1, 
				"中国", 
				"黄色", 
				172, 
				170, 
				"平头", 
				"黑色", 
				"棕色眼睛", 
				"大鼻子", 
				"小耳朵", 
				"厚嘴唇", 
				"粗脖子", 
				new 鱼肠剑("鱼肠剑"));
		System.out.println(p9.toString());
		
		于氏一剑 p10 =new 于氏一剑(
				"于氏一剑十", 
				20, 
				1, 
				"中国", 
				"黄色", 
				172, 
				170, 
				"平头", 
				"黑色", 
				"棕色眼睛", 
				"大鼻子", 
				"小耳朵", 
				"厚嘴唇", 
				"粗脖子", 
				new 鱼肠剑("鱼肠剑"));
		System.out.println(p10.toString());
		
	}
	
}

运行结果:
 

People [name=于氏一剑一, age=20, sex=1, nationality=中国, skinColour=黄色, height=172, weight=170, hairstyle=平头, hairColor=黑色, eye=棕色眼睛, nose=大鼻子, ear=小耳朵, most=厚嘴唇, neck=粗脖子, sword=366712642]
People [name=于氏一剑二, age=20, sex=1, nationality=中国, skinColour=黄色, height=172, weight=170, hairstyle=平头, hairColor=黑色, eye=棕色眼睛, nose=大鼻子, ear=小耳朵, most=厚嘴唇, neck=粗脖子, sword=1829164700]
People [name=于氏一剑三, age=20, sex=1, nationality=中国, skinColour=黄色, height=172, weight=170, hairstyle=平头, hairColor=黑色, eye=棕色眼睛, nose=大鼻子, ear=小耳朵, most=厚嘴唇, neck=粗脖子, sword=2018699554]
People [name=于氏一剑四, age=20, sex=1, nationality=中国, skinColour=黄色, height=172, weight=170, hairstyle=平头, hairColor=黑色, eye=棕色眼睛, nose=大鼻子, ear=小耳朵, most=厚嘴唇, neck=粗脖子, sword=1311053135]
People [name=于氏一剑五, age=20, sex=1, nationality=中国, skinColour=黄色, height=172, weight=170, hairstyle=平头, hairColor=黑色, eye=棕色眼睛, nose=大鼻子, ear=小耳朵, most=厚嘴唇, neck=粗脖子, sword=118352462]
People [name=于氏一剑六, age=20, sex=1, nationality=中国, skinColour=黄色, height=172, weight=170, hairstyle=平头, hairColor=黑色, eye=棕色眼睛, nose=大鼻子, ear=小耳朵, most=厚嘴唇, neck=粗脖子, sword=1550089733]
People [name=于氏一剑七, age=20, sex=1, nationality=中国, skinColour=黄色, height=172, weight=170, hairstyle=平头, hairColor=黑色, eye=棕色眼睛, nose=大鼻子, ear=小耳朵, most=厚嘴唇, neck=粗脖子, sword=865113938]
People [name=于氏一剑八, age=20, sex=1, nationality=中国, skinColour=黄色, height=172, weight=170, hairstyle=平头, hairColor=黑色, eye=棕色眼睛, nose=大鼻子, ear=小耳朵, most=厚嘴唇, neck=粗脖子, sword=1442407170]
People [name=于氏一剑九, age=20, sex=1, nationality=中国, skinColour=黄色, height=172, weight=170, hairstyle=平头, hairColor=黑色, eye=棕色眼睛, nose=大鼻子, ear=小耳朵, most=厚嘴唇, neck=粗脖子, sword=1028566121]
People [name=于氏一剑十, age=20, sex=1, nationality=中国, skinColour=黄色, height=172, weight=170, hairstyle=平头, hairColor=黑色, eye=棕色眼睛, nose=大鼻子, ear=小耳朵, most=厚嘴唇, neck=粗脖子, sword=1118140819]

结论:

所有信息都要重新写!!

所以说:当我们需要new一个需要大量赋值,重复性很高的对象时,使用clone方法会很大程度的提高便利性、代码的整洁性。

我这里只写了十几个属性,如果类的属性高达上百个,体现便利性和代码整洁性会更明显。

应用场景

上边比较容易理解的应用场景。

 

还有其他使用场景:

1、资源优化场景。

2、类初始化需要消化非常多的资源,这个资源包括数据、硬件资源等。

3、性能和安全要求的场景。

4、通过 new 产生一个对象需要非常繁琐的数据准备或访问权限,则可以使用原型模式。(如上)

5、一个对象多个修改者的场景。

6、一个对象需要提供给其他对象访问,而且各个调用者可能都需要修改其值时,可以考虑使用原型模式拷贝多个对象供调用者使用。

7、在实际项目中,原型模式很少单独出现,一般是和工厂方法模式一起出现,通过 clone 的方法创建一个对象,然后由工厂方法提供给调用者。原型模式已经与 Java 融为浑然一体,大家可以随手拿来使用。

问题:

但是,相信细心的小伙伴已经发现了一下问题:

即用clone方法克隆的10个“于氏一剑”的“鱼肠剑”没有跟着克隆。

 

这是因为,clone方法对于除String之外引用类型的数据,都无法克隆。因此这克隆方式也叫“浅克隆”

而对引用类型数据也进行克隆的方式叫做“深克隆”

 

实现深克隆方法1

对于应用类型的数据,也重写clone方法,克隆对象时,把引用的对象也进行克隆。

我们对上边的案例做点修改。

鱼肠剑——添加clone方法

package 原型模式.使用原型.深克隆1;

public class 鱼肠剑 implements Cloneable{
	String name;

	public 鱼肠剑(String name) {
		this.name = name;
	}
	
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@Override
	protected Object clone() throws CloneNotSupportedException {
		return super.clone();
	}
}

于氏一剑——clone方法里边,对鱼肠剑也进行克隆

package 原型模式.使用原型.深克隆1;

public class 于氏一剑 implements Cloneable {
	String name;
	int age;
	int sex;


	String nationality;
	String skinColour;
	int height;
	int weight;
	String hairstyle;
	String hairColor;
	String eye;
	String nose;
	String ear;
	String most;
	String neck;
	鱼肠剑 sword;

	public 于氏一剑() {

	}
	public 于氏一剑(String name, int age, int sex, String nationality, String skinColour, int height, int weight,
			String hairstyle, String hairColor, String eye, String nose, String ear, String most, String neck,
			鱼肠剑 sword) {
		super();
		this.name = name;
		this.age = age;
		this.sex = sex;
		this.nationality = nationality;
		this.skinColour = skinColour;
		this.height = height;
		this.weight = weight;
		this.hairstyle = hairstyle;
		this.hairColor = hairColor;
		this.eye = eye;
		this.nose = nose;
		this.ear = ear;
		this.most = most;
		this.neck = neck;
		this.sword = sword;
	}


	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public int getSex() {
		return sex;
	}
	public void setSex(int sex) {
		this.sex = sex;
	}
	public String getNationality() {
		return nationality;
	}
	public void setNationality(String nationality) {
		this.nationality = nationality;
	}
	public String getSkinColour() {
		return skinColour;
	}
	public void setSkinColour(String skinColour) {
		this.skinColour = skinColour;
	}
	public int getHeight() {
		return height;
	}
	public void setHeight(int height) {
		this.height = height;
	}
	public int getWeight() {
		return weight;
	}
	public void setWeight(int weight) {
		this.weight = weight;
	}
	public String getHairstyle() {
		return hairstyle;
	}
	public void setHairstyle(String hairstyle) {
		this.hairstyle = hairstyle;
	}
	public String getHairColor() {
		return hairColor;
	}
	public void setHairColor(String hairColor) {
		this.hairColor = hairColor;
	}
	public String getEye() {
		return eye;
	}
	public void setEye(String eye) {
		this.eye = eye;
	}
	public String getNose() {
		return nose;
	}
	public void setNose(String nose) {
		this.nose = nose;
	}
	public String getEar() {
		return ear;
	}
	public void setEar(String ear) {
		this.ear = ear;
	}
	public String getMost() {
		return most;
	}
	public void setMost(String most) {
		this.most = most;
	}
	public String getNeck() {
		return neck;
	}
	public void setNeck(String neck) {
		this.neck = neck;
	}
	public 鱼肠剑 getSword() {
		return sword;
	}
	public void setSword(于氏一剑 master) {
		this.sword = sword;
	}
	@Override
	public String toString() {
		return "People [name=" + name + ", age=" + age + ", sex=" + sex + ", nationality=" + nationality
				+ ", skinColour=" + skinColour + ", height=" + height + ", weight=" + weight + ", hairstyle="
				+ hairstyle + ", hairColor=" + hairColor + ", eye=" + eye + ", nose=" + nose + ", ear=" + ear
				+ ", most=" + most + ", neck=" + neck + ", sword=" + sword.hashCode() + "]";
	}
	// 克隆该实例,使用默认的clone方法来完成
	@Override
	protected Object clone() throws CloneNotSupportedException  {

		于氏一剑 people = null;
		people = (于氏一剑)super.clone();
		
		// 对剑也进行克隆
		people.sword = (鱼肠剑)people.sword.clone();
		
		return people;
	}


}

我——不用做任何改变

运行结果:

People [name=于氏一剑一, age=20, sex=1, nationality=中国, skinColour=黄色, height=172, weight=170, hairstyle=平头, hairColor=黑色, eye=棕色眼睛, nose=大鼻子, ear=小耳朵, most=厚嘴唇, neck=粗脖子, sword=366712642]
People [name=于氏一剑二, age=20, sex=1, nationality=中国, skinColour=黄色, height=172, weight=170, hairstyle=平头, hairColor=黑色, eye=棕色眼睛, nose=大鼻子, ear=小耳朵, most=厚嘴唇, neck=粗脖子, sword=1829164700]
People [name=于氏一剑三, age=20, sex=1, nationality=中国, skinColour=黄色, height=172, weight=170, hairstyle=平头, hairColor=黑色, eye=棕色眼睛, nose=大鼻子, ear=小耳朵, most=厚嘴唇, neck=粗脖子, sword=2018699554]
People [name=于氏一剑四, age=20, sex=1, nationality=中国, skinColour=黄色, height=172, weight=170, hairstyle=平头, hairColor=黑色, eye=棕色眼睛, nose=大鼻子, ear=小耳朵, most=厚嘴唇, neck=粗脖子, sword=1311053135]
People [name=于氏一剑五, age=20, sex=1, nationality=中国, skinColour=黄色, height=172, weight=170, hairstyle=平头, hairColor=黑色, eye=棕色眼睛, nose=大鼻子, ear=小耳朵, most=厚嘴唇, neck=粗脖子, sword=118352462]
People [name=于氏一剑六, age=20, sex=1, nationality=中国, skinColour=黄色, height=172, weight=170, hairstyle=平头, hairColor=黑色, eye=棕色眼睛, nose=大鼻子, ear=小耳朵, most=厚嘴唇, neck=粗脖子, sword=1550089733]
People [name=于氏一剑七, age=20, sex=1, nationality=中国, skinColour=黄色, height=172, weight=170, hairstyle=平头, hairColor=黑色, eye=棕色眼睛, nose=大鼻子, ear=小耳朵, most=厚嘴唇, neck=粗脖子, sword=865113938]
People [name=于氏一剑八, age=20, sex=1, nationality=中国, skinColour=黄色, height=172, weight=170, hairstyle=平头, hairColor=黑色, eye=棕色眼睛, nose=大鼻子, ear=小耳朵, most=厚嘴唇, neck=粗脖子, sword=1442407170]
People [name=于氏一剑九, age=20, sex=1, nationality=中国, skinColour=黄色, height=172, weight=170, hairstyle=平头, hairColor=黑色, eye=棕色眼睛, nose=大鼻子, ear=小耳朵, most=厚嘴唇, neck=粗脖子, sword=1028566121]
People [name=于氏一剑十, age=20, sex=1, nationality=中国, skinColour=黄色, height=172, weight=170, hairstyle=平头, hairColor=黑色, eye=棕色眼睛, nose=大鼻子, ear=小耳朵, most=厚嘴唇, neck=粗脖子, sword=1118140819]

可以看到,鱼肠剑也跟着克隆了。

 

实现深克隆方法2

这次使用的是IO的序列化和反序列化

鱼肠剑——不许要多写,只需要实现对象序列化的接口Serializable就行

package 原型模式.使用原型.深克隆2;

import java.io.Serializable;

public class 鱼肠剑 implements Serializable{
	String name;

	public 鱼肠剑(String name) {
		this.name = name;
	}
	
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

}

于氏一剑——写一个深克隆的方法,用序列化和反序列化实现

package 原型模式.使用原型.深克隆2;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

public class 于氏一剑 implements Serializable {
	String name;
	int age;
	int sex;


	String nationality;
	String skinColour;
	int height;
	int weight;
	String hairstyle;
	String hairColor;
	String eye;
	String nose;
	String ear;
	String most;
	String neck;
	鱼肠剑 sword;

	public 于氏一剑() {

	}
	public 于氏一剑(String name, int age, int sex, String nationality, String skinColour, int height, int weight,
			String hairstyle, String hairColor, String eye, String nose, String ear, String most, String neck,
			鱼肠剑 sword) {
		super();
		this.name = name;
		this.age = age;
		this.sex = sex;
		this.nationality = nationality;
		this.skinColour = skinColour;
		this.height = height;
		this.weight = weight;
		this.hairstyle = hairstyle;
		this.hairColor = hairColor;
		this.eye = eye;
		this.nose = nose;
		this.ear = ear;
		this.most = most;
		this.neck = neck;
		this.sword = sword;
	}


	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public int getSex() {
		return sex;
	}
	public void setSex(int sex) {
		this.sex = sex;
	}
	public String getNationality() {
		return nationality;
	}
	public void setNationality(String nationality) {
		this.nationality = nationality;
	}
	public String getSkinColour() {
		return skinColour;
	}
	public void setSkinColour(String skinColour) {
		this.skinColour = skinColour;
	}
	public int getHeight() {
		return height;
	}
	public void setHeight(int height) {
		this.height = height;
	}
	public int getWeight() {
		return weight;
	}
	public void setWeight(int weight) {
		this.weight = weight;
	}
	public String getHairstyle() {
		return hairstyle;
	}
	public void setHairstyle(String hairstyle) {
		this.hairstyle = hairstyle;
	}
	public String getHairColor() {
		return hairColor;
	}
	public void setHairColor(String hairColor) {
		this.hairColor = hairColor;
	}
	public String getEye() {
		return eye;
	}
	public void setEye(String eye) {
		this.eye = eye;
	}
	public String getNose() {
		return nose;
	}
	public void setNose(String nose) {
		this.nose = nose;
	}
	public String getEar() {
		return ear;
	}
	public void setEar(String ear) {
		this.ear = ear;
	}
	public String getMost() {
		return most;
	}
	public void setMost(String most) {
		this.most = most;
	}
	public String getNeck() {
		return neck;
	}
	public void setNeck(String neck) {
		this.neck = neck;
	}
	public 鱼肠剑 getSword() {
		return sword;
	}
	public void setSword(于氏一剑 master) {
		this.sword = sword;
	}
	@Override
	public String toString() {
		return "People [name=" + name + ", age=" + age + ", sex=" + sex + ", nationality=" + nationality
				+ ", skinColour=" + skinColour + ", height=" + height + ", weight=" + weight + ", hairstyle="
				+ hairstyle + ", hairColor=" + hairColor + ", eye=" + eye + ", nose=" + nose + ", ear=" + ear
				+ ", most=" + most + ", neck=" + neck + ", sword=" + sword.hashCode() + "]";
	}
	protected Object deepClone() {
		//创建流对象
		ByteArrayOutputStream bos = null;
		ObjectOutputStream oos = null;
		ByteArrayInputStream bis = null;
		ObjectInputStream ois = null;

		try {

			//序列化
			bos = new ByteArrayOutputStream();
			oos = new ObjectOutputStream(bos);
			oos.writeObject(this); //当前这个对象以对象流的方式输出

			//反序列化
			bis = new ByteArrayInputStream(bos.toByteArray());
			ois = new ObjectInputStream(bis);
			于氏一剑 copyObj = (于氏一剑)ois.readObject();

			return copyObj;

		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return null;
		} finally {
			//关闭流
			try {
				bos.close();
				oos.close();
				bis.close();
				ois.close();
			} catch (Exception e2) {
				// TODO: handle exception
				System.out.println(e2.getMessage());
			}
		}

	}


}

我——只需要调用deepClone的方法就行

package 原型模式.使用原型.深克隆2;

public class 我 {

	public static void main(String[] args) throws CloneNotSupportedException {
		于氏一剑 p1 =new 于氏一剑(
				"于氏一剑一", 
				20, 
				1, 
				"中国", 
				"黄色", 
				172, 
				170, 
				"平头", 
				"黑色", 
				"棕色眼睛", 
				"大鼻子", 
				"小耳朵", 
				"厚嘴唇", 
				"粗脖子", 
				new 鱼肠剑("鱼肠剑"));
		System.out.println(p1.toString());
		
		于氏一剑 p2 = (于氏一剑)p1.deepClone();
		p2.setName("于氏一剑二");
		System.out.println(p2.toString());

		于氏一剑 p3 = (于氏一剑)p1.deepClone();
		p3.setName("于氏一剑三");
		System.out.println(p3.toString());
		
		于氏一剑 p4 = (于氏一剑)p1.deepClone();
		p4.setName("于氏一剑四");
		System.out.println(p4.toString());
		
		于氏一剑 p5 = (于氏一剑)p1.deepClone();
		p5.setName("于氏一剑五");
		System.out.println(p5.toString());
		
		于氏一剑 p6 = (于氏一剑)p1.deepClone();
		p6.setName("于氏一剑六");
		System.out.println(p6.toString());
		
		于氏一剑 p7 = (于氏一剑)p1.deepClone();
		p7.setName("于氏一剑七");
		System.out.println(p7.toString());
		
		于氏一剑 p8 = (于氏一剑)p1.deepClone();
		p8.setName("于氏一剑八");
		System.out.println(p8.toString());
		
		于氏一剑 p9 = (于氏一剑)p1.deepClone();
		p9.setName("于氏一剑九");
		System.out.println(p9.toString());
		
		于氏一剑 p10 = (于氏一剑)p1.deepClone();
		p10.setName("于氏一剑十");
		System.out.println(p10.toString());
		
	}
	
}

运行结果:
 

People [name=于氏一剑一, age=20, sex=1, nationality=中国, skinColour=黄色, height=172, weight=170, hairstyle=平头, hairColor=黑色, eye=棕色眼睛, nose=大鼻子, ear=小耳朵, most=厚嘴唇, neck=粗脖子, sword=366712642]
People [name=于氏一剑二, age=20, sex=1, nationality=中国, skinColour=黄色, height=172, weight=170, hairstyle=平头, hairColor=黑色, eye=棕色眼睛, nose=大鼻子, ear=小耳朵, most=厚嘴唇, neck=粗脖子, sword=931919113]
People [name=于氏一剑三, age=20, sex=1, nationality=中国, skinColour=黄色, height=172, weight=170, hairstyle=平头, hairColor=黑色, eye=棕色眼睛, nose=大鼻子, ear=小耳朵, most=厚嘴唇, neck=粗脖子, sword=1607521710]
People [name=于氏一剑四, age=20, sex=1, nationality=中国, skinColour=黄色, height=172, weight=170, hairstyle=平头, hairColor=黑色, eye=棕色眼睛, nose=大鼻子, ear=小耳朵, most=厚嘴唇, neck=粗脖子, sword=764977973]
People [name=于氏一剑五, age=20, sex=1, nationality=中国, skinColour=黄色, height=172, weight=170, hairstyle=平头, hairColor=黑色, eye=棕色眼睛, nose=大鼻子, ear=小耳朵, most=厚嘴唇, neck=粗脖子, sword=381259350]
People [name=于氏一剑六, age=20, sex=1, nationality=中国, skinColour=黄色, height=172, weight=170, hairstyle=平头, hairColor=黑色, eye=棕色眼睛, nose=大鼻子, ear=小耳朵, most=厚嘴唇, neck=粗脖子, sword=2129789493]
People [name=于氏一剑七, age=20, sex=1, nationality=中国, skinColour=黄色, height=172, weight=170, hairstyle=平头, hairColor=黑色, eye=棕色眼睛, nose=大鼻子, ear=小耳朵, most=厚嘴唇, neck=粗脖子, sword=668386784]
People [name=于氏一剑八, age=20, sex=1, nationality=中国, skinColour=黄色, height=172, weight=170, hairstyle=平头, hairColor=黑色, eye=棕色眼睛, nose=大鼻子, ear=小耳朵, most=厚嘴唇, neck=粗脖子, sword=1329552164]
People [name=于氏一剑九, age=20, sex=1, nationality=中国, skinColour=黄色, height=172, weight=170, hairstyle=平头, hairColor=黑色, eye=棕色眼睛, nose=大鼻子, ear=小耳朵, most=厚嘴唇, neck=粗脖子, sword=363771819]
People [name=于氏一剑十, age=20, sex=1, nationality=中国, skinColour=黄色, height=172, weight=170, hairstyle=平头, hairColor=黑色, eye=棕色眼睛, nose=大鼻子, ear=小耳朵, most=厚嘴唇, neck=粗脖子, sword=2065951873]

可以看到,也实现了鱼肠剑跟着于氏一剑克隆。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值