diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..70e34ec --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "C_Cpp.errorSquiggles": "disabled" +} \ No newline at end of file diff --git a/c++/abstract_factory.cc b/c++/abstract_factory.cc new file mode 100644 index 0000000..3a139d2 --- /dev/null +++ b/c++/abstract_factory.cc @@ -0,0 +1,38 @@ +/** + * @name 抽象工厂模式 + * @brief: 创建相关对象族 + * @note 场景:跨平台UI组件库(如 Windows/macOS 按钮+文本框组合) + */ + +class Button { +public: + virtual void render() = 0; +}; +class WinButton : public Button { +public: + void render() override {} +}; +class MacButton : public Button { +public: + void render() override {} +}; + +class GUIFactory { +public: + virtual Button* createButton() = 0; +}; +class WinFactory : public GUIFactory { + Button* createButton() override { return new WinButton(); } +}; +class MacFactory : public GUIFactory { + Button* createButton() override { return new MacButton(); } +}; + +int main() +{ + // 使用 + GUIFactory* factory = new WinFactory(); // 根据配置切换 + Button* btn = factory->createButton(); + btn->render(); + return 0; +} \ No newline at end of file diff --git a/c++/adapter.cc b/c++/adapter.cc new file mode 100644 index 0000000..e84128a --- /dev/null +++ b/c++/adapter.cc @@ -0,0 +1,32 @@ +/** + * @name 适配器模式 + * @brief: 转换接口以兼容客户端 + * @note 场景:集成旧库、第三方库接口转换 + */ + +// 旧类 (不兼容接口) +class LegacyPrinter { +public: + void printDocument() { /* 旧式打印 */ } +}; + +class PrinterInterface { +public: + virtual void print() = 0; +}; + +// 适配器 +class PrinterAdapter : public PrinterInterface { + LegacyPrinter legacyPrinter; +public: + void print() override { + legacyPrinter.printDocument(); // 调用旧方法 + } +}; + +// 使用 +int main() +{ + PrinterInterface* printer = new PrinterAdapter(); + printer->print(); +} \ No newline at end of file diff --git a/c++/builder.cc b/c++/builder.cc new file mode 100644 index 0000000..fe9e678 --- /dev/null +++ b/c++/builder.cc @@ -0,0 +1,54 @@ +/** + * @name 建造者模式 + * @brief: 分步构造复杂对象 + * @note 场景:创建包含多个配置选项的对象(如电脑配置、订单) + */ + +class Pizza { +public: + void setDough(const std::string& d) { dough = d; } + void setSauce(const std::string& s) { sauce = s; } + void addTopping(const std::string& t) { toppings.push_back(t); } +private: + std::string dough; + std::string sauce; + std::vector toppings; +}; + +class PizzaBuilder { +public: + virtual void buildDough() = 0; + virtual void buildSauce() = 0; + virtual void buildToppings() = 0; + Pizza* getPizza() { return pizza; } +protected: + Pizza* pizza = new Pizza(); +}; + +class HawaiianBuilder : public PizzaBuilder { +public: + void buildDough() override { pizza->setDough("cross"); } + void buildSauce() override { pizza->setSauce("mild"); } + void buildToppings() override { + pizza->addTopping("ham"); + pizza->addTopping("pineapple"); + } +}; + +class Director { +public: + Pizza* makePizza(PizzaBuilder* builder) { + builder->buildDough(); + builder->buildSauce(); + builder->buildToppings(); + return builder->getPizza(); + } +}; + +// 使用 +int main() +{ + Director director; + HawaiianBuilder builder; + Pizza* pizza = director.makePizza(&builder); +} \ No newline at end of file diff --git a/c++/chain_responsibility.cc b/c++/chain_responsibility.cc new file mode 100644 index 0000000..39aad2f --- /dev/null +++ b/c++/chain_responsibility.cc @@ -0,0 +1,45 @@ +/** + * @name 责任链模式 + * @brief: 使多个对象都有机会处理请求 + * @note 场景:日志过滤系统、审批流程、异常处理链 + */ + +class Handler { +public: + virtual void handleRequest(int level) = 0; + void setNext(Handler* h) { next = h; } +protected: + Handler* next = nullptr; +}; + +class Manager : public Handler { +public: + void handleRequest(int level) override { + if (level <= 5) { + // 处理请求 + } else if (next) { + next->handleRequest(level); + } + } +}; + +class Director : public Handler { + void handleRequest(int level) override { + if (level > 5 && level <= 10) { + // 处理请求 + } else if (next) { + next->handleRequest(level); + } + } +}; + +// 使用 +int main() +{ + Handler* manager = new Manager(); + Handler* director = new Director(); + manager->setNext(director); + + manager->handleRequest(3); // Manager处理 + manager->handleRequest(7); // Director处理 +} \ No newline at end of file diff --git a/c++/command.cc b/c++/command.cc new file mode 100644 index 0000000..2b91fc1 --- /dev/null +++ b/c++/command.cc @@ -0,0 +1,36 @@ +/** + * @name 命令模式 + * @brief: 将请求封装为对象 + * @note 场景:GUI 按钮操作、撤销/重做功能、任务队列 + */ + +class Command { +public: + virtual void execute() = 0; +}; + +class Light { +public: + void turnOn() {} +}; + +class LightOnCommand : public Command { + Light* light; +public: + void execute() override { light->turnOn(); } +}; + +class RemoteControl { + Command* command; +public: + void setCommand(Command* cmd) { command = cmd; } + void pressButton() { command->execute(); } +}; + +// 使用 +int main() +{ + RemoteControl remote; + remote.setCommand(new LightOnCommand()); + remote.pressButton(); +} \ No newline at end of file diff --git a/c++/decorator.cc b/c++/decorator.cc new file mode 100644 index 0000000..cf2c04c --- /dev/null +++ b/c++/decorator.cc @@ -0,0 +1,37 @@ +/** + * @name 装饰器模式 + * @brief: 动态添加职责 + * @note 场景:流处理(加密/压缩装饰)、GUI 组件增强 + */ + +class Coffee { +public: + virtual double cost() = 0; +}; +class SimpleCoffee : public Coffee { +public: + double cost() override { return 6.9; } +}; + +class CoffeeDecorator : public Coffee { +protected: + Coffee* coffee; +public: + CoffeeDecorator(Coffee* c) : coffee(c) {} +}; + +class MilkDecorator : public CoffeeDecorator { +public: + MilkDecorator(Coffee* c) : CoffeeDecorator(c) {} + double cost() override { + return coffee->cost() + 0.5; // 增加牛奶费用 + } +}; + +// 使用 +int main() +{ + Coffee* coffee = new SimpleCoffee(); + coffee = new MilkDecorator(coffee); // 动态装饰 + coffee->cost(); // 基础 + 牛奶 +} \ No newline at end of file diff --git a/c++/factory_method.cc b/c++/factory_method.cc new file mode 100644 index 0000000..ced0ac4 --- /dev/null +++ b/c++/factory_method.cc @@ -0,0 +1,34 @@ +/** + * @name 工厂方法模式 + * @brief: 将对象创建委托给子类 + * @note 场景:框架需要扩展多种类型对象(如 UI 控件、游戏角色) + */ + +class Product { +public: + virtual void use() = 0; +}; + +class ConcreteProductA : public Product { + void use() override { /* 产品A的功能 */ } +}; + +class Creator { +public: + virtual Product* createProduct() = 0; +}; + +class ConcreteCreatorA : public Creator { + Product* createProduct() override { + return new ConcreteProductA(); + } +}; + +// 使用 +int main() +{ + Creator* creator = new ConcreteCreatorA(); + Product* product = creator->createProduct(); + product->use(); + return 0; +} \ No newline at end of file diff --git a/c++/flyweght.cc b/c++/flyweght.cc new file mode 100644 index 0000000..0ed1161 --- /dev/null +++ b/c++/flyweght.cc @@ -0,0 +1,38 @@ +/** + * @name 享元模式 + * @brief: 高效共享大量细粒度对象 + * @note 场景:文本编辑器字符格式、游戏粒子系统 + */ + +class CharacterStyle { +public: + CharacterStyle(const std::string& font, int size, bool bold) + : font(font), size(size), bold(bold) {} + // 风格属性... +private: + std::string font; + int size; + bool bold; +}; + +class StyleFactory { +public: + CharacterStyle* getStyle(const std::string& font, int size, bool bold) { + std::string key = font + std::to_string(size) + (bold ? "b" : ""); + if (styles.find(key) == styles.end()) { + styles[key] = new CharacterStyle(font, size, bold); + } + return styles[key]; + } +private: + std::unordered_map styles; +}; + +class TextCharacter { +public: + TextCharacter(char c, CharacterStyle* style) + : character(c), style(style) {} +private: + char character; + CharacterStyle* style; // 共享的样式对象 +}; \ No newline at end of file diff --git a/c++/interpreter.cc b/c++/interpreter.cc new file mode 100644 index 0000000..9bd6231 --- /dev/null +++ b/c++/interpreter.cc @@ -0,0 +1,41 @@ +/** + * @name 解释器模式 + * @brief: 定义语言的语法表示 + * @note 场景:SQL解析、正则表达式、数学公式计算 + */ + +class Expression { +public: + virtual int interpret() = 0; +}; + +class Number : public Expression { +public: + Number(int value) : value(value) {} + int interpret() override { return value; } +private: + int value; +}; + +class Add : public Expression { +public: + Add(Expression* left, Expression* right) + : left(left), right(right) {} + int interpret() override { + return left->interpret() + right->interpret(); + } +private: + Expression* left; + Expression* right; +}; + +// 使用:构建表达式树 (2 + (3 + 4)) +Expression* expr = new Add( + new Number(2), + new Add(new Number(3), new Number(4)) +); + +int main() +{ + int result = expr->interpret(); // 输出9 +} \ No newline at end of file diff --git a/c++/observer.cc b/c++/observer.cc new file mode 100644 index 0000000..6740ef9 --- /dev/null +++ b/c++/observer.cc @@ -0,0 +1,35 @@ +/** + * @name 观察者模式 + * @brief: 定义对象间一对多的依赖关系 + * @note 场景:事件处理系统、消息订阅、MVC 模型更新视图 + */ + +#include +#include + +class Observer { +public: + virtual void update(const std::string& msg) = 0; +}; + +class Subject { + std::vector observers; +public: + void attach(Observer* o) { observers.push_back(o); } + void notify(const std::string& msg) { + for (auto o : observers) o->update(msg); + } +}; + +class ConcreteObserver : public Observer { + void update(const std::string& msg) override { /* 处理消息 */ } +}; + +// 使用 +int main() +{ + Subject subject; + subject.attach(new ConcreteObserver()); + subject.notify("Event occurred!"); + return 0; +} \ No newline at end of file diff --git a/c++/proxy.cc b/c++/proxy.cc new file mode 100644 index 0000000..470b1d7 --- /dev/null +++ b/c++/proxy.cc @@ -0,0 +1,41 @@ +/** + * @name 代理模式 + * @brief: 为其他对象提供代理以控制访问 + * @note 场景:延迟加载、访问控制、远程代理 + */ + +class Image { +public: + virtual void display() = 0; +}; + +class RealImage : public Image { +public: + RealImage(const std::string& file) : filename(file) { load(); } + void display() override { /* 显示图片 */ } +private: + void load() { /* 耗时加载操作 */ } + std::string filename; +}; + +class ProxyImage : public Image { +public: + ProxyImage(const std::string& file) : filename(file) {} + void display() override { + if (!realImage) { + realImage = new RealImage(filename); + } + realImage->display(); + } +private: + RealImage* realImage = nullptr; + std::string filename; +}; + +// 使用 +int main() +{ + Image* image = new ProxyImage("large_photo.jpg"); + // 此时未加载真实图片 + image->display(); // 首次访问时加载 +} \ No newline at end of file diff --git a/c++/singleton.cc b/c++/singleton.cc new file mode 100644 index 0000000..a1435b5 --- /dev/null +++ b/c++/singleton.cc @@ -0,0 +1,25 @@ +/** + * @name 单例模式 + * @brief: 确保类只有一个实例,并提供全局访问点 + * @note 场景:日志记录器、配置管理、线程池、数据库连接池 + */ + +class Singleton { +public: + static Singleton& getInstance() { + static Singleton instance; // 线程安全 (C++11) + return instance; + } + void doSomething() { /* ... */ } +private: + Singleton() = default; // 私有构造 + Singleton(const Singleton&) = delete; // 禁用拷贝 + Singleton& operator=(const Singleton&) = delete; +}; + +// 使用 +int main() +{ + Singleton::getInstance().doSomething(); + return 0; +} \ No newline at end of file diff --git a/c++/state.cc b/c++/state.cc new file mode 100644 index 0000000..d231a11 --- /dev/null +++ b/c++/state.cc @@ -0,0 +1,40 @@ +/** + * @name 状态模式 + * @brief: 允许对象在内部状态改变时改变行为 + * @note 场景:订单状态机、游戏角色状态、电梯控制 + */ + +class OrderState { +public: + virtual void shipOrder(Order* order) = 0; + virtual void cancelOrder(Order* order) = 0; +}; + +class NewState : public OrderState { + void shipOrder(Order* order) override {}; + void cancelOrder(Order* order) override {}; +}; + +class ShippedState : public OrderState { + void shipOrder(Order* order) override { /* 已发货,不可重复操作 */ } + void cancelOrder(Order* order) override { /* 已发货不可取消 */ } +}; + +class Order { +public: + Order() : state(new NewState()) {} + void ship() { state->shipOrder(this); } + void cancel() { state->cancelOrder(this); } + void changeState(OrderState* newState) { + delete state; + state = newState; + } +private: + OrderState* state; +}; + +// 状态转换实现 +void NewState::shipOrder(Order* order) { + // 发货逻辑... + order->changeState(new ShippedState()); +} \ No newline at end of file diff --git a/c++/strategy.cc b/c++/strategy.cc new file mode 100644 index 0000000..30b4b53 --- /dev/null +++ b/c++/strategy.cc @@ -0,0 +1,30 @@ +/** + * @name 策略模式 + * @brief: 封装可互换的算法 + * @note 场景:支付方式选择(信用卡/支付宝)、排序算法切换 + */ + +class PaymentStrategy { +public: + virtual void pay(int amount) = 0; +}; + +class CreditCardStrategy : public PaymentStrategy { + void pay(int amount) override { /* 信用卡支付逻辑 */ } +}; + +class PaymentContext { + PaymentStrategy* strategy; +public: + void setStrategy(PaymentStrategy* s) { strategy = s; } + void executePayment(int amount) { strategy->pay(amount); } +}; + +// 使用 +int main() +{ + PaymentContext context; + context.setStrategy(new CreditCardStrategy()); + context.executePayment(100); + return 0; +} \ No newline at end of file diff --git a/c++/template_method.cc b/c++/template_method.cc new file mode 100644 index 0000000..ae1940f --- /dev/null +++ b/c++/template_method.cc @@ -0,0 +1,33 @@ +/** + * @name 模板方法模式 + * @brief: 定义算法框架,允许子类重写特定步骤 + * @note 场景:数据解析流程、数据库操作模板、游戏主循环 + */ + +class DataExporter { +public: + // 模板方法 (final 阻止子类覆盖整个算法) + void exportData() final { + openDataSource(); + convertData(); // 子类可重写的步骤 + closeDataSource(); + } +protected: + virtual void convertData() = 0; // 纯虚函数 + void openDataSource() { /* 通用实现 */ } + void closeDataSource() { /* 通用实现 */ } +}; + +class CSVExporter : public DataExporter { +protected: + void convertData() override { + // CSV格式转换逻辑 + } +}; + +// 使用 +int main() +{ + DataExporter* exporter = new CSVExporter(); + exporter->exportData(); // 执行完整流程 +} \ No newline at end of file