zoukankan      html  css  js  c++  java
  • [创建型] 原型模式

    所谓原型模型,就是需要创建大量相同或者相似对象时,可以通过对一个已有对象的复制获取更多的对象。也就是所谓clone。

    代码如下:

    View Code
     1 #include <iostream>
    2 #include <string>
    3 using namespace std;
    4 // 原型抽象类
    5 class CPrototype
    6 {
    7 public:
    8 CPrototype(){}
    9 virtual ~CPrototype(){}
    10 virtual CPrototype* Clone() = 0;
    11 void fun()
    12 {
    13 cout << "我是一个克隆体" << endl;
    14 }
    15 };
    16
    17 // 具体原型类1
    18 class CConcretePrototype1 : public CPrototype
    19 {
    20 public:
    21 CConcretePrototype1(){}
    22 virtual ~CConcretePrototype1() {}
    23 virtual CPrototype* Clone()
    24 {
    25 CConcretePrototype1 *pClone = new CConcretePrototype1;
    26 *pClone = *this;
    27 return pClone;
    28 }
    29 };
    30
    31 // 具体原型类2
    32 class CConcretePrototype2 : public CPrototype
    33 {
    34 public:
    35 CConcretePrototype2(){}
    36 virtual ~CConcretePrototype2(){}
    37 virtual CPrototype* Clone()
    38 {
    39 CConcretePrototype2 *pClone = new CConcretePrototype2;
    40 *pClone = *this;
    41 return pClone;
    42 }
    43 };
    44 void main()
    45 {
    46 CPrototype * a1;
    47 CConcretePrototype1 *b1 = new CConcretePrototype1;
    48 a1 = b1->Clone();
    49 a1->fun();
    50
    51 CPrototype * a2;
    52 CConcretePrototype1 *b2 = new CConcretePrototype1;
    53 a2 = b2->Clone();
    54 a2->fun();
    55 }

      我是一个克隆体

      我是一个克隆体


     

  • 相关阅读:
    深入单机事务
    事务的本质
    云计算是什么?
    linux命令之tail
    linux命令之rpm
    String、StringBuffer与StringBuilder区别
    自定义今天的520
    logback中MDC使用
    数组(转载)
    spring获取webapplicationcontext,applicationcontext几种方法详解(转载)
  • 原文地址:https://www.cnblogs.com/xuxu8511/p/2405810.html
Copyright © 2011-2022 走看看