forked from huihut/interview
-
Notifications
You must be signed in to change notification settings - Fork 0
/
concrete_subject.h
45 lines (41 loc) · 971 Bytes
/
concrete_subject.h
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
//
// Created by xiemenghui on 2018/7/21.
//
#ifndef DESIGNPATTERN_CONCRETE_SUBJECT_H
#define DESIGNPATTERN_CONCRETE_SUBJECT_H
#include "subject.h"
#include "observer.h"
#include <iostream>
#include <list>
// Specific Subject
class ConcreteSubject : public ISubject
{
public:
ConcreteSubject(){ m_fPrice = 10.0; }
void SetPrice(float price)
{
m_fPrice = price;
}
void Attach(IObserver * observer)
{
m_observers.push_back(observer);
}
void Detach(IObserver * observer)
{
m_observers.remove(observer);
}
// Notify all observers
void Notify()
{
std::list<IObserver *>::iterator it = m_observers.begin();
while (it != m_observers.end())
{
(*it)->Update(m_fPrice);
++it;
}
}
private:
std::list<IObserver *> m_observers; // Observer list
float m_fPrice; // Price
};
#endif //DESIGNPATTERN_CONCRETE_SUBJECT_H