欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Java设计模式之原型模式

程序员文章站 2022-05-05 12:12:21
...
定义:
用原型实例指定创建对象的种类,并且通过拷贝这些原型创建新的对象.

Prototype模式允许一个对象再创建另外一个可定制的对象,根本无需知道任何如何创建的细节,工作原理是:通过将一个原型对象传给那个要发动创建的对象,这个要发动创建的对象通过请求原型对象拷贝它们自己来实施创建

浅复制

   被复制对象的所有变量都含有与原来的对象相同的值,而所有的对其他对象的引用都仍然指向原来的对象。换言之,浅复制仅仅复制所考虑的对象,而不复制它所引用的对象。

深复制

   被复制对象的所有的变量都含有与原来的对象相同的值,除去那些引用其他对象的变量。那些引用其他对象的变量将指向被复制过的新对象,而不再是原有的那些被引用的对象。换言之,深复制把重复的对象所引用的对象都复制一遍,而这种对被引用到的对象的复制叫做间接复制。



浅复制

Mokey.JAVA

package desin.Prototype.lower;

import java.util.Date;



public class Mokey implements Cloneable {

private int height;
private int weight;
private Date birthDate;
private GoldRingdeStaff goldRingdeStaff;

public  Mokey(){
  birthDate= new Date();
}

public Date getBirthDate() {
  return birthDate;
}

public void setBirthDate(Date birthDate) {
  this.birthDate = birthDate;
}

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 Object clone(){
  Mokey mokey=null;
  try {
   mokey=(Mokey)super.clone();
  } catch (CloneNotSupportedException e) {
   // TODO 自动生成 catch 块
   e.printStackTrace();
  }
  finally{
   return mokey;
  }
}

public GoldRingdeStaff getGoldRingdeStaff() {
  return goldRingdeStaff;
}

public void setGoldRingdeStaff(GoldRingdeStaff goldRingdeStaff) {
  this.goldRingdeStaff = goldRingdeStaff;
}

}
GoldRingdeStaff。JAVA

package desin.Prototype.lower;

public class GoldRingdeStaff {
private float height=100.0f;
private float weight=10.0f;
public float getHeight() {
  return height;
}
public void setHeight(float height) {
  this.height = height;
}
public float getWeight() {
  return weight;
}
public void setWeight(float weight) {
  this.weight = weight;
}

}

testClient.JAVA

package desin.Prototype.lower;

public class testClient {
private Mokey mokey= new Mokey();

public void change(){
  Mokey copymokey2;
   copymokey2=(Mokey)mokey.clone();
  System.out.println("monkey birth date :"+mokey.getBirthDate());
  System.out.println("copymokey2 birth date :"+copymokey2.getBirthDate());
  System.out.println("copymokey2 ==monkey :"+(copymokey2==mokey));
  System.out.println("copymokey2 staff ==monkey staff:"+(copymokey2.getGoldRingdeStaff()==mokey.getGoldRingdeStaff()));
}


public static void main(String[] args) {
  // TODO 自动生成方法存根
  testClient testClient= new testClient();
  testClient.change();

}

}



深复制

Mokey.JAVA

package desin.Prototype.deep;

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;

import com.sun.corba.se.internal.io.OptionalDataException;


public class Mokey implements Serializable, Cloneable {
private int height;
private int weight;
private Date birthDate;
private GoldRingdeStaff goldRingdeStaff;

public  Mokey(){
  this.birthDate=new Date();
  this.goldRingdeStaff=new GoldRingdeStaff();
}

public Date getBirthDate() {
  return birthDate;
}

public void setBirthDate(Date birthDate) {
  this.birthDate = birthDate;
}

public GoldRingdeStaff getGoldRingdeStaff() {
  return goldRingdeStaff;
}

public void setGoldRingdeStaff(GoldRingdeStaff goldRingdeStaff) {
  this.goldRingdeStaff = goldRingdeStaff;
}

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 Object deepClone() throws IOException,OptionalDataException,ClassNotFoundException{
 
  ByteArrayOutputStream bo = new ByteArrayOutputStream();
  ObjectOutputStream oo= new ObjectOutputStream(bo);
  oo.writeObject(this);
 
  ByteArrayInputStream bi = new ByteArrayInputStream(bo.toByteArray());
  ObjectInputStream oi= new ObjectInputStream(bi);
  return (oi.readObject());
}

}

GoldRingdeStaff.JAVA

package desin.Prototype.deep;

import java.io.Serializable;



public class GoldRingdeStaff implements Cloneable, Serializable {
private float height=100.0f;
private float weight=10.0f;
public float getHeight() {
  return height;
}
public void setHeight(float height) {
  this.height = height;
}
public float getWeight() {
  return weight;
}
public void setWeight(float weight) {
  this.weight = weight;
}
}
testClient.JAVA

package desin.Prototype.deep;

import java.io.IOException;

import com.sun.corba.se.internal.io.OptionalDataException;


public class testClient {
private Mokey mokey= new Mokey();

public void change()throws IOException,OptionalDataException,ClassNotFoundException{
  Mokey copymokey2;
   copymokey2=(Mokey)mokey.deepClone();
  System.out.println("monkey birth date :"+mokey.getBirthDate());
  System.out.println("copymokey2 birth date :"+copymokey2.getBirthDate());
  System.out.println("copymokey2 ==monkey :"+(copymokey2==mokey));
  System.out.println("copymokey2 staff ==monkey staff:"+(copymokey2.getGoldRingdeStaff()==mokey.getGoldRingdeStaff()));
}


public static void main(String[] args) throws IOException,OptionalDataException,ClassNotFoundException{
  // TODO 自动生成方法存根
  testClient testClient= new testClient();
  testClient.change();

}

}