虚函数练习--广州军区微波通信系统

一 项目需求

系统拓扑:
image

项目需求:
因为各种不确定原因,包括认为原因,ODU设备会自动的切换到其它类型的设备,而切换后的设备,和原设备有很多不同的地方。如何完美的实现这个切换呢?

解决方案:
使用多态。

二 项目实现

//------------------------  ODU 类 ------------------------
/****    ODU.h   *****/
#pragma once
#include <string>
using namespace std;

#define ODU_TYPE_331_FLAG "331"
#define ODU_TYPE_335_FLAG "335"
enum class ODU_TYPE
{
    ODU_TYPE_331,
    ODU_TYPE_335,
    ODU_TYPE_UNKNOW
};

// ODU类,用于处理老型号ODU331设备
class ODU
{
public:
    ODU();
    
    // 心跳包
    virtual bool heartBeat();  
    // 获取该设备的名称
    virtual string name();
    // 获取当前ODU类型
    ODU_TYPE getODUType();
protected:
    ODU_TYPE type;
};

/****    ODU.cpp   *****/
#include "ODU.h"
#include <iostream>
ODU::ODU()
{
    type = ODU_TYPE::ODU_TYPE_331;
    cout << "调用ODU构造函数" << endl;
}

bool ODU::heartBeat()
{
    cout << name() << "模拟串口协议读取数据,获取心跳包反馈[" << ODU_TYPE_331_FLAG << "]:";
    
    string response;  // 响应
    cin >> response;

    bool ret = false;
    if (response == ODU_TYPE_331_FLAG)
    {
        type = ODU_TYPE::ODU_TYPE_331;
        ret = true;
    }

    return ret;
}

string ODU::name()
{
    string ret;
    switch (type)
    {
    case ODU_TYPE::ODU_TYPE_331:
        ret = "ODU331";
        break;
    case ODU_TYPE::ODU_TYPE_335:
        ret = "ODU335";
        break;
    case ODU_TYPE::ODU_TYPE_UNKNOW:
    default:
        ret = "未知";
        break;
    }
    return ret;
}

ODU_TYPE ODU::getODUType()
{
    return type;
}

//------------------------  ODU335 类 ------------------------
/****    ODU335.cpp   *****/
#pragma once
#include "ODU.h"

class ODU335 :public ODU
{
public:
    ODU335();

    bool heartBeat();
};

/****    ODU335.cpp   *****/
#include <iostream>
#include "ODU335.h"

ODU335::ODU335()
{
    type = ODU_TYPE::ODU_TYPE_335;
    cout << "调用ODU335的构造函数" << endl;
}

bool ODU335::heartBeat()
{
    cout << name() << "模拟串口协议读取数据,获取心跳包反馈[" << ODU_TYPE_335_FLAG << "]:";

    string response;  // 响应
    cin >> response;

    bool ret = false;
    if (response == ODU_TYPE_335_FLAG)
    {
        type = ODU_TYPE::ODU_TYPE_335;
        ret = true;
    }

    return ret;
}

//------------------------  main.cpp  ------------------------
#include <iostream>
#include <thread>   // 线程
#include <Windows.h>
#include "ODU.h"
#include "ODU335.h"

using namespace std;

ODU* odu = NULL;
void oduMonitorHandler()
{
    while (1)
    {
        // 当odu的类型是335的时候,父类的指针指向子类的对象,因为heartBeat是虚函数,所以实现多态
        if (odu->heartBeat() == false)
        {
            // 切换odu
            ODU_TYPE type = odu->getODUType();
            switch (type)
            {
            case ODU_TYPE::ODU_TYPE_331:
                delete odu;
                odu = new ODU335();
                break;
            case ODU_TYPE::ODU_TYPE_335:
                delete odu;
                odu = new ODU();
                break;
            case ODU_TYPE::ODU_TYPE_UNKNOW:
            default:
                odu = NULL;
                return;
            }
        }

        Sleep(3000);
    }
}

int main()
{
    odu = new ODU();

    // 创建一个线程,对ODU进行监测
    thread oduMonitor(oduMonitorHandler);

    // 主线程等待线程 oduMonitor 的结束
    oduMonitor.join();

    return 0;
}

image

posted @ 2022-05-11 05:47  荒年、  阅读(36)  评论(0编辑  收藏  举报