当前位置:   article > 正文

c++架构师必懂的五种设计模式_c++架构型模式

c++架构型模式

C++中常见的五种设计模式包括:单例模式、工厂模式、观察者模式、策略模式和装饰器模式。以下是给出每个设计模式对应的main函数和完整代码示例:

  1. 单例模式:
#include<iostream>

class Singleton {
private:
    static Singleton* instance;
    // 私有构造函数,防止外部实例化
    Singleton() {}

public:
    // 获取实例的唯一入口
    static Singleton* getInstance() {
        if (instance == nullptr) {
            instance = new Singleton();
        }
        return instance;
    }

    void showMessage() {
        std::cout << "Hello, I am a Singleton." << std::endl;
    }
};

// 初始化实例指针为nullptr
Singleton* Singleton::instance = nullptr;

int main() {
    Singleton* singleton = Singleton::getInstance();
    singleton->showMessage();

    return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  1. 工厂模式:
#include<iostream>

// 抽象产品类
class Product {
public:
    virtual void showMessage() = 0;
};

// 具体产品类A
class ConcreteProductA : public Product {
public:
    void showMessage() override {
        std::cout << "Hello, I am Product A." << std::endl;
    }
};

// 具体产品类B
class ConcreteProductB : public Product {
public:
    void showMessage() override {
        std::cout << "Hello, I am Product B." << std::endl;
    }
};

// 工厂类
class Factory {
public:
    Product* createProduct(int type) {
        if (type == 1) {
            return new ConcreteProductA();
        } else if (type == 2) {
            return new ConcreteProductB();
        } else {
            return nullptr;
        }
    }
};

int main() {
    Factory factory;

    Product* productA = factory.createProduct(1);
    productA->showMessage();

    Product* productB = factory.createProduct(2);
    productB->showMessage();

    delete productA;
    delete productB;

    return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  1. 观察者模式:
#include<iostream>
#include<vector>

// 抽象观察者类
class Observer {
public:
    virtual void update(int data) = 0;
};

// 具体观察者类A
class ConcreteObserverA : public Observer {
public:
    void update(int data) override {
        std::cout << "Observer A: " << data << std::endl;
    }
};

// 具体观察者类B
class ConcreteObserverB : public Observer {
public:
    void update(int data) override {
        std::cout << "Observer B: " << data << std::endl;
    }
};

// 主题类(被观察者)
class Subject {
private:
    int data;
    std::vector<Observer*> observers;

public:
    void attach(Observer* observer) {
        observers.push_back(observer);
    }

    void setData(int value) {
        data = value;
        notifyObservers();
    }

    void notifyObservers() {
        for (auto observer : observers) {
            observer->update(data);
        }
    }
};

int main() {
    Subject subject;
    ConcreteObserverA observerA;
    ConcreteObserverB observerB;

    subject.attach(&observerA);
    subject.attach(&observerB);

    subject.setData(10);

    return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  1. 策略模式:
#include<iostream>

// 抽象策略类
class Strategy {
public:
    virtual void execute() = 0;
};

// 具体策略类A
class ConcreteStrategyA : public Strategy {
public:
    void execute() override {
        std::cout << "Executing Strategy A." << std::endl;
    }
};

// 具体策略类B
class ConcreteStrategyB : public Strategy {
public:
    void execute() override {
        std::cout << "Executing Strategy B." << std::endl;
    }
};

// 上下文类
class Context {
private:
    Strategy* strategy;

public:
    void setStrategy(Strategy* strategy) {
        this->strategy = strategy;
    }

    void executeStrategy() {
        strategy->execute();
    }
};

int main() {
    Context context;
    ConcreteStrategyA strategyA;
    ConcreteStrategyB strategyB;

    context.setStrategy(&strategyA);
    context.executeStrategy();

    context.setStrategy(&strategyB);
    context.executeStrategy();

    return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  1. 装饰器模式:
#include<iostream>

// 抽象组件类
class Component {
public:
    virtual void showMessage() = 0;
};

// 具体组件类
class ConcreteComponent : public Component {
public:
    void showMessage() override {
        std::cout << "Hello, I am a Concrete Component." << std::endl;
    }
};

// 抽象装饰器类
class Decorator : public Component {
protected:
    Component* component;

public:
    Decorator(Component* component) : component(component) {}

    void showMessage() override {
        component->showMessage();
    }
};

// 具体装饰器类A
class ConcreteDecoratorA : public Decorator {
public:
    ConcreteDecoratorA(Component* component) : Decorator(component) {}

    void showMessage() override {
        Decorator::showMessage();
        std::cout << "Additional functionality A." << std::endl;
    }
};

// 具体装饰器类B
class ConcreteDecoratorB : public Decorator {
public:
    ConcreteDecoratorB(Component* component) : Decorator(component) {}

    void showMessage() override {
        Decorator::showMessage();
        std::cout << "Additional functionality B." << std::endl;
    }
};

int main() {
    ConcreteComponent component;
    ConcreteDecoratorA decoratorA(&component);
    ConcreteDecoratorB decoratorB(&decoratorA);

    decoratorB.showMessage();

    return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60

以上是C++中常见的五种设计模式,并分别给出了每个设计模式对应的main函数和完整代码示例。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/天景科技苑/article/detail/823071
推荐阅读
相关标签
  

闽ICP备14008679号