原型模式

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();

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

}

}

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1大同小异的工作周报 Sunny软件公司一直使用自行开发的一套OA (Office Automatic,办公自动化...
    justCode_阅读 1,181评论 0 3
  • 一 预备知识 1. 首先我们要知道 原型模式是设计模式中的创建型 2.要了解原型模式我们要了解java 中的深...
    CRUD_1133阅读 439评论 0 0
  • 原型模式(Prototype Pattern)是首先创建一个原型对象,再通过复制这个原型对象来创建更多同类型的对象...
    小小小明_c050阅读 518评论 0 4
  • 原型模式 这里需要引入两个概念: 浅克隆浅克隆. 它的工作原理如下: 在内存中先开辟一块和原始对象相同的空间, 然...
    普明子阅读 241评论 0 0
  • 新方副总(陈小君):你好,余经理。非常高兴来到美丽的都江堰。我是新西兰佳沛国际有限公司 亚洲区副总经理陈小君。这位...
    lustre19阅读 682评论 0 0