0
点赞
收藏
分享

微信扫一扫

量化交易之设计模式篇 - 原型模式


//
// Created by win10 on 2021/11/16.
//

#include <thread>
#include <iostream>
using namespace std;

// 原型模式
class Prototype {
public:
Prototype(): m_string("") {}
Prototype(string str): m_string(str) {}
virtual ~Prototype() = default;

void show() {
std::cout << "show: " << this->m_string << std::endl;
}

virtual Prototype* clone() = 0;

private:
string m_string;
};

class ConcretePrototype1: public Prototype {
public:
ConcretePrototype1() {};
ConcretePrototype1(string str): Prototype(str) {};

virtual Prototype* clone() {
ConcretePrototype1* newObj = new ConcretePrototype1();
*newObj = *this;
return newObj;
}
};
class ConcretePrototype2: public Prototype {
public:
ConcretePrototype2() {};
ConcretePrototype2(string str):Prototype(str) {};

virtual Prototype* clone() {
ConcretePrototype2* newObj = new ConcretePrototype2();
*newObj = *this;
return newObj;
}
};

int main() {

ConcretePrototype1* test = new ConcretePrototype1("tqz");
ConcretePrototype2* test2 = dynamic_cast<ConcretePrototype2 *>(test->clone());

test->show();
test2->show();

delete test;
delete test2;

return 0;
}

举报

相关推荐

0 条评论