原型模式

深入讨论 - 浅拷贝和深拷贝

浅拷贝的介绍

相关链接:Python3中深拷贝与浅拷贝

  1. 对于数据类型是基本数据类型的成员变量,浅拷贝会直接进行值传递,也就是将该属性值复制一份给新的对象

  2. 对于数据类型是 引用数据类型 的成员变量,比如说这个成员变量是某个数组,某个类的对象等,那么浅拷贝会进行引用传递,也就是只是将该成员变量的引用值(内存地址)复制一份给新的对象.因为实际上两个对象的该成员变量都指向了同一个实例(内存地址).在这种情况下,在一个对象中修改该成员变量会影响到另一个对象的改成员变量的值.

  3. 前面我们克隆羊就是浅拷贝

  4. 浅拷贝是使用默认的clone()方法来实现的 sheep = (Sheep) super.clone();

浅拷贝 实践1

Sheep

package com.atguigu.prototype.improve;

public class Sheep implements Cloneable {
    private String name;
    private int age;
    private String color;
    private String address = "蒙古样";


    public Sheep(String name, int age, String color) {
        this.name = name;
        this.age = age;
        this.color = color;
    }

    @Override
    public String toString() {
        return "Sheep{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", color='" + color + '\'' +
                ", address='" + address + '\'' +
                '}';
    }

    public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

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

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    // 克隆该实例,使用默认的克隆方法来完成
    @Override
    protected Object clone() {

        Sheep sheep = null;
        //
        try {
            sheep = (Sheep) super.clone();
        } catch (CloneNotSupportedException e) {
            System.out.println(e.getMessage());
        }
        return sheep;
    }
}

Clinet

package com.atguigu.prototype.improve;

public class Clinet {
    public static void main(String[] args) {
        // 原型模式完成 对象的创建
        Sheep sheep = new Sheep("tom", 1, "白色");
        Sheep sheep2 = (Sheep) sheep.clone();   // 克隆
        Sheep sheep3 = (Sheep) sheep.clone();   // 克隆
        Sheep sheep4 = (Sheep) sheep.clone();   // 克隆

        System.out.println(sheep);
        System.out.println(sheep2);
        System.out.println(sheep3);
        System.out.println(sheep4);

        /*
        * Sheep{name='tom', age=1, color='白色', address='蒙古样'}
        Sheep{name='tom', age=1, color='白色', address='蒙古样'}
        Sheep{name='tom', age=1, color='白色', address='蒙古样'}
        Sheep{name='tom', age=1, color='白色', address='蒙古样'}

        Process finished with exit code 0

        * */
    }
}

浅拷贝 实践2

Sheep

package com.atguigu.prototype.improve2;

public class Sheep implements Cloneable {
    private String name;
    private int age;
    private String color;
    private String address = "蒙古样";
    public Sheep friend;   // 是对象 ,克隆的时候会怎么处理

    public Sheep(String name, int age, String color) {
        this.name = name;
        this.age = age;
        this.color = color;
    }

    @Override
    public String toString() {
        return "Sheep{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", color='" + color + '\'' +
                ", address='" + address + '\'' +
                ", friend=" + friend +
                '}';
    }

    public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

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

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public Sheep getFriend() {
        return friend;
    }

    public void setFriend(Sheep friend) {
        this.friend = friend;
    }

    // 克隆该实例,使用默认的克隆方法来完成
    @Override
    protected Object clone() {

        Sheep sheep = null;
        //
        try {
            sheep = (Sheep) super.clone();
        } catch (CloneNotSupportedException e) {
            System.out.println(e.getMessage());
        }
        return sheep;
    }
}

Clinet

package com.atguigu.prototype.improve2;

public class Clinet {
    public static void main(String[] args) {
        // 原型模式完成 对象的创建
        Sheep sheep = new Sheep("tom", 1, "白色");
        sheep.friend =  new Sheep("jack", 3, "黑色");
        Sheep sheep2 = (Sheep)sheep.clone();    // 克隆
        System.out.println("sheep="+sheep);
        System.out.println("sheep.friend="+sheep.friend.hashCode());
        System.out.println("sheep2="+sheep2);
        System.out.println("sheep2.friend="+sheep2.friend.hashCode());

        /*
        *
        * sheep=Sheep{name='tom', age=1, color='白色', address='蒙古样', friend=Sheep{name='jack', age=3, color='黑色', address='蒙古样', friend=null}}
        sheep.friend=356573597
        sheep2=Sheep{name='tom', age=1, color='白色', address='蒙古样', friend=Sheep{name='jack', age=3, color='黑色', address='蒙古样', friend=null}}
        sheep2.friend=356573597

        Process finished with exit code 0
        * */

        // 这不就是相当于没克隆成么,浅拷贝的
    }
}


results matching ""

    No results matching ""