亚马逊SDE2低阶设计面试:Alexa设备电源状态报告类设计优化
面向对象优化方案:Alexa设备电源/电池状态报告系统
面试问题要求
- Alexa设备分为多种硬件类型:仅带扬声器、仅带显示屏、同时带扬声器与显示屏、两者皆无、带扬声器且可外接显示屏。
- 设备分为带电池和无电池两类,状态消息需区分这两种情况。
- 根据硬件配置输出对应形式的状态:仅扬声器则语音播报,仅显示屏则显示,两者兼具则同时输出,且不能调用不存在的硬件功能。
- 设计需具备扩展性,新增带LED显示屏阵列的设备时,只需最小改动现有代码。
原方案的扩展性问题
- 硬编码设备子类:每个设备都需要继承
Device并重写printStatus,新增设备时必须创建新类,违反开闭原则。 - 电源状态无统一接口:
Battery和Power没有共同基类,设备类需要单独处理两种电源类型,新增电源类型(如太阳能)时改动成本高。 - 输出组件管理不一致:部分设备用单个成员变量(如
EchoDot的speaker),部分用列表(如FireTV的outputs),逻辑重复且维护性差。 - 状态生成与输出耦合:设备类中直接生成状态消息并调用输出,职责不单一,修改消息格式或输出逻辑需要改动多个类。
优化后的C++实现方案
1. 统一电源状态接口
#include <string> #include <vector> #include <memory> #include <iostream> // 统一电源状态抽象接口 class PowerSource { public: virtual ~PowerSource() = default; virtual std::string getStatusMessage() const = 0; }; // 有线电源实现(无电池设备) class WiredPower : public PowerSource { private: bool isCharging; // 仅对支持充电的有线设备有效 public: WiredPower(bool charging = false) : isCharging(charging) {} std::string getStatusMessage() const override { return isCharging ? "设备正在充电" : "设备已连接电源"; } }; // 电池电源实现(带电池设备) class BatteryPower : public PowerSource { private: int percentage; bool isCharging; public: BatteryPower(int pct, bool charging = false) : percentage(pct), isCharging(charging) {} std::string getStatusMessage() const override { std::string msg = "电池电量: " + std::to_string(percentage) + "%"; if (isCharging) msg += " (正在充电)"; return msg; } };
2. 输出组件抽象与实现
// 输出组件抽象接口 class OutputComponent { public: virtual ~OutputComponent() = default; virtual void render(const std::string& message) const = 0; }; // 扬声器输出实现 class Speaker : public OutputComponent { public: void render(const std::string& message) const override { std::cout << "[扬声器] 播报: " << message << std::endl; } }; // 显示屏输出实现 class Display : public OutputComponent { public: void render(const std::string& message) const override { std::cout << "[显示屏] 显示: " << message << std::endl; } }; // 新增LED阵列输出(无需修改原有核心类) class LEDArray : public OutputComponent { public: void render(const std::string& message) const override { std::cout << "[LED阵列] 滚动显示: " << message << std::endl; } };
3. 基础设备类(组合模式)
// 核心设备类,通过组合组件实现功能,无需为每个设备新建子类 class AlexaDevice { private: std::unique_ptr<PowerSource> powerSource; std::vector<std::unique_ptr<OutputComponent>> outputs; public: AlexaDevice(std::unique_ptr<PowerSource> ps) : powerSource(std::move(ps)) {} // 动态添加输出组件,支持外接设备扩展 void addOutput(std::unique_ptr<OutputComponent> output) { outputs.push_back(std::move(output)); } // 统一状态报告逻辑,无需重写 void reportStatus() const { if (!powerSource) return; std::string statusMsg = powerSource->getStatusMessage(); for (const auto& output : outputs) { output->render(statusMsg); } } };
4. 设备工厂(快速创建不同设备)
// 设备工厂,封装不同设备的组件组合逻辑 class DeviceFactory { public: static std::unique_ptr<AlexaDevice> createEchoDot() { auto device = std::make_unique<AlexaDevice>(std::make_unique<WiredPower>()); device->addOutput(std::make_unique<Speaker>()); return device; } static std::unique_ptr<AlexaDevice> createEchoShow() { auto device = std::make_unique<AlexaDevice>(std::make_unique<WiredPower>()); device->addOutput(std::make_unique<Speaker>()); device->addOutput(std::make_unique<Display>()); return device; } static std::unique_ptr<AlexaDevice> createFireTablet(int batteryPct, bool isCharging) { auto device = std::make_unique<AlexaDevice>(std::make_unique<BatteryPower>(batteryPct, isCharging)); device->addOutput(std::make_unique<Speaker>()); device->addOutput(std::make_unique<Display>()); return device; } // 新增带LED阵列的设备,只需添加工厂方法 static std::unique_ptr<AlexaDevice> createAlexaLEDClock(int batteryPct) { auto device = std::make_unique<AlexaDevice>(std::make_unique<BatteryPower>(batteryPct)); device->addOutput(std::make_unique<LEDArray>()); return device; } };
5. 使用示例
int main() { auto echoDot = DeviceFactory::createEchoDot(); echoDot->reportStatus(); // 输出: [扬声器] 播报: 设备已连接电源 auto fireTablet = DeviceFactory::createFireTablet(75, true); fireTablet->reportStatus(); // 输出: // [扬声器] 播报: 电池电量: 75% (正在充电) // [显示屏] 显示: 电池电量: 75% (正在充电) auto ledClock = DeviceFactory::createAlexaLEDClock(40); ledClock->reportStatus(); // 输出: [LED阵列] 滚动显示: 电池电量: 40% return 0; }
优化设计核心思路
- 组合优于继承:将电源、输出等功能抽象为独立组件,设备通过组合组件实现功能,避免子类爆炸。
- 统一接口抽象:所有电源类型实现
PowerSource,所有输出类型实现OutputComponent,确保新增类型时无需修改核心逻辑。 - 开闭原则:新增设备只需通过工厂函数组合现有组件;新增电源/输出类型只需实现对应接口,完全符合扩展性要求。
- 动态扩展支持:
AlexaDevice支持动态添加输出组件,满足可外接设备的需求。
内容的提问来源于stack exchange,提问作者sachin
相关产品推荐
相关产品推荐

