新建了策略工厂类

This commit is contained in:
jackfiled 2022-06-27 14:21:07 +08:00
parent ad37da5974
commit 672e26d175
4 changed files with 170 additions and 0 deletions

22
include/StrategyFactory.h Normal file
View File

@ -0,0 +1,22 @@
//
// Created by ricardo on 2022/6/27.
//
#ifndef AUTO_BUS_GUI_STRATEGY_FACTORY_H
#define AUTO_BUS_GUI_STRATEGY_FACTORY_H
#include "cstdio"
#include "QByteArray"
#include "BusStrategyBase.h"
#include "BusFCFSStrategy.h"
#include "BusSSTFStrategy.h"
#include "BusSCANStrategy.h"
class StrategyFactory
{
public:
static BusStrategyBase *GetStrategy(const QString& file_name);
};
#endif //AUTO_BUS_GUI_STRATEGY_FACTORY_H

View File

@ -45,6 +45,8 @@ public:
*/ */
int node_num; int node_num;
int total_distance;
explicit RailsModel(); explicit RailsModel();
~RailsModel(); ~RailsModel();

144
src/StrategyFactory.cpp Normal file
View File

@ -0,0 +1,144 @@
//
// Created by ricardo on 2022/6/27.
//
#include "StrategyFactory.h"
BusStrategyBase *StrategyFactory::GetStrategy(const QString& file_name)
{
QByteArray bytes = file_name.toLatin1();
FILE *config_file = NULL;
char buffer[30];
int total_station = 0;
int distance = 0;
int chosen_strategy = 0;
fopen_s(&config_file, bytes.data(), "r");
// 循环读取文件的每一行
while (fgets(buffer, sizeof buffer, config_file) != NULL)
{
char first_char = buffer[0];
char *p;
switch (first_char)
{
case '#':
// 如果读取到#什么都不做
break;
case 'T':
// TOTAL_STATION
p = buffer;
// 把数字前面的所有字符全部干掉
while (*p < '0' || *p > '9')
{
p++;
}
// 讲道理,应该只有两位数,所以就这样处理了
if (*(p + 1) == '\n' || *(p + 1) == '\0')
{
total_station = *p - 48;
}
else
{
total_station = (*p - 48) * 10 + *(p + 1) - 48;
}
break;
case 'S':
// STRATEGY
p = buffer;
// 将=前的字符全部略去
while (*p != '=')
{
p++;
}
// =也去掉
p++;
// =和策略之间的空格也去掉
while (*p == ' ')
{
p++;
}
if (*p == 'F' && *(p + 1) == 'C') //FCFS
{
chosen_strategy = BUS_FCFS;
}
else if (*p == 'S' && *(p + 1) == 'S') //SSTF
{
chosen_strategy = BUS_SSTF;
}
else if (*p == 'S' && *(p + 1) == 'C') //SCAN
{
chosen_strategy = BUS_SCAN;
}
else
{
// 读取失败
chosen_strategy = -1;
}
break;
case 'D':
// DISTANCE
p = buffer;
// 把数字前面的所有字符全部干掉
while (*p < '0' || *p > '9')
{
p++;
}
if (*(p + 1) == '\n' || *(p + 1) == '\0')
{
distance = *p - 48;
}
break;
default:
continue;
}
}
// 处理参数去缺省值的情况
if (distance == 0)
{
distance = 2;
}
if (total_station == 0)
{
total_station = 5;
}
if(chosen_strategy == -1)
{
chosen_strategy = BUS_FCFS;
}
BusStrategyBase *controller;
switch (chosen_strategy)
{
case BUS_FCFS:
controller = new BusFCFSStrategy;
break;
case BUS_SSTF:
controller = new BusSSTFStrategy;
break;
case BUS_SCAN:
controller = new BusSCANStrategy;
default:
controller = nullptr;
break;
}
if(controller != nullptr)
{
controller->rails_model->CreateRails(distance, total_station);
}
return controller;
}

View File

@ -8,6 +8,7 @@ RailsModel::RailsModel()
{ {
rails = nullptr; rails = nullptr;
node_num = 0; node_num = 0;
total_distance = 0;
} }
RailsModel::~RailsModel() RailsModel::~RailsModel()
@ -76,6 +77,7 @@ void RailsModel::CreateRails(int distance, int node_number)
rails = head; rails = head;
node_num = node_number; node_num = node_number;
total_distance = node_number * distance;
} }
void RailsModel::FreeRails() void RailsModel::FreeRails()