Language / 语言选择:
本仓库是一个 C++ 课程实验项目,围绕一个简化的“车辆 / ADAS 控制系统”展开。
核心是对车辆位置与朝向的建模(Pose)、执行器 Executor、命令系统 Cmder,以及多种车辆行为(普通车、公交车、跑车)。
项目同时配有 Google Test 单元测试,便于你通过测试来验证每一步实验结果。
仓库主要目录说明(以 task_1 目录为根):
src/core/Direction.hpp / .cpp:方向枚举与移动向量(E/S/W/N)。Point.hpp / .cpp:平面坐标点 (x, y)。PoseHandler.hpp / .cpp:封装Pose,负责前进、后退、转向、加速等状态更新。Singleton.hpp:通用单例模板,用于CmderFactory等。
cmder/Command.hpp:命令接口ICommand以及MoveCommand/TurnLeftCommand/TurnRightCommand/FastCommand等具体命令。ActionGroup.hpp / .cpp:把一系列基本动作打包成“动作组”并执行。CmderFactory.hpp / .cpp:把命令字符串解析为一组可执行命令对象(命令工厂)。BusOrchestrator.hpp、NormalOrchestrator.hpp、SportsCarOrchestrator.hpp:根据车辆类型组织命令执行的策略入口。CmderOrchestrator.hpp、Command.hpp等:命令调度相关抽象。
ExecutorImpl.hpp / .cpp:Executor的具体实现,持有PoseHandler和CmderOrchestrator,对外提供Execute/Query等接口。
include/Executor.hpp:对外暴露的执行器接口与Pose、ExecutorType等类型声明。
tests/- 使用 Google Test 编写的单元测试,例如:
ExecutorTest.cppExecutorReverseTest.cppExecutorFastTest.cppSportsCarTest.cppBusTest.cpp
googletest/:GoogleTest 源码(作为子目录构建)。
- 使用 Google Test 编写的单元测试,例如:
CMakeLists.txt&src/CMakeLists.txt&tests/CMakeLists.txt:CMake 构建脚本。script/build.bat:构建脚本。run.bat:运行脚本(一般执行主程序或测试)。clean.bat:清理构建输出。
从整体上看,这个项目模拟了“车辆在平面网格上运动”的过程:
-
位姿建模:
- 使用
Point表示位置,Direction表示朝向(E/S/W/N)。 PoseHandler内部持有Pose,对外提供Forward、Backward、TurnLeft、TurnRight、Fast、Reverse等方法来修改状态。
- 使用
-
命令系统:
- 每个命令字符(例如
M,L,R,B,F等)对应一个具体的ICommand实现或一个std::function,封装了一步操作。 CmderFactory/ActionGroup把字符串解析为命令列表,然后逐个对PoseHandler执行。
- 每个命令字符(例如
-
执行器
Executor:- 对外暴露简洁接口:
Executor::NewExecutor(const Pose &, ExecutorType):构造不同类型的执行器(公交车、跑车、普通车)。void Execute(const std::string &commands):执行命令串。Pose Query() const:查询当前车辆位置与朝向。
ExecutorImpl内部使用CmderOrchestrator来决定不同类型车辆的行为策略(例如跑车的加速规则与公交车不同)。
- 对外暴露简洁接口:
-
多种车辆类型:
ExecutorType::BUS/SPORTS_CAR/NORMAL对应不同 Orchestrator。- 各自有不同的“命令解释方式”或动作组合方式,用以演示“面向对象+策略模式”的用法。
-
测试驱动:
- 每个实验(lab)配套若干测试文件,例如
ExecutorReverseTest针对后退逻辑,ExecutorTurnRoundTest针对转向/掉头逻辑等。
- 每个实验(lab)配套若干测试文件,例如
以下命令都在 task_1 目录下执行。
Windows 下可以直接双击或在 PowerShell 中执行:
cd C:\Users\Owner\code\project\C++Course\task_1
# 构建
script\build.bat
# 运行(通常是主程序或者测试入口)
script\run.bat
# 清理
script\clean.bat项目已经提供了 CMake 配置,可以在 VS Code 或命令行直接调用:
cd C:\Users\Owner\code\project\C++Course\task_1
# 配置(Debug,MinGW Makefiles)
cmake -DCMAKE_BUILD_TYPE=Debug -G "MinGW Makefiles" -S . -B build
# 编译
cmake --build build如果你使用 VS Code 中的 cmake configure 任务,也会做类似的事情。
构建完成后,可以在 build/tests 目录下找到测试可执行文件(通常是一个 ${PROJECT_NAME}-main,运行时会执行所有 Google Test 用例):
cd C:\Users\Owner\code\project\C++Course\task_1\build\tests
# 直接运行测试 EXE
.\nC-Course-task_1-main.exe
# 或使用 ctest(需在 build 目录下)
cd ..
ctest在 VS Code 中,如果安装了 C++ TestMate / Google Test 相关扩展,也可以在 Testing 面板中看到所有测试并单独调试某一个用例。
本仓库包含多个分支,对应不同实验阶段或重构版本。下面是每个分支的建议说明(名称即 Git 分支名):
- 当前主线分支。
- 通常包含所有实验完成后的参考实现,代码结构已经相对完整:
Executor+CmderOrchestrator+ 各类Command+ActionGroup+ 单例工厂等。
- 适合作为“总览代码架构”和“查阅最终实现”的入口。
- 实验 1:代码整洁基础。
- 目标:
- 从一个相对原始的实现出发,进行命名优化、函数拆分、去除重复代码等。
- 强调“可读性”“单一职责”“小函数”等 Clean Code 概念。
- 特点:
- 架构较简单,更多是对原始逻辑的整理与重构。
- 实验 2(阶段一):面向对象扩展:支持
F(Fast)命令。 - 目标:
- 在命令系统中增加一个“加速/快速移动”相关的语义,通常是
F命令。 - 探索如何通过新增
FastCommand/ 修改PoseHandler::Fast()等方式扩展行为,而尽量不破坏已有结构。
- 在命令系统中增加一个“加速/快速移动”相关的语义,通常是
- 特点:
- 在
Command.hpp/PoseHandler.hpp附近可看到与Fast行为相关的改动。
- 在
- 实验 2(阶段二):面向对象的三个特性综合运用。
- 关注点:
- 继承:例如
ICommand接口与多个具体命令类。 - 封装:
PoseHandler封装位姿细节、ExecutorImpl封装执行流程。 - 多态:通过基类指针/引用调用不同子类的
DoOperate。
- 继承:例如
- 特点:
- 分支中会更多体现接口抽象、虚函数、多态分发的用法。
- 实验 3(阶段一):函数式编程风格。
- 目标:
- 使用
std::function、lambda、std::for_each等来实现命令调度。 - 减少显式的
if-else/switch,改用“表驱动 + 函数对象”的方式。
- 使用
- 示例:
std::unordered_map<char, std::function<void(PoseHandler &)>>映射命令字符到操作。std::for_each+ lambda 执行命令序列。
- 实验 3(阶段二):在函数式风格下支持
B(Backward / Reverse)命令。 - 目标:
- 在 FP 风格的架构上,平滑扩展一个新命令(例如后退或反向相关逻辑)。
- 观察:仅增加映射表项 + 对应动作逻辑,即可扩展功能,验证 FP 在扩展性上的优势。
- 特点:
- 在
ActionGroup/CmderFactory之类的地方会看到针对B的处理。
- 在
- 实验 3(阶段三):面向对象与函数式的综合重构版本。
- 目标:
- 在实践 OOP + FP 的过程中,对项目进行再一次结构化重构。
- 清晰划分:
- “状态”由谁维护(
PoseHandler)。 - “动作”如何组织(
ActionGroup/Command/std::function)。 - “策略”如何注入(
CmderOrchestrator+ExecutorImpl)。
- “状态”由谁维护(
- 特点:
- 代码风格更统一,职责划分更清晰,便于阅读和扩展。
- 实验 4:综合实战。
- 目标:
- 把前面各实验中的知识点(Clean Code、OOP、FP)综合运用在一个稍大型的场景中。
- 增强对“从需求到设计再到实现”的完整流程的体验。
- 特点:
- 可能包含更多的业务规则、边界情况处理。
- 测试用例更加丰富和贴近“真实需求”。
建议的学习路线:
- 从
lab1-cleancode-base开始,关注命名与基本结构; - 切到
lab2-oop-support-F、lab2-oop-three-features,体会如何通过 OOP 扩展行为; - 再看
lab3-fp、lab3-fp-support-B,体会用 FP 风格来替代 if-else / switch 的好处; - 阅读
lab3-oop-recfactor-final,理解一个综合重构后的较优架构; - 最后看
lab4-comprehensive-actual-combat和main,把所有内容串联起来。
切换分支时的命令示例:
# 查看所有分支
git branch
# 切换到某个实验分支
git checkout lab3-fp如果你想在此基础上继续扩展,可以尝试:
- 增加障碍物 / 地图边界检测;
- 增加更多车辆类型(如卡车、自行车),让
ExecutorType与 Orchestrator 更加多样; - 引入日志系统,对每一步动作记录日志;
- 使用 CI(如 GitHub Actions)自动构建并运行测试。
欢迎在 GitHub 上使用 Issue / Pull Request 记录你的想法和改动。希望这个项目能帮助你更好地理解 C++ 的面向对象、函数式编程以及整洁代码实践。
This repository is a C++ course project that builds a simplified "vehicle / ADAS control system".
It focuses on modeling vehicle pose (Pose), the executor (Executor), the command system (Cmder),
and different vehicle behaviors (normal car, bus, sports car).
Main directories (rooted at task_1):
src/core/Direction.hpp / .cpp: Direction enum and motion vectors (E/S/W/N).Point.hpp / .cpp: 2D point (x, y).PoseHandler.hpp / .cpp: WrapsPose, responsible for forward/backward/turn/fast/reverse operations.Singleton.hpp: Generic singleton template, used byCmderFactoryetc.
cmder/Command.hpp: Command interfaceICommandand concrete commands likeMoveCommand,TurnLeftCommand,TurnRightCommand,FastCommand.ActionGroup.hpp / .cpp: Packs a sequence of basic actions into an action group and executes them.CmderFactory.hpp / .cpp: Parses command strings into executable command objects (command factory).BusOrchestrator.hpp,NormalOrchestrator.hpp,SportsCarOrchestrator.hpp: orchestrators for different vehicle types.CmderOrchestrator.hpp,Command.hpp: abstractions around command orchestration.
ExecutorImpl.hpp / .cpp: Concrete implementation ofExecutor, holding aPoseHandlerand aCmderOrchestrator, exposingExecuteandQuery.
include/Executor.hpp: Public executor interface, and declarations ofPose,ExecutorType, etc.
tests/- Google Test based unit tests, e.g.:
ExecutorTest.cppExecutorReverseTest.cppExecutorFastTest.cppSportsCarTest.cppBusTest.cpp
googletest/: GoogleTest source as a subdirectory.
- Google Test based unit tests, e.g.:
CMakeLists.txt,src/CMakeLists.txt,tests/CMakeLists.txt: CMake build scripts.script/build.bat: Build script.run.bat: Run script (main program or tests).clean.bat: Clean build outputs.
Conceptually, this project simulates a vehicle moving on a 2D grid:
-
Pose modeling
Pointrepresents position;Directionrepresents heading (E/S/W/N).PoseHandlerowns aPoseand provides operations likeForward,Backward,TurnLeft,TurnRight,Fast,Reverse.
-
Command system
- Each command character (e.g.
M,L,R,B,F) maps to a concreteICommandimplementation or astd::functionthat performs one atomic operation. CmderFactory/ActionGroupconverts a string into a list of commands and executes them one by one onPoseHandler.
- Each command character (e.g.
-
Executor
- A simple public API:
Executor::NewExecutor(const Pose &, ExecutorType): construct executors for bus, sports car, or normal car.void Execute(const std::string &commands): execute a command sequence.Pose Query() const: get current pose (position and heading).
- Internally,
ExecutorImpluses aCmderOrchestratorto decide how different vehicle types interpret and organize commands.
- A simple public API:
-
Multiple vehicle types
ExecutorType::BUS/SPORTS_CAR/NORMALcorrespond to different orchestrators, demonstrating the strategy pattern.
-
Tests
- Each lab/step is backed by Google Test cases, e.g. reverse logic, turn-around logic, fast movement, sports car behavior, bus behavior, etc.
All commands below assume the working directory is task_1.
cd C:\Users\Owner\code\project\C++Course\task_1
# Build
script\build.bat
# Run (main program or test entry)
script\run.bat
# Clean
script\clean.batcd C:\Users\Owner\code\project\C++Course\task_1
# Configure (Debug, MinGW Makefiles)
cmake -DCMAKE_BUILD_TYPE=Debug -G "MinGW Makefiles" -S . -B build
# Build
cmake --build buildAfter building, you can run tests in build/tests (usually one ${PROJECT_NAME}-main
executable that runs all Google Test cases):
cd C:\Users\Owner\code\project\C++Course\task_1\build\tests
./C-Course-task_1-main.exe # or the actual test executable name
cd ..
ctestIn VS Code, with a Google Test extension installed, you can also discover and run individual test cases from the Testing panel.
This repository contains several branches, each representing a lab step or a refactoring stage.
main: consolidated version with all features and refactorings.lab1-cleancode-base: focus on clean code basics (naming, small functions, SRP).lab2-oop-support-F: extend OOP design to supportF(fast) command.lab2-oop-three-features: practice inheritance, encapsulation, and polymorphism.lab3-fp: introduce functional style (lambdas,std::function, table-driven dispatch).lab3-fp-support-B: extend the FP-style design to supportB(backward / reverse).lab3-oop-recfactor-final: combined OOP + FP refactoring version.lab4-comprehensive-actual-combat: comprehensive lab, applying all previous techniques.
You can switch branches with:
git branch # list branches
git checkout lab3-fpSuggested path:
- Start from
lab1-cleancode-baseto understand the basic structure and clean code ideas. - Move to
lab2-oop-support-Fandlab2-oop-three-featuresto see how OOP extends behavior. - Then read
lab3-fpandlab3-fp-support-Bfor functional-style command handling. - Look at
lab3-oop-recfactor-finalfor a more polished architecture. - Finally, study
lab4-comprehensive-actual-combatandmainas comprehensive examples.
Ideas for further extension:
- Add obstacles or map boundaries.
- Add more vehicle types (truck, bike, etc.).
- Add logging for each action.
- Integrate CI (e.g., GitHub Actions) to build and run tests automatically.
Issues and pull requests are welcome if you use this project as a practice playground.