0
点赞
收藏
分享

微信扫一扫

量化交易之设计模式篇 - 装饰模式

东林梁 2023-03-02 阅读 123


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

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

class Person {
public:
Person() {};
virtual ~Person() {};

Person(string name) {
this->m_name = name;
}

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

private:
string m_name;
};

// 装饰类
class Finery: public Person {
public:
void decorate(Person* person) {
this->m_person = person;
}

virtual void show() {
this->m_person->show();
}

protected:
Person* m_person;
};

class TShirts: public Finery {
public:
virtual void show() {
std::cout << "TShirts" << std::endl;
this->m_person->show();
}
};

class BigTrouser: public Finery {
public:
virtual void show() {
std::cout << "BigTrouser" << std::endl;
this->m_person->show();
}
};


int main() {

Person* person = new Person("tqz");
TShirts* tShirts = new TShirts();
BigTrouser* bigTrouser = new BigTrouser();

bigTrouser->decorate(person);
tShirts->decorate(bigTrouser);
tShirts->show();

return 0;
}

举报

相关推荐

0 条评论