Home > Backend Development > C++ > body text

Application cases of design patterns in C++

WBOY
Release: 2024-06-01 20:14:00
Original
870 people have browsed it

Yes, design patterns are widely used in C++. The Observer pattern is a one-to-one relationship in which one object (the principal) manages dependent objects (the observers) and notifies them of state changes. In this example, weather data (the subject) notifies the display (the observer) of state changes, thereby updating the display. Design patterns provide proven solutions for creating more flexible and maintainable code.

Application cases of design patterns in C++

Application cases of design patterns in C++

Design patterns are reusable solutions for solving common problems. software development issues. They provide a set of proven best practices that can help developers create more flexible and maintainable code.

Observer Pattern

The Observer pattern defines a one-to-one relationship in which one object (subject) manages a set of dependent objects (observers). When the subject's state changes, it notifies all observers so they can update accordingly.

Implementation:

#include <iostream>
#include <vector>

class Subject {
public:
    void addObserver(Observer* obs) { observers.push_back(obs); }
    void removeObserver(Observer* obs) { observers.erase(observers.begin() + find(observers.begin(), observers.end(), obs)); }
    void notifyObservers() { for (Observer* obs : observers) obs->update(); }
    int getSubjectState() const { return subjectState; }
    void setSubjectState(int state) { subjectState = state; notifyObservers(); }

private:
    int subjectState;
    std::vector<Observer*> observers;
};

class Observer {
public:
    virtual void update() = 0;
};

class Observer1 : public Observer {
public:
    void update() { std::cout << "Observer 1 notified, subject state: " << subject->getSubjectState() << "\n"; }
    Subject* subject;
};

class Observer2 : public Observer {
public:
    void update() { std::cout << "Observer 2 notified, subject state: " << subject->getSubjectState() << "\n"; }
    Subject* subject;
};

int main() {
    Subject subject;
    Observer1 observer1;
    observer1.subject = &subject;
    Observer2 observer2;
    observer2.subject = &subject;

    subject.addObserver(&observer1);
    subject.addObserver(&observer2);

    subject.setSubjectState(10);
    subject.setSubjectState(20);

    return 0;
}
Copy after login

Output:

Observer 1 notified, subject state: 10
Observer 2 notified, subject state: 10
Observer 1 notified, subject state: 20
Observer 2 notified, subject state: 20
Copy after login

In this example, Subject is the weather Data, Observer are two displays. When the weather data changes (setSubjectState), it notifies the display (notifyObservers) and the display updates its displayed content (update).

Conclusion:

Design patterns provide proven solutions that can help developers create more flexible and maintainable code. The Observer pattern is just one of many design patterns that is widely used in C++ and other languages.

The above is the detailed content of Application cases of design patterns in C++. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!