0
点赞
收藏
分享

微信扫一扫

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


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

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

// 工厂方法模式
// 对象类部分
class LeiFeng {
public:
virtual void work() {
std::cout << "LeiFeng do work" << std::endl;
}
};

class Student: public LeiFeng {
public:
virtual void work() {
std::cout << "Student do work" << std::endl;
}
};

class Volunteer: public LeiFeng {
public:
virtual void work() {
std::cout << "Volunteer do work" << std::endl;
}
};

// 工厂类部分
class LeiFengFactory {
public:
static LeiFeng* createLeiFeng() {
return new LeiFeng();
}
};

class StudentFactory: LeiFengFactory {
public:
static Student* createLeiFeng() {
return new Student();
}
};

class VolunteerFactory: LeiFengFactory {
public:
static Volunteer* createLeiFeng() {
return new Volunteer();
}
};

int main() {

StudentFactory* studentFactory = new StudentFactory();
LeiFeng* leiFeng = studentFactory->createLeiFeng();

leiFeng->work();

delete leiFeng;
delete studentFactory;

return 0;
}

举报

相关推荐

0 条评论