/
Dikar
/
Observer
Обзор
Документация
Войти
/
Dikar
/
Observer
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
main.cpp
55 строк
2 KB
Andrey
Observer
20 дек 2025, 21:09
Не верифицирован
20 дек 2025, 21:09
e34c22e
Код
Авторство
О чём код?
#include "ConcreteObservable.h" #include "ConcreteObserver.h" #include <memory> #include <iostream> #include <chrono> #include <thread> #include "spdlog/spdlog.h" #include "spdlog/sinks/stdout_color_sinks.h" int main() { // Инициализация spdlog auto console = spdlog::stdout_color_mt("console"); spdlog::set_default_logger(console); spdlog::set_pattern("[%H:%M:%S.%e] [thread %t] %v"); spdlog::info("=== Starting Observer Pattern Test ==="); // Создаем наблюдателей auto Observer1 = std::make_shared<ConcreteObserver>("obs1"); auto Observer2 = std::make_shared<ConcreteObserver>("obs2"); auto Observer3 = std::make_shared<ConcreteObserver>("obs3"); // Создаем observable ConcreteObservable Notifier; // Добавляем наблюдателей Notifier.AddObserver(Observer1); Notifier.AddObserver(Observer2); Notifier.AddObserver(Observer3); spdlog::info("Notifying all observers 3 times:"); // Запускаем уведомления в разных потоках for (int i = 1; i <= 3; ++i) { spdlog::info("Notification {}:", i); Notifier.NotifyAll(); // Небольшая пауза между уведомлениями std::this_thread::sleep_for(std::chrono::milliseconds(100)); } spdlog::info("Notifying individual observers:"); Notifier.Notify("obs1"); Notifier.Notify("obs2"); // Ждем завершения всех асинхронных операций spdlog::info("Waiting for async operations..."); std::this_thread::sleep_for(std::chrono::seconds(1)); spdlog::info("=== Test completed ==="); return 0; }