c++ verson

This commit is contained in:
icodekang 2025-07-10 22:26:33 +08:00
parent 92db5d94c8
commit 6b734fd028
16 changed files with 562 additions and 0 deletions

3
.vscode/settings.json vendored Normal file
View File

@ -0,0 +1,3 @@
{
"C_Cpp.errorSquiggles": "disabled"
}

38
c++/abstract_factory.cc Normal file
View File

@ -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;
}

32
c++/adapter.cc Normal file
View File

@ -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();
}

54
c++/builder.cc Normal file
View File

@ -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<std::string> 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);
}

View File

@ -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处理
}

36
c++/command.cc Normal file
View File

@ -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();
}

37
c++/decorator.cc Normal file
View File

@ -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(); // 基础 + 牛奶
}

34
c++/factory_method.cc Normal file
View File

@ -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;
}

38
c++/flyweght.cc Normal file
View File

@ -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<std::string, CharacterStyle*> styles;
};
class TextCharacter {
public:
TextCharacter(char c, CharacterStyle* style)
: character(c), style(style) {}
private:
char character;
CharacterStyle* style; // 共享的样式对象
};

41
c++/interpreter.cc Normal file
View File

@ -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
}

35
c++/observer.cc Normal file
View File

@ -0,0 +1,35 @@
/**
* @name
* @brief:
* @note MVC
*/
#include <string>
#include <vector>
class Observer {
public:
virtual void update(const std::string& msg) = 0;
};
class Subject {
std::vector<Observer*> 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;
}

41
c++/proxy.cc Normal file
View File

@ -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(); // 首次访问时加载
}

25
c++/singleton.cc Normal file
View File

@ -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;
}

40
c++/state.cc Normal file
View File

@ -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());
}

30
c++/strategy.cc Normal file
View File

@ -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;
}

33
c++/template_method.cc Normal file
View File

@ -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(); // 执行完整流程
}