量化交易之设计模式篇 - 模板方法模式

阅读 77

2023-03-02


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

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

/* 模板方式模式
* 把不变的代码部分都转移到父类中, 将可变的代码用virtual留到子类中重写
*/
class AbstractClass {
public:
void show() {
std::cout << "show: " << this->getName() << std::endl;
}

protected:
virtual string getName() = 0;
};

class Naruto: public AbstractClass {
protected:
virtual string getName() {
return "uzumaki naruto.";
}
};

class Luhi: public AbstractClass {
protected:
virtual string getName() {
return "monky d luhi.";
}
};

int main() {
Naruto* naruto = new Naruto();
Luhi* luhi = new Luhi();

naruto->show();
luhi->show();

delete naruto;
delete luhi;

return 0;
}

精彩评论(0)

0 0 举报