• 设计模式——原型模式


    一、任务

    量的原型

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

    二、类图

    三、代码

     1、深克隆(C++)

     1 #include <iostream>
     2 #include <string.h>
     3 using namespace std;
     4 
     5 class Vector {
     6 private:
     7     int x1;
     8     int y1;
     9     int x2;
    10     int y2;
    11 public:
    12 
    13     Vector(int x1, int y1,int x2,int y2)//构造函数
    14     {
    15         x1 = x1;
    16         y1 = y1;
    17         x2 = x2;
    18         y2 = y2;
    19         cout << "构造函数" << endl;
    20     }
    21     Vector(const Vector& p)//拷贝构造函数
    22     {
    23         
    24         x1 = p.x1;
    25         y1 = p.y1;
    26         x2 = p.x2;
    27         y2 = p.y2;
    28         cout << "进行深克隆" << endl;
    29     }
    30     Vector() {
    31 
    32     }
    33     ~Vector()
    34     {
    35         cout << "析构函数 ..." << "(" << x1 << ", " << y1 << ")—>(" << x2 << "," << y2 << ")" << endl;
    36         
    37     }
    38     void dispaly()
    39     {
    40         cout<<"(" << x1<< ", " << y1<<")—>("<<x2<<","<<y2<<")" << endl;
    41     }
    42     void setX1(int x)
    43     {
    44         x1 = x;
    45     }
    46     void setY1(int x)
    47     {
    48         y1 = x;
    49     }
    50     void setX2(int x)
    51     {
    52         x2 = x;
    53 
    54     }
    55     void setY2(int x)
    56     {
    57         y2 = x;
    58     }
    59 };
    60 int main()
    61 {
    62     Vector *p1=new Vector();
    63     p1->setX1(1);
    64     p1->setX2(4);
    65     p1->setY1(7);
    66     p1->setY2(3);
    67     p1->dispaly();
    68     cout << "进行深克隆" << endl;
    69     Vector *p2(p1);
    70 
    71    p2->dispaly();
    72    cout << "判断p1==p2...." << endl;
    73    if (p1 == p2) {
    74        cout << "true" << endl;
    75    }
    76    else {
    77        cout << "flase" << endl;
    78    }
    79     return 0;
    80 }
    DeepClone.cpp

    2、浅克隆(C++)

     1 #include <iostream>
     2 using namespace std;
     3 
     4 //接口
     5 class Vector
     6 {
     7 public:
     8     Vector() {}
     9     virtual ~Vector() {}
    10 
    11     virtual Vector* Clone() = 0;
    12 };
    13 
    14 //实现
    15 class ConcreteVector : public Vector
    16 {
    17 private:
    18     int x1;
    19     int y1;
    20     int x2;
    21     int y2;
    22     int m_counter;
    23 public:
    24     ConcreteVector() :m_counter(0) {}
    25     virtual ~ConcreteVector() {}
    26 
    27     //拷贝构造函数
    28     ConcreteVector(const ConcreteVector& rhs)
    29     {
    30         m_counter = rhs.m_counter;
    31     }
    32    
    33     void set(int a,int b,int c,int d) {
    34         x1 = a;
    35         y1 = b;
    36         x2 = c;
    37         y2 = d;
    38 
    39     }
    40     //复制自身
    41     virtual ConcreteVector* Clone()
    42     {
    43         //调用拷贝构造函数
    44         return new ConcreteVector(*this);
    45     }
    46 
    47     void dispaly()
    48     {
    49         cout << "(" << x1 << ", " << y1 << ")—>(" << x2 << "," << y2 << ")" << endl;
    50     }
    51     
    52 };
    53 
    54 int main()
    55 {
    56     //生成对像
    57     ConcreteVector* conProA = new ConcreteVector();
    58     conProA->set(14, 4, 2, 8);
    59     
    60     //复制自身
    61     ConcreteVector* conProB = conProA->Clone();
    62     conProB->set(14, 4, 2, 8);
    63     cout << "深克隆前...." << endl;
    64     conProA->dispaly();
    65     cout  << endl;
    66     cout << "深克隆后...." << endl;
    67     conProB->dispaly();
    68     cout << "判断p1==p2...." << endl;
    69     if (conProA == conProB) {
    70 
    71         cout << "true" << endl;
    72     }
    73     else {
    74         cout << "flase" << endl;
    75     }
    76     delete conProA;
    77     conProA = NULL;
    78 
    79     delete conProB;
    80     conProB = NULL;
    81 
    82     return 0;
    83 }
    ShallowClone.cpp
  • 相关阅读:
    create joint
    delphi 使用parent让进度条上显示文字
    abSymMeshMEL.txt
    ini写配置信息
    CreateBindGroupNode.txt
    CreateaJointCurve.txt
    09 IKFKMatch.txt
    TIF_to_MAP.BAT
    ImportBVHv20.txt
    FormatDateTime 一段以时间为命令的代码
  • 原文地址:https://www.cnblogs.com/Lizhichengweidashen/p/14904635.html
Copyright © 2020-2023  润新知