程序员地瓜哥的小屋

Java中的原型模式

2021-12-29  本文已影响0人  CodingDGSun

原型模式简介

浅克隆

类Book实现Cloneable接口并重写clone方法
public class Book implements Cloneable {
    private String name;//书名
    private Date publishDate;//发布日期

    public Book() {
    }

    public Book(String name, Date publishDate) {
        this.name = name;
        this.publishDate = publishDate;
    }

    public String getName() {
        return name;
    }

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

    public Date getPublishDate() {
        return publishDate;
    }

    public void setPublishDate(Date publishDate) {
        this.publishDate = publishDate;
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }

    @Override
    public String toString() {
        return "Book{" +
                "name='" + name + '\'' +
                ", publishDate=" + publishDate +
                '}';
    }
}
测试类
public class Test {
    public static void main(String[] args) throws CloneNotSupportedException {
        String name = "Java 设计模式";
        Date publishDate = new Date();

        Book book = new Book(name, publishDate);
        //克隆出对象
        Book clone = (Book) book.clone();

        System.out.println(book);
        System.out.println(book.hashCode());
        System.out.println(clone);
        System.out.println(clone.hashCode());

        System.out.println("==================");

        publishDate.setTime(publishDate.getTime() + 2000);//修改时间
        System.out.println(book);
        System.out.println(book.hashCode());
        System.out.println(clone);
        System.out.println(clone.hashCode());

        /**
         * 测试结果:修改了时间,源对象和克隆的对象的时间,都发生了改变
         * Book{name='Java 设计模式', publishDate=Wed Dec 29 12:17:33 CST 2021}
         * 2093631819
         * Book{name='Java 设计模式', publishDate=Wed Dec 29 12:17:33 CST 2021}
         * 2074407503
         * ==================
         * Book{name='Java 设计模式', publishDate=Wed Dec 29 12:17:35 CST 2021}
         * 2093631819
         * Book{name='Java 设计模式', publishDate=Wed Dec 29 12:17:35 CST 2021}
         * 2074407503
         */
    }
}
测试结果
Book{name='Java 设计模式', publishDate=Wed Dec 29 12:17:33 CST 2021}
2093631819
Book{name='Java 设计模式', publishDate=Wed Dec 29 12:17:33 CST 2021}
2074407503
==================
Book{name='Java 设计模式', publishDate=Wed Dec 29 12:17:35 CST 2021}
2093631819
Book{name='Java 设计模式', publishDate=Wed Dec 29 12:17:35 CST 2021}
2074407503

深克隆

类Book实现Cloneable接口并重写clone方法
public class Book implements Cloneable {
    private String name;//书名
    private Date publishDate;//发布日期

    public Book() {
    }

    public Book(String name, Date publishDate) {
        this.name = name;
        this.publishDate = publishDate;
    }

    public String getName() {
        return name;
    }

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

    public Date getPublishDate() {
        return publishDate;
    }

    public void setPublishDate(Date publishDate) {
        this.publishDate = publishDate;
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
        Object obj = super.clone();
        Book book = (Book) obj;

        //将源对象的属性也进行克隆,即深克隆
        book.publishDate = (Date) book.publishDate.clone();
        return obj;
    }

    @Override
    public String toString() {
        return "Book{" +
                "name='" + name + '\'' +
                ", publishDate=" + publishDate +
                '}';
    }
}
测试类
public class Test {
    public static void main(String[] args) throws CloneNotSupportedException {
        String name = "Java 设计模式";
        Date publishDate = new Date();

        Book book = new Book(name, publishDate);
        //克隆出对象
        Book clone = (Book) book.clone();

        System.out.println(book);
        System.out.println(book.hashCode());
        System.out.println(clone);
        System.out.println(clone.hashCode());

        System.out.println("==================");

        publishDate.setTime(publishDate.getTime() + 2000);
        System.out.println(book);
        System.out.println(book.hashCode());
        System.out.println(clone);
        System.out.println(clone.hashCode());

        /**
         * 测试结果:修改了时间,源对象的时间,发生了改变,但克隆的对象的时间没有改变
         * Book{name='Java 设计模式', publishDate=Wed Dec 29 12:41:47 CST 2021}
         * 2093631819
         * Book{name='Java 设计模式', publishDate=Wed Dec 29 12:41:47 CST 2021}
         * 2074407503
         * ==================
         * Book{name='Java 设计模式', publishDate=Wed Dec 29 12:41:49 CST 2021}
         * 2093631819
         * Book{name='Java 设计模式', publishDate=Wed Dec 29 12:41:47 CST 2021}
         * 2074407503
         */
    }
}
测试结果
Book{name='Java 设计模式', publishDate=Wed Dec 29 12:41:47 CST 2021}
2093631819
Book{name='Java 设计模式', publishDate=Wed Dec 29 12:41:47 CST 2021}
2074407503
==================
Book{name='Java 设计模式', publishDate=Wed Dec 29 12:41:49 CST 2021}
2093631819
Book{name='Java 设计模式', publishDate=Wed Dec 29 12:41:47 CST 2021}
2074407503
上一篇下一篇

猜你喜欢

热点阅读