设计模式之原型模式

2023-02-07  本文已影响0人  Tinyspot

1. 原型模式

1.2 核心组成

1.3 应用场景

1.4 浅拷贝与深拷贝区别

2. 浅拷贝

public void demo() {
    ArrayList<String> list = new ArrayList<>();
    list.add("AAA");
    list.add("BBB");

    Object copyList = list.clone();
    System.out.println(copyList == list);
    System.out.println(copyList);
    // 结果是false, 但内容是一样的
}

2.2 对象复制

@Test
public void test() {
    Course course = new Course("English");
    Student student = new Student("Tinyspot", 20, course);

    Student stu2 = new Student();
    stu2.setName(student.getName());
    stu2.setAge(student.getAge());
    stu2.setCourse(student.getCourse());

    student.setName("222");
    student.setAge(22);
    student.getCourse().setTitle("Chinese");
    // or course.setTitle("Chinese");
    System.out.println(student.getCourse() == stu2.getCourse()); // true
}

打印对象:
student: Student(name=222, age=22, course=Course(title=Chinese))
stu2: Student(name=Tinyspot, age=20, course=Course(title=Chinese))

2.3 浅拷贝示例

@Test
public void test() throws CloneNotSupportedException {
    Student student = new Student("Tinyspot", 20, new Course("English"));
    Student stuCopy = student.clone();

    student.setAge(22);
    stuCopy.getCourse().setTitle("Chinese");
}

打印对象:
student: Student(name=Tinyspot, age=22, course=Course(title=Chinese))
stuCopy: Student(name=Tinyspot, age=20, course=Course(title=Chinese))

@Data
@NoArgsConstructor
@AllArgsConstructor
public class Student implements Cloneable {
    private String name;
    private Integer age;
    private Course course;

    @Override
    public Student clone() throws CloneNotSupportedException {
        return (Student) super.clone();
    }
}

@Data
@NoArgsConstructor
@AllArgsConstructor
public class Course {
    private String title;
}

3. 深拷贝

3.1 实现方式一

@Test
public void test() throws CloneNotSupportedException {
    Student student = new Student("Tinyspot", 20, new Course("English"));
    Student stuCopy = student.clone();
    
    student.setAge(22);
    student.getCourse().setTitle("Chinese");
}

打印对象:
student: Student(name=Tinyspot, age=22, course=Course(title=Chinese))
stuCopy: Student(name=Tinyspot, age=20, course=Course(title=English))

@Data
@NoArgsConstructor
@AllArgsConstructor
public class Student implements Cloneable {
    private String name;
    private Integer age;
    private Course course;

    @Override
    public Student clone() throws CloneNotSupportedException {
        Student student = (Student) super.clone();
        // 将拷贝出的对象赋给成员变量
        // or student.course = this.course.clone();
        student.setCourse(this.course.clone());
        return student;
    }
}

@Data
@NoArgsConstructor
@AllArgsConstructor
public class Course implements Cloneable {
    private String title;

    @Override
    public Course clone() throws CloneNotSupportedException {
        return (Course) super.clone();
    }
}

3.2 实现方式二

@Test
public void test() throws CloneNotSupportedException {
    Person person = new Person();
    person.setName("Tinyspot");
    person.getList().add("AAA");

    // 测试浅拷贝
    Person personCopy = person.clone();

    person.setName("222");
    person.getList().add("BBB");
    // or personCopy.getList().add("BBB");

    System.out.println("person: " + person);
    System.out.println("personCopy: " + personCopy);
    System.out.println(person.getList() == personCopy.getList()); // true
}

打印对象:
person: Person(name=222, age=null, list=[AAA, BBB])
personCopy: Person(name=Tinyspot, age=null, list=[AAA, BBB])

@Test
public void test() {
    Person person = new Person();
    person.setName("Tinyspot");
    person.getList().add("AAA");

    // 测试深拷贝
    Person personCopy = person.deepClone();
    personCopy.getList().add("BBB");
}

打印对象
person: Person(name=Tinyspot, age=null, list=[AAA])
personCopy: Person(name=Tinyspot, age=null, list=[AAA, BBB])

上一篇下一篇

猜你喜欢

热点阅读