原型模式

1.过程相同,但是结果不同

2.数据内容完全相同,但是实例不同

克隆就是原型模式的一种使用场景。

代码:

孙悟空的金箍棒

1)

package com.gupaoedu.vip.prototype.greatestsage;

import java.util.Date;

//猴子

public class Monkey {

//身高

protected int height;//基本

//体重

protected int weight;

//生日

protected Date birthday;//不是基本类型

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 Date getBirthday() {

return birthday;

}

public void setBirthday(Date birthday) {

this.birthday = birthday;

}

}

2)

package com.gupaoedu.vip.prototype.greatestsage;

import java.io.ByteArrayInputStream;

import java.io.ByteArrayOutputStream;

import java.io.IOException;

import java.io.ObjectInputStream;

import java.io.ObjectOutputStream;

import java.io.Serializable;

import java.util.Date;

/**

* 齐天大圣

* @author Tom

*

*/

public class TheGreatestSage  extends Monkey implements Cloneable,Serializable{

//金箍棒

private GoldRingedStaff staff;

//从石头缝里蹦出来

public TheGreatestSage(){

this.staff = new GoldRingedStaff();

this.birthday = new Date();

this.height = 150;

this.weight = 30;

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

}

//分身技能

public Object clone(){

//深度克隆

ByteArrayOutputStream bos = null;

ObjectOutputStream oos = null;

ByteArrayInputStream bis = null;

ObjectInputStream ois = null;

try {

//return super.clone();//默认浅克隆,只克隆八大基本数据类型和String

//序列化

bos = new ByteArrayOutputStream();

oos = new ObjectOutputStream(bos);

oos.writeObject(this);

//反序列化

bis = new ByteArrayInputStream(bos.toByteArray());

ois = new ObjectInputStream(bis);

TheGreatestSage copy = (TheGreatestSage)ois.readObject();

copy.birthday = new Date();

return copy;

} catch (Exception e) {

e.printStackTrace();

return null;

}finally{

try {

bos.close();

oos.close();

bis.close();

ois.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

//变化

public void change(){

TheGreatestSage copySage = (TheGreatestSage)clone();

System.out.println("大圣本尊生日是:" + this.getBirthday().getTime());

System.out.println("克隆大圣的生日是:" + copySage.getBirthday().getTime());

System.out.println("大圣本尊和克隆大圣是否为同一个对象:" + (this == copySage));

System.out.println("大圣本尊持有的金箍棒跟克隆大圣持有金箍棒是否为同一个对象:" + (this.getStaff() == copySage.getStaff()));

}

public GoldRingedStaff getStaff() {

return staff;

}

public void setStaff(GoldRingedStaff staff) {

this.staff = staff;

}

}

3)

package com.gupaoedu.vip.prototype.greatestsage;

import java.io.Serializable;

/**

* 金箍棒

* @author Tom

*

*/

public class GoldRingedStaff implements Serializable{

private float height = 100; //长度

private float diameter = 10;//直径

/**

* 金箍棒长大

*/

public void grow(){

this.diameter *= 2;

this.height *= 2;

}

/**

* 金箍棒缩小

*/

public void shrink(){

this.diameter /= 2;

this.height /= 2;

}

}

4)

package com.gupaoedu.vip.prototype.greatestsage;

public class TestPrototype {

public static void main(String[] args) {

TheGreatestSage sage = new TheGreatestSage();

sage.change();

//跟《西游记》中描述的一致,怎么办?

}

}

你可能感兴趣的:(原型模式)