原型模式

原型模式(Prototype): 用原型实例指定创建对象的种类,并且通过拷贝原型来创建新的实例对象。它允许一个对象再创建另外一个可定制的对象,根本无需知道任何创建细节,工作原理,通过将一个原型对象传给那个要发动创建的对象,这个发动创建对象通过请求原型对象拷贝它们自己来创建。
通俗点,就是通过拷贝来进行创建实例。
原型模式适合在什么场景使用?一是类初始化需要消化非常多的资源,这个资源包括数据、硬件资源
等;二是通过new 产生一个对象需要非常繁琐的数据准备或访问权限,则可以使用原型模式;三是一个对
象需要提供给其他对象访问,而且各个调用者可能都需要修改其值时,可以考虑使用原型模式拷贝多个对
象供调用者使用。在实际项目中,原型模式很少单独出现,一般是和工厂方法模式一起出现,通过clone
的方法创建一个对象,然后由工厂方法提供给调用者。


package pattern.prototype;

public class Car implements Cloneable {
private String name;
private Wheel wheel;

public void setName(String name) {
this.name = name;
}

public String getName() {
return name;
}

public void setWheel(Wheel wheel) {
this.wheel = wheel;
}

public Wheel getWheel() {
return wheel;
}

public Object clone() {
Car car = null;
try {
car = (Car) super.clone();
car.wheel= (Wheel) this.wheel.clone();
} catch (CloneNotSupportedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return car;
}
}



package pattern.prototype;

public class Wheel implements Cloneable {
private String name;

public void setName(String name) {
this.name = name;
}

public String getName() {
return name;
}

public Object clone() {
Wheel wheel = null;
try {
wheel = (Wheel) super.clone();
} catch (CloneNotSupportedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return wheel;
}
}



package pattern.prototype;

public class Test {
public static void main(String[] args) {
Car car1 = new Car();
car1.setName("Benz");
Wheel w = new Wheel();
w.setName("AA");
car1.setWheel(w);
Car car2=(Car) car1.clone();
System.out.println(car1.getName());
System.out.println(car1.getWheel().getName());
System.out.println("*********************************");
car2.getWheel().setName("BB");
car2.setName("toyota");
System.out.println(car2.getName());
System.out.println(car2.getWheel().getName());
System.out.println("*********************************");

System.out.println(car1.getName());
System.out.println(car1.getWheel().getName());
}
}




下面是通过Java序列化实现的原型模式,通过将对象序列化,可以实现深拷贝,这种方式可以不用在每个关联对象中添加clone方法,只须实现Serializable接口.故是一种提倡用法:


package pattern.prototype;

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

public class Car implements Serializable {
/**
*
*/

private String name;
private Wheel wheel;

public void setName(String name) {
this.name = name;
}

public String getName() {
return name;
}

public void setWheel(Wheel wheel) {
this.wheel = wheel;
}

public Wheel getWheel() {
return wheel;
}

// public Object clone() {
// Car car = null;
// try {
// car = (Car) super.clone();
// car.wheel= (Wheel) this.wheel.clone();
// } catch (CloneNotSupportedException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
// return car;
// }

public static Object copy(Object oldObj) {
Object obj = null;
try {
System.out.println(oldObj + " dfdf");
// Write the object out to a byte array
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream out = new ObjectOutputStream(bos);
out.writeObject(oldObj);
out.flush();
out.close();
// Retrieve an input stream from the byte array and read
// a copy of the object back in.
ByteArrayInputStream bis = new ByteArrayInputStream(bos
.toByteArray());
ObjectInputStream in = new ObjectInputStream(bis);
obj = in.readObject();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException cnfe) {
cnfe.printStackTrace();
}
return obj;
}

}



package pattern.prototype;

import java.io.Serializable;

public class Wheel implements Serializable {
private String name;

public void setName(String name) {
this.name = name;
}

public String getName() {
return name;
}

public Object clone() {
Wheel wheel = null;
try {
wheel = (Wheel) super.clone();
} catch (CloneNotSupportedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return wheel;
}
}



package pattern.prototype;

public class Test {
public static void main(String[] args) {
Car car1 = new Car();
car1.setName("Benz");
Wheel w = new Wheel();
w.setName("AA");
car1.setWheel(w);
Car car2=(Car) Car.copy(car1);
System.out.println(car1.getName());
System.out.println(car1.getWheel().getName());
System.out.println("*********************************");
car2.getWheel().setName("BB");
car2.setName("toyota");
System.out.println(car2.getName());
System.out.println(car2.getWheel().getName());
System.out.println("*********************************");

System.out.println(car1.getName());
System.out.println(car1.getWheel().getName());
}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值