原型模式

问题描述:

向量的原型

用C++完成数学中向量的封装,其中,用指针和动态申请支持向量长度的改变,使用浅克隆和深克隆复制向量类,比较这两种克隆方式的异同。

浅克隆类图:

 

 

浅克隆Java代码:

复制代码
 1 //Vector.java
 2 package qiankelong;
 3 
 4 import java.util.Arrays;
 5 
 6 public class Vector implements Cloneable 
 7 {
 8     private int[] array;
 9     
10     private Attachment attachment=null;
11     
12     public Vector()
13     {
14         
15         this.attachment=new Attachment();
16     }     
17     
18     public Object clone()
19     {
20         Vector clone=null;
21         try
22         {
23             clone=(Vector)super.clone();        
24         }
25         catch(CloneNotSupportedException e)
26         {
27             System.out.println("Clone failure!");
28         }
29         return clone;
30     }
31     
32     public Attachment getAttachment()
33     {
34         return this.attachment;
35     }
36     
37     public void display()
38     {
39             System.out.println(Arrays.toString(array));    
40     }
41 
42     public int[] getArray() {
43         return array;
44     }
45 
46     public void setArray(int[] array) {
47         this.array = array;
48     }
49     
50 }
51 //Attachment.java
52 package qiankelong;
53 public class Attachment
54 {
55     public void download()
56     {
57         System.out.println("复制向量");    
58     }
59 }
60 //Client.java
61 package qiankelong;
62 public class Client
63 {
64     public static void main(String a[])
65     {
66         
67         Vector vector,copyVector;
68         vector=new Vector();
69         int[] arr= {2,8,3,6};
70         vector.setArray(arr);
71         System.out.println("浅克隆示例:");
72         copyVector=(Vector)vector.clone();
73         System.out.print("vector:");
74         vector.display();
75         System.out.print("copyVector:");
76         copyVector.display();
77         System.out.println("vector==copyVector?");
78         System.out.println(vector==copyVector);
79         
80         System.out.println("vector.getAttachment==copyVector.getAttachment?"); 
81         System.out.println(vector.getAttachment()==copyVector.getAttachment());            
82     }
83 }
复制代码

 

浅克隆运行结果:

 

 

深克隆类图:

 

 

深克隆Java代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
//Vector.java
package shenkelong;
import java.io.*;
import java.util.Arrays;
 
public class Vector implements Serializable
{
    private int[] array;
    private Attachment attachment=null;
     
    public Vector()
    {
        this.attachment=new Attachment();
    }
     
    public Object deepClone() throws IOException, ClassNotFoundException, OptionalDataException
    {
        //将对象写入流中
        ByteArrayOutputStream bao=new ByteArrayOutputStream();
        ObjectOutputStream oos=new ObjectOutputStream(bao);
        oos.writeObject(this);
         
        //将对象从流中取出
        ByteArrayInputStream bis=new ByteArrayInputStream(bao.toByteArray());
        ObjectInputStream ois=new ObjectInputStream(bis);
        return(ois.readObject());
    }
     
    public Attachment getAttachment()
    {
        return this.attachment;
    }
     
    public void display()
    {
            System.out.println(Arrays.toString(array));
    }
 
    public int[] getArray() {
        return array;
    }
 
    public void setArray(int[] array) {
        this.array = array;
    }
}
//Attachment.java
package shenkelong;
import java.io.*;
 
public class Attachment implements Serializable
{
    public void download()
    {
        System.out.println("复制向量");
    }
}
//Client.java
package shenkelong;
public class Client
{
    public static void main(String a[])
    {
        Vector vector,copyVector=null;
         
        vector=new Vector();
        int[] arr= {2,8,3,6};
        vector.setArray(arr);
        System.out.println("深克隆示例:");
         
        try{
            copyVector=(Vector)vector.deepClone(); 
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }  
        System.out.print("vector:");
        vector.display();
        System.out.print("copyVector:");
        copyVector.display();
        System.out.println("vector==copyVector?");
        System.out.println(vector==copyVector);
         
        System.out.println("vector.getAttachment==copyVector.getAttachment?");
        System.out.println(vector.getAttachment()==copyVector.getAttachment());        
    }
}

  

posted @ 2021-10-07 18:25  奇怪的软工人  阅读(100)  评论(0编辑  收藏  举报