代码拉取完成,页面将自动刷新
#include <iostream>
#include <memory>
#include <vector>
class Observer {
public:
Observer(std::string name = "")
: m_name(name) {}
virtual ~Observer() = default;
virtual void update(int time) = 0;
protected:
std::string m_name;
};
class Student : public Observer {
public:
Student(std::string name = "")
: Observer(name) {}
void update(int time) override {
std::cout << m_name << " " << time << std::endl;
}
};
class Subject {
public:
virtual ~Subject() = default;
virtual void registerObserver(Observer *observer) = 0;
virtual void removeObserver(Observer *observer) = 0;
virtual void notifyObserver() = 0;
virtual void setTime() = 0;
};
class Clock : public Subject {
public:
void registerObserver(Observer *observer) override {
std::unique_ptr<Observer> ptr(observer);
observers.push_back(std::move(ptr));
}
void removeObserver(Observer *observer) override {
for (auto it = observers.begin(); it != observers.end(); it++) {
if (it->get() == observer) {
observers.erase(it);
break;
}
}
}
void notifyObserver() override {
for (auto &observer : observers) {
observer->update(m_time);
}
}
void setTime() override {
m_time += 1;
if (m_time == 24) {
m_time = 0;
}
notifyObserver();
}
private:
std::vector<std::unique_ptr<Observer>> observers;
int m_time = 0;
};
int main() {
std::unique_ptr<Subject> clock = std::make_unique<Clock>();
int numOfStudent;
std::cin >> numOfStudent;
while (numOfStudent--) {
std::string name;
std::cin >> name;
Observer *stu = new Student(name);
clock->registerObserver(stu);
}
int updateTimes;
std::cin >> updateTimes;
while (updateTimes--) {
clock->setTime();
}
return 0;
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。