Bean scope of Prototype prototype model (shallow vs. deep copy)

In Spring Bean five scopes, wherein singleton and prototype embodiments are single mode and the prototype pattern.
Here to talk about the singleton and prototype mode, sort so that I review.

spring in the bean is actually created two types: single-case model and prototype mode. (Prototype model factory pattern needs and match up)

A, prototype model
prototype model is one of the 23 kinds of design patterns.
 When you create objects objects created by the new keyword (like loading judgment, memory allocation, initialization, etc.), the length of the object when we need a lot longer consumed will affect the performance, relative to the prototype model through one by one to new objects words, consumption low.
 Our prototype model, also known as clone mode, that is a prototype of an object is cloned an identical objects, properties, and the prototype of the object to the same. And no effect on the prototype object.
Clone prototype embodiment, there are two modes: shallow clone and a deep clone.

Shallow Cloning: A copy of this object is just an array of objects therein, and the like are not copied referenced object, or point to the internal elements of the native target address.

Shallow clone just copy the object under consideration, without copying the object it refers to. Only copy of this subject, the object of all its internal references to other objects and arrays, which do not copy, they still point to the original address. Method clone Object class provides only copy of this object, array inside object, the object reference is not copied, etc., or the native address pointing to the internal elements of the object.

Deep Cloning: A deep copy the object to be copied objects referenced are copied again.

Copies need to implement Cloneable, Serializable two interfaces, the clone method override
// shallow copy

Pojo类:
public class User implements Cloneable, Serializable {
    private String name;
    private Date date;
    public User() {
        super();
        // TODO Auto-generated constructor stub
    }
    public User(String name, Date date) {
        super();
        this.name = name;
        this.date = date;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Date getDate() {
        return date;
    }
    public void setDate(Date date) {
        this.date = date;
    }
    @Override
    public Object clone() throws CloneNotSupportedException {
        // TODO Auto-generated method stub
        return super.clone();
    }
}

测试:
public class Test {
    public static void main(String[] args) throws CloneNotSupportedException {
        System.out.println("---初始化---");
        User user = new User();
        Date date = new Date(1231231231231l);
        user.setName("zsl");
        user.setDate(date);
        System.out.println("user name:"+user.getName());
        System.out.println("user date:"+user.getDate());
        System.out.println("user:"+user);
        System.out.println("----修改时间----");
        date.setTime(123231231231l);
        user.setDate(date);
        System.out.println("修改后user中date:"+user.getDate());
        
        System.out.println("----进行浅克隆----");
        User user1 = (User) user.clone();
        user1.setName("zsl666");
        System.out.println("user1中name:"+user1.getName());
        System.out.println("user1 date:"+user1.getDate());
        System.out.println("user1:"+user1);
    }
}

// deep clone in two ways:
// 1, based on shallow copy, clone method override in the subject object referenced copy all
// 2, and using a sequence of deep copy deserialization

1、基于浅拷贝实现深拷贝
Pojo类:
public class User implements Cloneable, Serializable {
    private String name;
    private Date date;
    public User() {
        super();
        // TODO Auto-generated constructor stub
    }
    public User(String name, Date date) {
        super();
        this.name = name;
        this.date = date;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Date getDate() {
        return date;
    }
    public void setDate(Date date) {
        this.date = date;
    }
    @Override
    public Object clone() throws CloneNotSupportedException {
        // TODO Auto-generated method stu
        Object object = super.clone();
        User user= (User) object;
        user.date= (Date) this.date.clone();
        return object;
    }
}
测试:
public class Test {
    public static void main(String[] args) throws CloneNotSupportedException {
        System.out.println("---初始化---");
        User user = new User();
        Date date = new Date(1231231231231l);
        user.setName("zsl");
        user.setDate(date);
        System.out.println("user name:"+user.getName());
        System.out.println("user date:"+user.getDate());
        System.out.println("user:"+user);
        
        
        User user1 = (User) user.clone();
        
        System.out.println("----修改时间----");
        date.setTime(123231231231l);
//      user.setDate(date);
        System.out.println("修改后user中date:"+user.getDate());
        
        System.out.println("----进行深克隆----");

        user1.setName("zsl666");
        System.out.println("user1中name:"+user1.getName());
        System.out.println("user1 date:"+user1.getDate());
        System.out.println("user1:"+user1);
    }
}
2、使用序列化和反序列化进行深拷贝
Pojo类:
package com.zsl.pojo;

import java.io.Serializable;
import java.util.Date;

public class User implements Cloneable, Serializable {
    private String name;
    private Date date;
    
    
    public User() {
        super();
        // TODO Auto-generated constructor stub
    }


    public User(String name, Date date) {
        super();
        this.name = name;
        this.date = date;
    }


    public String getName() {
        return name;
    }


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


    public Date getDate() {
        return date;
    }


    public void setDate(Date date) {
        this.date = date;
    }


    @Override
    public Object clone() throws CloneNotSupportedException {
        // TODO Auto-generated method stub
        return super.clone();
    }
    
    
}
测试:
public class Test2 {
    public static void main(String[] args) throws CloneNotSupportedException, IOException, ClassNotFoundException {
        System.out.println("---初始化---");
        User user = new User();
        Date date = new Date(1231231231231l);
        user.setName("zsl");
        user.setDate(date);
        System.out.println("user name:"+user.getName());
        System.out.println("user date:"+user.getDate());
        System.out.println("user:"+user);
        
        //序列化:把对象转换为字节序列的过程。
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(bos);
        oos.writeObject(user);
        
        byte[] bs = bos.toByteArray();
        
        //反序列化:把字节序列恢复为对象的过程。
        ByteArrayInputStream bis = new ByteArrayInputStream(bs);
        ObjectInputStream ois = new ObjectInputStream(bis);
        User user1 = (User) ois.readObject();
        
        System.out.println("----进行深克隆----");
        user1.setName("zsl666");
        System.out.println("user1中name:"+user1.getName());
        System.out.println("user1 date:"+user1.getDate());
        System.out.println("user1:"+user1);
    }
}

By way of acquiring a large number of clone objects when there is little performance overhead impact, and new ways as more and more object instance, performance would drop dramatically, so the prototype model is a more important way to obtain instance.
Prototype models rarely occur alone, usually appear together and factory method pattern, create an object by means clone, and then provided to the caller by the factory method.

Second, singleton
covering Next, ha ....

Guess you like

Origin www.cnblogs.com/zhangsonglin/p/11123157.html