场景
- 思考一下:克隆技术是怎么样的过程?
- JavaScript语言中的,继承怎么实现?那里面也有prototype
原型模式
- 通过new产生一个对象需要繁琐的数据准备或访问权限,则可以使用原型模式。
- 就是java中的克隆技术,以某个对象为原型,复制出新的对象。显然,新的对象具备原型对象的特点。
- 优势有:效率高(直接克隆,避免了重新执行构造过程步骤)。
- 克隆类似于new,但是不同于new。new创建新的对象属性采用的是默认值。克隆出的对象的属性值完全和原型对象相同。并且克隆出的新对象改变不会影响原型对象。然后,再修改克隆对象的值。
原型模式实现
- Cloneable接口和clone方法
- Prototype模式中实现起来最困难的地方就是内存复制操作,所幸在Java中提供了clone()方法替我们做了绝大部分事情。
package prototype;
import java.util.Date;
public class Sheep implements Cloneable{
private String sname;
private Date birthday;
public Sheep() {
}
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;
}
@Override
protected Object clone() throws CloneNotSupportedException {
Object obj = super.clone();
return obj;
}
}
package prototype;
import java.util.Date;
/**
* @Description: 浅克隆
*/
public class Client {
public static void main(String[] args) throws Exception {
Date date = new Date();
Sheep s1 = new Sheep("少利",date);
Sheep s2 = (Sheep) s1.clone();
System.out.println(s1);
System.out.println(s1.getSname());
System.out.println(s1.getBirthday());
date.setTime(123123123141L);
System.out.println(s1.getBirthday());
s2.setSname("多利");
System.out.println(s2);
System.out.println(s2.getSname());
System.out.println(s2.getBirthday());
}
}
输出:
prototype.Sheep@52ab6c19
少利
Wed Apr 25 10:31:31 CST 2018
Mon Nov 26 08:52:03 CST 1973
prototype.Sheep@6929ae9b
多利
Mon Nov 26 08:52:03 CST 1973
package prototype;
import java.util.Date;
public class Sheep2 implements Cloneable{
private String sname;
private Date birthday;
public Sheep2() {
}
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;
}
@Override
protected Object clone() throws CloneNotSupportedException {
Object obj = super.clone();
// 深克隆实现
Sheep2 sheep = (Sheep2) obj;
sheep.birthday = (Date) this.birthday.clone();
return obj;
}
}
package prototype;
import java.util.Date;
/**
* @Description: 深克隆
*/
public class Client2 {
public static void main(String[] args) throws Exception{
Date date = new Date();
Sheep2 s1 = new Sheep2("少利",date);
Sheep2 s2 = (Sheep2) s1.clone();
System.out.println(s1);
System.out.println(s1.getSname());
System.out.println(s1.getBirthday());
date.setTime(123123123141L);
System.out.println(s1.getBirthday());
s2.setSname("多利");
System.out.println(s2);
System.out.println(s2.getSname());
System.out.println(s2.getBirthday());
}
}
输出:
prototype.Sheep2@55991e21
少利
Wed Apr 25 10:33:34 CST 2018
Mon Nov 26 08:52:03 CST 1973
prototype.Sheep2@2533b5db
多利
Wed Apr 25 10:33:34 CST 2018
利用序列化和反序列化技术实现深克隆
package prototype;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Date;
/**
* @Description: 利用序列化和反序列化技术实现深克隆
*/
public class Client3 {
public static void main(String[] args) throws Exception{
Date date = new Date();
Sheep s1 = new Sheep("少利",date);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(bos);
oos.writeObject(s1);
byte[] bytes = bos.toByteArray();
ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
ObjectInputStream ois = new ObjectInputStream(bis);
Sheep s2 = (Sheep) ois.readObject();
System.out.println(s1);
System.out.println(s1.getSname());
System.out.println(s1.getBirthday());
date.setTime(123123123141L);
System.out.println(s1.getBirthday());
s2.setSname("多利");
System.out.println(s2);
System.out.println(s2.getSname());
System.out.println(s2.getBirthday());
}
}
输出:
prototype.Sheep@667053be
少利
Wed Apr 25 10:47:00 CST 2018
Mon Nov 26 08:52:03 CST 1973
prototype.Sheep@528ef256
多利
Wed Apr 25 10:47:00 CST 2018
普通new方式创建对象和clone方式创建对象的效率差异
package prototype;
import java.util.concurrent.TimeUnit;
public class Client4 {
public static void testNew(int size){
long start = System.currentTimeMillis();
for (int i=0;i<size;i++){
Laptop t = new Laptop();
}
long end = System.currentTimeMillis();
System.out.println("new的方式创建耗时:"+(end-start));
}
public static void testClone(int size) throws CloneNotSupportedException {
long start = System.currentTimeMillis();
Laptop t = new Laptop();
for (int i=0;i<size;i++){
Laptop clone = (Laptop) t.clone();
}
long end = System.currentTimeMillis();
System.out.println("clone的方式创建耗时:"+(end-start));
}
public static void main(String[] args) throws Exception{
testNew(1000);
testClone(1000);
}
}
class Laptop implements Cloneable{
public Laptop(){
try {
TimeUnit.MILLISECONDS.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
输出:
new的方式创建耗时:10324
clone的方式创建耗时:11
如果需要段时间创建大量对象,并且new的过程比较耗时,则可以考虑使用原型模式。
开发中的应用场景
- 原型模式很少单独出现,一般是和工厂方法模式一起出现,通过clone的方法创建一个对象,然后由工厂方法提供给调用者。
spring中bean的创建实际就两种模式:单例模式和原型模式。(当然,原型模式需要和工厂模式搭配起来)
网友评论