3- prototype model

Prototype mode

Prototype model (Prototype Pattern) is used to create a duplicate of an object, while ensuring performance. This type of design pattern belongs create schema, which provides the best way to create objects.

scenes to be used

1. When a class object requires a large amount of prototype is the best choice, as a prototype model of the object is copied in memory, the object is much better performance than the new new direct, in which case, more objects, the more obvious advantages of the prototype pattern reflected.

2. If the initialization of an object requires a lot of other data objects preparation or complicated computing other resources, you can use the prototype model.

3. When needed when a large amount of public information object, a small field of personalized settings, there may be used the prototype copy of the object copy mode for processing occurs.

Prototype shallow copy mode

Object class java clone method is shallow copy, when the reference field is present in the object class, two copies of the objects in reference field will point to the same object.

Prototype class

//原型类
public class HomeWork implements Cloneable{
    private String student; //学生
    private String teacher; //老师
    private String content; //内容

    public String getStudent() { return student; }
    public void setStudent(String student) { this.student = student; }
    public String getTeacher() { return teacher; }
    public void setTeacher(String teacher) { this.teacher = teacher; }
    public String getContent() { return content; }
    public void setContent(String content) { this.content = content; }
    @Override
    public String toString() {
        return "HomeWork"+super.toString()+"{" + "student='" + student + '\'' +
                ", teacher='" + teacher + '\'' + ", content='" + content + '\'' + '}';
    }
    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
}

Inheritance can be cloned by such tips Cloneable interface and override the parent clone method to achieve cloning.

Test category

//测试类
class Test {
    public static void main(String[] args) throws CloneNotSupportedException {
        List<HomeWork> homeWorks=new LinkedList<>(); //储存整个班级的所有作业
        HomeWork homeWork=new HomeWork(); //创建一个作业模板
        homeWork.setTeacher("me"); //设置模板的教师名称
        homeWork.setContent("xxxx");  //设置作业内容
        for(int a=0;a<5;a++) { //循环创建每个同学的作业
            HomeWork homeWork1=(HomeWork) homeWork.clone(); //克隆作业模板
            homeWork.setStudent("studnet"+a); //设置作业的学生
            homeWorks.add(homeWork1);
        }
    }
}

Prototype deep copy mode

java clone method of the Object class is shallow copy, when a class object reference field is present, by a quoted fields are copied to achieve deep copy.

Prototype class

//原型类
class HomeWork implements Cloneable{
    private String student; //学生
    private String teacher; //老师
    private String content; //内容
    private Date limitDate; //截至时间,!对象引用

    public String getStudent() { return student; }
    public void setStudent(String student) { this.student = student; }
    public String getTeacher() { return teacher; }
    public void setTeacher(String teacher) { this.teacher = teacher; }
    public String getContent() { return content; }
    public void setContent(String content) { this.content = content; }
    public Date getLimitDate() { return limitDate; }
    public void setLimitDate(Date limitDate) { this.limitDate = limitDate; }

    @Override
    public String toString() {
        return "HomeWork"+super.toString()+"{" +
                "student='" + student + '\'' +", teacher='" + teacher + '\'' +
                ", content='" + content + '\'' +", limitDate=" + limitDate + '}';
    }
    @Override
    protected Object clone() throws CloneNotSupportedException {
        HomeWork homeWork = (HomeWork) super.clone(); //先克隆整体
        homeWork.limitDate = (Date) limitDate.clone(); //再对象引用进行单独克隆
        return homeWork;
    }
}

By first cloning the entire object, then the individual clones to avoid field shallow clone of object references.

Test category

//测试类
class Test {
    public static void main(String[] args) throws CloneNotSupportedException {
        List<HomeWork> homeWorks=new LinkedList<>();
        HomeWork homeWork=new HomeWork();
        homeWork.setTeacher("me");
        homeWork.setContent("xxxx");
        homeWork.setLimitDate(new Date());
        for(int a=0;a<5;a++) {
            HomeWork homeWork1=(HomeWork) homeWork.clone();
            homeWork.setStudent("studnet"+a);
            homeWorks.add(homeWork1);
        }
    }
}
Published 27 original articles · won praise 1 · views 906

Guess you like

Origin blog.csdn.net/hu853996234/article/details/103209046