04 原型模式Prototype Pattern

使用场景

简单点说就是克隆对象!适用场景如下:

  • 1.当初始化类对象需要消耗非常多资源,或者说要进行繁琐的数据准备或者权限,如果想简化创建,可以使用原型模式。
  • 2.一个对象提供给其他对象访问,而各个调用者可能都需要修改对象的值,可以考虑使用原型模式克隆多个对象供调用者使用(保护性拷贝)

组成部分(三个角色):

  • Prototype —— 声明一个克隆自身的接口,用于约束想要克隆自己的类,要求实现定义的克隆方法。
  • ConcretePrototype —— 实现Prototype接口的类,这些类真正实现克隆自身的相关代码。
  • Client —— 客户端用户,调用类

UML类图如下

Java中的 == 与 equals 的区别

  • ==,如果是对比的基本数据类型(int,long等),比较存储的值是否相等,如果对比的是引用型的变量,比较的是所指向的对象地址是否相等

  • equals,不能用于比较基本数据类型,如果没对equals()方法进行重写,比较的是指向的对象地址,如果想要比较对象内容,需要自行重写方法,做相应的判断!!!!String调equals是可以判断内容是否一样,是因为对equals()方法进行了重写,具体自己参见源码!

克隆必须满足的条件(三个)

  • 1.对任何的对象x,都有:x.clone()!=x ,即不是同一对象;
  • 2.对任何的对象x,都有:x.clone().getClass==x.getClass(),即对象类型一致;
  • 3.如果对象obj的equals()方法定义恰当的话,那么obj.clone().equals(obj)应当是成立的。(推荐,不强制);

Java中如何使用:

Prototype原型类(想被克隆的类)实现Cloneable接口,重写clone()方法。
调用:
ConcretePrototype cp1 = new ConcretePrototype();
ConcretePrototype cp2 = (ConcretePrototype)cp1.clone(); 

代码示例

package prototypePattern;

/**
 * 原型模式的测试例程
 * @Package       prototypePattern
 * @Title:        Demo.java
 * @Company:      $
 * @author        BurgessLee 
 * @date          2018年10月11日-上午9:49:35
 * @Description:  $
 */
public class Demo {
	
	public static void main(String[] args) {
		Asserts asserts = new Asserts(100, new Money("人民币"), "现金");
		Asserts asserts2 = (Asserts)asserts.clone();
		
		System.out.println("asserts.equals(asserts2):" +asserts.equals(asserts2));
		System.out.println("asserts == asserts2:" +(asserts == asserts2));
		
		System.out.println("asserts.getClass() == asserts2.getClass():" +(asserts.getClass() == asserts2.getClass()));
		System.out.println("asserts.getMoney() == asserts2.getMoney():" +(asserts.getMoney() == asserts2.getMoney()));
		
		System.out.println("asserts.toString():" +asserts.toString());
		System.out.println("asserts2.toString():" +asserts2.toString());
		
		asserts.setAccount(200);
		System.out.println("asserts.toString():" +asserts.toString());
		System.out.println("asserts2.toString():" +asserts2.toString());
		
		asserts2.getMoney().setType("美金");
		System.out.println("asserts.toString():" +asserts.toString());
		System.out.println("asserts2.toString():" +asserts2.toString());
	}
	
}

class Money{
	private String type;//币种

	public Money(String type) {
		super();
		this.type = type;
	}

	public String getType() {
		return type;
	}

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

	@Override
	public String toString() {
		return "Money [type=" + type + "]";
	}
}

class Asserts implements Cloneable{
	
	private int account;//数目
	private Money money;//币种
	private String kind;//资金种类
	public Asserts(int account, Money money, String kind) {
		super();
		this.account = account;
		this.money = money;
		this.kind = kind;
	}
	public int getAccount() {
		return account;
	}
	public void setAccount(int account) {
		this.account = account;
	}
	public Money getMoney() {
		return money;
	}
	public void setMoney(Money money) {
		this.money = money;
	}
	public String getKind() {
		return kind;
	}
	public void setKind(String kind) {
		this.kind = kind;
	}
	@Override
	public String toString() {
		return "Asserts [account=" + account + ", money=" + money + ", kind="
				+ kind + "]";
	}
	
	@Override
	protected Object clone(){
		Asserts asserts = null;
		try {
			asserts = (Asserts)super.clone();
		} catch (CloneNotSupportedException e) {
			// TODO Auto-generated catch block
		}
		return asserts;
	}
	
}

打印结果:

asserts.equals(asserts2):false
asserts == asserts2:false
asserts.getClass() == asserts2.getClass():true
asserts.getMoney() == asserts2.getMoney():true
asserts.toString():Asserts [account=100, money=Money [type=人民币], kind=现金]
asserts2.toString():Asserts [account=100, money=Money [type=人民币], kind=现金]
asserts.toString():Asserts [account=200, money=Money [type=人民币], kind=现金]
asserts2.toString():Asserts [account=100, money=Money [type=人民币], kind=现金]
asserts.toString():Asserts [account=200, money=Money [type=美金], kind=现金]
asserts2.toString():Asserts [account=100, money=Money [type=美金], kind=现金]

结果分析得出结论

  • 1.执行克隆方法,不会调用构造方法!
  • 2.克隆会生成的新的对象变量指向的却是同一个内存地址!
  • 3.克隆前后数据类型一致!
  • 4.克隆的时候,类中基本数据类型的属性会新建,但是引用类型的 只会生成个新的引用变量,引用变量的地址依旧指向同一个内存地址!

深浅拷贝与实现深拷贝的两种简单套路

上面这种只新建基本类型数据不新建引用类型数据,称为浅拷贝, 如果连引用类型数据也新建的话,则称为深拷贝

深拷贝的其中一种实现套路是,引用类型也实现Cloneable接口,然后实现clone方法。比如让Money类也实现Cloneable接口,重写clone方法,然后Assets类,clone方法那里调这个方法,为money对象赋值。

测试代码如下:

package prototypePattern;

/**
 * 原型模式的测试例程
 * @Package       prototypePattern
 * @Title:        Demo.java
 * @Company:      $
 * @author        BurgessLee 
 * @date          2018年10月11日-上午9:49:35
 * @Description:  $
 */
public class Demo2 {
	
	public static void main(String[] args) {
		Asserts2 asserts = new Asserts2(100, new Money2("人民币"), "现金");
		Asserts2 asserts2 = (Asserts2)asserts.clone();
		
		System.out.println("asserts.equals(asserts2):" +asserts.equals(asserts2));
		System.out.println("asserts == asserts2:" +(asserts == asserts2));
		
		System.out.println("asserts.getClass() == asserts2.getClass():" +(asserts.getClass() == asserts2.getClass()));
		System.out.println("asserts.getMoney() == asserts2.getMoney():" +(asserts.getMoney() == asserts2.getMoney()));
		
		System.out.println("asserts.toString():" +asserts.toString());
		System.out.println("asserts2.toString():" +asserts2.toString());
		
		asserts.setAccount(200);
		System.out.println("asserts.toString():" +asserts.toString());
		System.out.println("asserts2.toString():" +asserts2.toString());
		
		asserts2.getMoney().setType("美金");
		System.out.println("asserts.toString():" +asserts.toString());
		System.out.println("asserts2.toString():" +asserts2.toString());
	}
	
}

class Money2 implements Cloneable{
	private String type;//币种

	public Money2(String type) {
		super();
		this.type = type;
	}

	public String getType() {
		return type;
	}

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

	@Override
	public String toString() {
		return "Money [type=" + type + "]";
	}
	
	@Override
	protected Object clone(){
		Money2 money = null;
		try {
			money = (Money2)super.clone();
		} catch (CloneNotSupportedException e) {
			e.printStackTrace();
		}
		return money;
	}
}

class Asserts2 implements Cloneable{
	
	private int account;//数目
	private Money2 money;//币种
	private String kind;//资金种类
	public Asserts2(int account, Money2 money, String kind) {
		super();
		this.account = account;
		this.money = money;
		this.kind = kind;
	}
	public int getAccount() {
		return account;
	}
	public void setAccount(int account) {
		this.account = account;
	}
	public Money2 getMoney() {
		return money;
	}
	public void setMoney(Money2 money) {
		this.money = money;
	}
	public String getKind() {
		return kind;
	}
	public void setKind(String kind) {
		this.kind = kind;
	}
	@Override
	public String toString() {
		return "Asserts [account=" + account + ", money=" + money + ", kind="
				+ kind + "]";
	}
	
	@Override
	protected Object clone(){
		Asserts2 asserts = null;
		try {
			asserts = (Asserts2)super.clone();
			asserts.setMoney((Money2)this.getMoney().clone());
			
		} catch (CloneNotSupportedException e) {
			// TODO Auto-generated catch block
		}
		return asserts;
	}
	
}

程序运行结果:

asserts.equals(asserts2):false
asserts == asserts2:false
asserts.getClass() == asserts2.getClass():true
asserts.getMoney() == asserts2.getMoney():false
asserts.toString():Asserts [account=100, money=Money [type=人民币], kind=现金]
asserts2.toString():Asserts [account=100, money=Money [type=人民币], kind=现金]
asserts.toString():Asserts [account=200, money=Money [type=人民币], kind=现金]
asserts2.toString():Asserts [account=100, money=Money [type=人民币], kind=现金]
asserts.toString():Asserts [account=200, money=Money [type=人民币], kind=现金]
asserts2.toString():Asserts [account=100, money=Money [type=美金], kind=现金]

好的,结果创建了新的对象,没毛病,另外这种方法实现深拷贝如果属性的类型也是对象,那么需要一直递归的克隆下去!!要想深度克隆成功,必须要整个克隆所涉及的对象都要正确实现克隆方法,如果其中有一个没有正确实现克隆,那么就会导致克隆失败。

这就是深拷贝的其中一种实现套路,还有另一种套路是 序列化,属性的类型是引用类型的话,需要实现Serializable接口,然后自己写个方法来在里面完成对象转二进制流与二进制流转对象的方法,然后返回克隆后的对象!

示例代码如下:

package prototypePattern;

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

import com.sun.xml.internal.messaging.saaj.util.ByteOutputStream;

/**
 * 原型模式的测试例程
 * @Package       prototypePattern
 * @Title:        Demo.java
 * @Company:      $
 * @author        BurgessLee 
 * @date          2018年10月11日-上午9:49:35
 * @Description:  $
 */
public class Demo3 {
	
	public static void main(String[] args) throws ClassNotFoundException, IOException {
		Asserts3 asserts = new Asserts3(100, new MoneyS("人民币"), "现金");
		Asserts3 asserts2 = asserts.deepClone();
		
		System.out.println("asserts.equals(Asserts3):" +asserts.equals(asserts2));
		System.out.println("asserts == Asserts3:" +(asserts == asserts2));
		
		System.out.println("asserts.getClass() == Asserts3.getClass():" +(asserts.getClass() == asserts2.getClass()));
		System.out.println("asserts.getMoney() == Asserts3.getMoney():" +(asserts.getMoney() == asserts2.getMoney()));
		
		System.out.println("asserts.toString():" +asserts.toString());
		System.out.println("Asserts3.toString():" +asserts2.toString());
		
		asserts.setAccount(200);
		System.out.println("asserts.toString():" +asserts.toString());
		System.out.println("Asserts3.toString():" +asserts2.toString());
		
		asserts2.getMoney().setType("美金");
		System.out.println("asserts.toString():" +asserts.toString());
		System.out.println("Asserts3.toString():" +asserts2.toString());
	}
	
}

class MoneyS implements Serializable{
	private String type;//币种

	public MoneyS(String type) {
		super();
		this.type = type;
	}

	public String getType() {
		return type;
	}

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

	@Override
	public String toString() {
		return "Money [type=" + type + "]";
	}

}

class Asserts3 implements Serializable{
	
	private int account;//数目
	private MoneyS money;//币种
	private String kind;//资金种类
	public Asserts3(int account, MoneyS money, String kind) {
		super();
		this.account = account;
		this.money = money;
		this.kind = kind;
	}
	public int getAccount() {
		return account;
	}
	public void setAccount(int account) {
		this.account = account;
	}
	public MoneyS getMoney() {
		return money;
	}
	public void setMoney(MoneyS money) {
		this.money = money;
	}
	public String getKind() {
		return kind;
	}
	public void setKind(String kind) {
		this.kind = kind;
	}
	@Override
	public String toString() {
		return "Asserts [account=" + account + ", money=" + money + ", kind="
				+ kind + "]";
	}
	
	public Asserts3 deepClone() throws IOException, ClassNotFoundException{
		//写入当前对象的二进制流
		ByteOutputStream byteOutputStream = new ByteOutputStream();
		ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteOutputStream);
		objectOutputStream.writeObject(this);
		
		//读出二进制流产生的新对象
		ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteOutputStream.toByteArray());
		ObjectInputStream objectInputStream = new ObjectInputStream(byteArrayInputStream);
		
		return (Asserts3)objectInputStream.readObject();
	}
	
}

输出结果:

asserts.equals(Asserts3):false
asserts == Asserts3:false
asserts.getClass() == Asserts3.getClass():true
asserts.getMoney() == Asserts3.getMoney():false
asserts.toString():Asserts [account=100, money=Money [type=人民币], kind=现金]
Asserts3.toString():Asserts [account=100, money=Money [type=人民币], kind=现金]
asserts.toString():Asserts [account=200, money=Money [type=人民币], kind=现金]
Asserts3.toString():Asserts [account=100, money=Money [type=人民币], kind=现金]
asserts.toString():Asserts [account=200, money=Money [type=人民币], kind=现金]
Asserts3.toString():Asserts [account=100, money=Money [type=美金], kind=现金]

优缺点

优点

  • 1.简化对象创建过程,当对象创建比较烦琐时,可提高创建效率;
  • 2.深拷贝可保存对象状态,可将对象拷贝后保存起来,需要的时候恢复;

缺点

  • 深拷贝时,属性存在引用类型多层嵌套的话,为了实现深克隆,每一层对象对应的类都必须支持深克隆,实现起来可能会比较麻烦
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值