java设计模式之原型模式

本文介绍了原型模式,并通过浅克隆和深克隆两种方式详细解释了其工作原理。通过具体的Java代码示例,展示了如何实现对象的克隆,并比较了浅克隆与深克隆的区别。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

下面我们来浅谈原型模式,这里通过浅克隆和深克隆来说明

      其实我们spring的bean中  scope=‘prototype’ ,这里其实就是原型模式,可以实例化多个对象

   浅克隆:简单一句,就是引用对象,没有被克隆!

   深克隆:就是引用对象也被克隆了

  实体类:

package com.asiainfo.prototype;

import java.io.Serializable;

public class Person implements Serializable{
	
	private String username;

	private int age;
	
	public Person() {
		
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}
}

  浅克隆:   

package com.asiainfo.prototype;

import java.util.Date;

public class FlowMoney implements Cloneable{
	
	private String type;//钱的种类 
	
	private String score;//额度
	
	private Person person;
	
	public FlowMoney() {
	}

	
	public FlowMoney(String type, String score,Person person) {
		this.type = type;
		this.score = score;
		this.person=person;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public String getScore() {
		return score;
	}

	public void setScore(String score) {
		this.score = score;
	}
	
	public Object clone() throws CloneNotSupportedException {
		FlowMoney o=null; 
		  try
		   { 
		    o=(FlowMoney)super.clone(); 
		   } 
		  catch(CloneNotSupportedException e) 
		   { 
		    System.out.println(e.toString()); 
		   } 
		  return o; 
	}


	public Person getPerson() {
		return person;
	}


	public void setPerson(Person person) {
		this.person = person;
	}

}
深克隆:

package com.asiainfo.prototype;

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

public class DeepMoney implements Cloneable,Serializable{
	
	private String type;//钱的种类 
	
	private String score;//额度
	
	private Person person;
	
	public DeepMoney() {
	}

	
	public DeepMoney(String type, String score,Person person) {
		this.type = type;
		this.score = score;
		this.person=person;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public String getScore() {
		return score;
	}

	public void setScore(String score) {
		this.score = score;
	}
	
	
	public Object clone() throws CloneNotSupportedException {
                //通过序列化与反序列化 实现深克隆,将引用对象也克隆到新对象中
               Object obj = null;
		
                //序列化
		ByteArrayOutputStream  baos = new ByteArrayOutputStream();
		
		ObjectInputStream ois = null;
		
		try {
			ObjectOutputStream oos = new ObjectOutputStream(baos);
			
			oos.writeObject(this);
			//反序列化
			ByteArrayInputStream bis = new  ByteArrayInputStream(baos.toByteArray());
			
			ois = new ObjectInputStream(bis);
			
			obj= ois.readObject();
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return obj;
	}

	public Person getPerson() {
		return person;
	}

	public void setPerson(Person person) {
		this.person = person;
	}
}

测试:
<pre name="code" class="html">package com.asiainfo.test;

import com.asiainfo.prototype.DeepMoney;
import com.asiainfo.prototype.FlowMoney;
import com.asiainfo.prototype.Person;

public class TestPrototype {
	public static void main(String[] args) throws Exception {
		//浅克隆,引用对象是不能克隆的
		Person person = new Person();
		person.setAge(22);
		person.setUsername("zengml");
		FlowMoney money1  = new FlowMoney("RMB","100",person);
		FlowMoney money2= (FlowMoney) money1.clone();
		
		//修改引用对象的值
		money1.getPerson().setAge(23);
		money1.getPerson().setUsername("zengml1");
		
		System.out.println(money1.getPerson().getUsername());
		System.out.println(money2.getPerson().getUsername());
		
		//判断引用对象是否相等
		System.out.println("引用对象是否相等:"+(money1.getPerson()==money2.getPerson()));
		
		
		//深克隆
		Person depPerson = new Person();
		depPerson.setAge(22);
		depPerson.setUsername("zengml");
		DeepMoney money3  = new DeepMoney("RMB","100",depPerson);
		//克隆对象
		DeepMoney money4= (DeepMoney) money3.clone();
		
		//修改引用对象的值
		money3.getPerson().setAge(23);
		money3.getPerson().setUsername("zengml1");
		
		System.out.println(money3.getPerson().getUsername());
		System.out.println(money4.getPerson().getUsername());
		
		
		System.out.println("引用对象是否相等:"+(money3.getPerson()==money4.getPerson()));
	}
}



结果: 
zengml1
zengml1
引用对象是否相等:true
zengml1
zengml
引用对象是否相等:false

这样就很简单明了,深克隆与浅克隆的区别了!

      

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值