package cn.bunny.pattern8.demo2;

import lombok.Data;

public class Prototype {
    public static void main(String[] args) {
        Resume resume = new Resume();
        resume.name = "Bunny";
        resume.position = "架构师";
        resume.salary = 15000;

        School school = new School();
        school.schoolName = "江南大学";
        school.time = "2025年2月2日21:38:07";
        resume.school = school;
        System.out.println(resume);

        Resume resume1 = resume.clone();
        resume1.salary = 19000;
        resume1.school.schoolName = "清华大学";
        System.out.println(resume1);

        Resume resume2 = resume.clone();
        resume2.salary = 20000;
        System.out.println(resume2);

        System.out.println(resume1 == resume2);// false
        System.out.println(resume1.school == resume2.school);// false
    }

    @Data
    static class Resume implements Cloneable {
        private String name;
        private String position;
        private Integer salary;
        private School school;

        @Override
        public Resume clone() {
            Resume resume = null;
            try {
                resume = (Resume) super.clone();
                if (school != null) {
                    resume.school = school.clone();
                }
            } catch (CloneNotSupportedException e) {
                throw new AssertionError();
            }

            return resume;
        }
    }

    @Data
    static class School implements Cloneable {
        private String schoolName;
        private String time;

        @Override
        public School clone() {
            try {
                return (School) super.clone();
            } catch (CloneNotSupportedException e) {
                throw new AssertionError();
            }
        }
    }
}
