版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_30604989/article/details/81674841
1、场景
- 思考一下:克隆技术是怎样的过程?
- JavaScript中的继承是怎样实现的?那里面也有protoType
2、原型模式
- 通过new产生一个对象需要非常繁琐的数据准备或访问权限,则可以通过使用原型模式。
- 就是java中的克隆技术,以某个对象为原型,复制出新的对象。显然,新的对象具备原型对象的特点。
- 优势有:效率高(直接克隆,避免了重新执行构造过程的步骤)
- 克隆类似于new,但是不同于new。new创建新的对象属性采用的是默认值。克隆出来的对象的属性值完全和原型对象相同。并且克隆出来的新对象改变不会影响原型对象。然后再修改克隆对象的值。
3、原型模式的实现
- Cloneable接口和clone方法
- Prototype模式中实现起来最困难的地方就是内存复制操作,所幸在java中提供了clone( )方法替我们做绝大部分的事情。
注意:克隆和拷贝是一个意思
4、实例
浅克隆
原型
public class Sheep implements Cloneable {
private String sname;
private Date birthday;
@Override
protected Object clone() throws CloneNotSupportedException {
//直接调用Object对象的clone方法
Object object = super.clone();
return object;
}
public Sheep(String sname, Date birthday) {
this.sname = sname;
this.birthday = birthday;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
}
克隆
* 测试原型模式(浅克隆)
*/
public class User {
public static void main(String[] args) throws CloneNotSupportedException {
Date date = new Date(1241125345L);
Sheep s1 = new Sheep("多利妈",date);
System.out.println(s1+","+s1.getSname()+","+s1.getBirthday());
Sheep s2 = (Sheep) s1.clone();
System.out.println(s2+","+s2.getSname()+","+s2.getBirthday());
}
}
浅克隆:当date对象被s1改变之后,s2获取到的值也是被改变过的
深克隆:当date被s1改变后,对后续的克隆并不产生影响。
* 克隆羊,要克隆必须实现Cloneable,深克隆
*/
public class Sheep2 implements Cloneable {
private String sname;
private Date birthday;
@Override
protected Object clone() throws CloneNotSupportedException {
//直接调用Object对象的clone方法
Object object = super.clone();
//添加如下代码实现深复制
Sheep2 sheep = (Sheep2) object;
sheep.birthday = (Date) this.birthday.clone();//把属性也进行克隆
return object;
}
public Sheep2(String sname, Date birthday) {
this.sname = sname;
this.birthday = birthday;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
}
* 测试原型模式(深克隆)
*/
public class User2 {
public static void main(String[] args) throws CloneNotSupportedException {
Date date = new Date(1241125345L);
Sheep2 s1 = new Sheep2("多利妈",date);
System.out.println(s1+","+s1.getSname()+","+s1.getBirthday());
s1.setBirthday(new Date(29237419L));
Sheep2 s2 = (Sheep2) s1.clone();
System.out.println(s2+","+s2.getSname()+","+s2.getBirthday());
}
}
5、利用序列化和反序列化技术实现深克隆
*
* 测试原型模式(深克隆,利用序列化和反序列化的方式实现深克隆)
*/
public class User3 {
public static void main(String[] args) throws CloneNotSupportedException, IOException, ClassNotFoundException {
Date date = new Date(1241125345L);
Sheep s1 = new Sheep("多利妈",date);
System.out.println(s1+","+s1.getSname()+","+s1.getBirthday());
//Sheep2 s2 = (Sheep2) s1.clone();
//使用序列化和反序列化实现深复制
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(bos);
oos.writeObject(s1);//写s1这个对象到bos
byte[] bytes = bos.toByteArray();
//反序列化
ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes);
ObjectInputStream ois = new ObjectInputStream(inputStream);
Sheep s2 = (Sheep) ois.readObject();//克隆好的新羊
s1.setBirthday(new Date(29237419L));
System.out.println(s1+","+s1.getSname()+","+s1.getBirthday());
System.out.println(s2+","+s2.getSname()+","+s2.getBirthday());
}
}
6、短时间大量创建对象时,原型模式和普通new方式的效率比较
class Loptop implements Cloneable{
public Loptop() {
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
public class User4 {
public static void testNew(int size){
Long start = System.currentTimeMillis();
for (int i = 0; i < size; i++) {
Loptop loptop = new Loptop();
}
Long end = System.currentTimeMillis();
System.out.println(end-start);
}
public static void testClone(int size) throws CloneNotSupportedException {
Long start = System.currentTimeMillis();
Loptop loptop = new Loptop();
for (int i = 0; i < size; i++) {
loptop = (Loptop) loptop.clone();
}
Long end = System.currentTimeMillis();
System.out.println(end-start);
}
public static void main(String[] args) throws CloneNotSupportedException {
testNew(1000);
testClone(1000);
}
}
结果比较:
7、开发场景中的应用
原型模式很少单独出现,一般是和工厂方法模式一起出现,通过clone的方法创建一个对象,然后由工厂方法提供给调用者。
- Spring中的bean的创建实际就是两种:单例模式和原型模式(当然,原型模式需要和工厂模式搭配起来)