返回顶部

一缕半夏微光

温柔半两,从容一生

导航

软件设计⑥|原型模式(深克隆、浅克隆)

一、原型模式

https://baike.baidu.com/item/%E6%8A%BD%E8%B1%A1%E5%B7%A5%E5%8E%82%E6%A8%A1%E5%BC%8F/2361182?fr=aladdin

二、代码

①类图:

②效果图:

③目录结构:

④代码:

Book.java

 1 package clone;
 2 
 3 import java.io.Serializable;
 4 
 5 public class Book implements Serializable{
 6 
 7     private String name;
 8 
 9     public Book(String name) {
10         this.name = name;
11     }
12 
13     public String getName() {
14         return name;
15     }
16 
17     public void setName(String name) {
18         this.name = name;
19     }
20 }

Client.java

 1 package clone;
 2 
 3 public class Client {
 4     public static void main(String[] args){
 5         Book book = new Book("Chinese");
 6         Student s = new Student("Tom",12,book);
 7         
 8         //浅克隆
 9         System.out.println("浅克隆");
10         Student s1 = s.clone();
11 
12         System.out.println(s==s1);
13         System.out.println(s.getClass()==s1.getClass());
14         System.out.println(s.getBook()==s1.getBook());
15 
16         System.out.println("------------------------");
17 
18         try {
19             //深克隆
20             System.out.println("深克隆");
21             Student s2 = s.deepClone();
22 
23             System.out.println(s==s2);
24             System.out.println(s.getClass()==s2.getClass());
25             System.out.println(s.getBook()==s2.getBook());
26 
27             System.out.println("-------------------");
28             System.out.println("s.age:"+s.getAge()+" s2.age:"+s2.getAge());
29         } catch (Exception e) {
30             e.printStackTrace();
31         }
32     }
33 }

Student.java

 1 package clone;
 2 
 3 import java.io.ByteArrayInputStream;
 4 import java.io.ByteArrayOutputStream;
 5 import java.io.ObjectInputStream;
 6 import java.io.ObjectOutputStream;
 7 import java.io.Serializable;
 8 
 9 public class Student implements Cloneable ,Serializable{
10 
11     private String name;
12     private transient int age;
13     private Book book;
14 
15     //浅克隆
16     public Student clone(){
17         Student stu = null;
18         try {
19             stu = (Student) super.clone();
20         } catch (CloneNotSupportedException e) {
21             e.printStackTrace();
22         }
23         return stu;
24     }
25 
26     //深克隆
27     public Student deepClone() throws Exception {
28         Student stu = null;
29         ByteArrayOutputStream bo = new ByteArrayOutputStream();
30         ObjectOutputStream oos = new ObjectOutputStream(bo);
31         oos.writeObject(this);
32         ByteArrayInputStream bi = new ByteArrayInputStream(bo.toByteArray());
33         ObjectInputStream oi = new ObjectInputStream(bi);
34         stu = (Student) oi.readObject();
35         return stu;
36     }
37 
38 
39     public Student(String name, int age, Book book) {
40         this.name = name;
41         this.age = age;
42         this.book = book;
43     }
44 
45     public String getName() {
46         return name;
47     }
48 
49     public void setName(String name) {
50         this.name = name;
51     }
52 
53     public int getAge() {
54         return age;
55     }
56 
57     public void setAge(int age) {
58         this.age = age;
59     }
60 
61     public Book getBook() {
62         return book;
63     }
64 
65     public void setBook(Book book) {
66         this.book = book;
67     }
68 }

参考链接:https://blog.csdn.net/yaoliao_11/article/details/53386044

posted on 2021-10-05 17:34  一缕半夏微光  阅读(68)  评论(0编辑  收藏  举报