码迷,mamicode.com
首页 > 其他好文 > 详细

原型模式

时间:2019-03-27 00:01:12      阅读:193      评论:0      收藏:0      [点我收藏+]

标签:port   实现   了解   object   prototype   string   引用   name   main   

先了解下,java中克隆相关的。需实现clonable接口,然后重写clone方法。

Teacher类:

package PrototypeParttern;

public class Teacher implements Cloneable {
    String name;
    Integer age;

    public String getName() {
        return name;
    }

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

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Teacher{" +
                "name=‘" + name + ‘\‘‘ +
                ", age=‘" + age + ‘\‘‘ +
                ‘}‘;
    }
    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
}

 

 Stu类:

package PrototypeParttern;

public class Stu implements Cloneable {
    String name;
    Integer age;
    Teacher teacher;

    public Stu() {
        System.out.println("测试clone方法是否调用构造方法");
    }

    public String getName() {
        return name;
    }

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

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public Teacher getTeacher() {
        return teacher;
    }

    public void setTeacher(Teacher teacher) {
        this.teacher = teacher;
    }

    @Override
    public String toString() {
        return "Stu{" +
                "name=‘" + name + ‘\‘‘ +
                ", age=" + age +
                ", teacher=" + teacher +
                ‘}‘;
    }
    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
}

 

测试类:

package PrototypeParttern;

public class CloneTest {
    public static void main(String[] args) {
        Teacher t1 = new Teacher();
        t1.setAge(32);
        t1.setName("wang Teacher");
        Stu s1 = new Stu();
        s1.setName("xiao ming");
        s1.setAge(22);
        s1.setTeacher(t1);
        System.out.println("复制前的学生1:"+s1);
        try {
            Stu s2 = (Stu) s1.clone();
            s2.setName("da ming");
            s2.setAge(24);
            Teacher t2 = s2.getTeacher();
            t2.setName("zhao Teacher");
            t2.setAge(34);
            s2.setTeacher(t2);
            System.out.println("复制后的学生1:"+s1);
            System.out.println("复制后的学生2:"+s2);
            System.out.println("teacher的地址"+s1.getTeacher().hashCode());
            System.out.println("teacher的地址"+s2.getTeacher().hashCode());

        } catch (CloneNotSupportedException e) {
        }
    }
}

 

测试clone方法是否调用构造方法
复制前的学生1:Stu{name=‘xiao ming‘, age=22, teacher=Teacher{name=‘wang Teacher‘, age=‘32‘}}
复制后的学生1:Stu{name=‘xiao ming‘, age=22, teacher=Teacher{name=‘zhao Teacher‘, age=‘34‘}}
复制后的学生2:Stu{name=‘da ming‘, age=24, teacher=Teacher{name=‘zhao Teacher‘, age=‘34‘}}
teacher的地址785992331
teacher的地址785992331

 

说明说,clone不是真正的复制,引用类型的成员变量是一样的。

 

重写clone方法可以实现深拷贝:

    @Override
    protected Object clone() throws CloneNotSupportedException {
        /*return super.clone();*/
        Stu newstudent = (Stu) super.clone();
        newstudent.teacher = (Teacher) teacher.clone();
        return newstudent;
    }

 

 

原型模式:

原型模式

标签:port   实现   了解   object   prototype   string   引用   name   main   

原文地址:https://www.cnblogs.com/chenmz1995/p/10604555.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!