有用な Observerパターンを考えてみましょう。
まずは Observerクラスから。これに関しては任意の型のイベントを受け取るといった想定
で、下記のようなシンプルなテンプレートクラスにします。
#ifndef OBSERVER_H_
#define OBSERVER_H_
#include "typelist.h"
template <typename T>
class Observer
{
public:
virtual void update(const T& event) = 0;
virtual ~Observer() {}
};
#endif // OBSERVER_H_
このObserverクラスに対する通知を担う Subjectクラスですが、ここでTypelistを使用
してみます。
#ifndef SUBJECT_H_
#define SUBJECT_H_
#include <vector>
#include "typelist.h"
#include "observer.h"
template <typename TL>
class Subject;
template <typename T, typename U>
class Subject< Typelist<T, U> > : public Subject<U>
{
typedef std::vector<Observer<T>*> Vector;
public:
Subject() : observers_()
{
}
~Subject()
{
}
using Subject<U>::notify;
void notify(const T& event)
{
typename Vector::iterator it = observers_.begin(), end_it = observers_.end();
for (; it != end_it; ++it) {
(*it)->update(event);
}
}
using Subject<U>::attach;
void attach(Observer<T>* observer_ptr)
{
observers_.push_back(observer_ptr);
}
using Subject<U>::detach;
void detach(Observer<T>* observer_ptr)
{
typename Vector::iterator it = observers_.begin(), end_it = observers_.end();
for (; it != end_it; ++it) {
if ((*it) == observer_ptr) {
break;
}
}
if (it != end_it) {
observers_.erase(it);
}
}
private:
Vector observers_;
};
template <>
class Subject<NullType>
{
public:
void notify(const NullType& /* no use */)
{
}
void attach(const NullType& /* no use */)
{
}
void detach(const NullType& /* no use */)
{
}
};
#endif // SUBJECT_H_
このSubjectクラスはテンプレートパラメータとしてTypelistを受け取り、Typelist要素の型
に対して attach, detach, notify操作を支援します。クライアントコードにおいては、
Subjectクラスオブジェクトをメンバーとして保有して、detach, attachを委譲するような
メソッドを定義してやれば良い感じになります。
EventTypeListという型でイベントの型集合のTypelistがあるとすると、attach, detachの
委譲メソッドは下記のような感じになります。
#ifndef CONCRETE_SUBJECT_H
#define CONCRETE_SUBJECT_H
#include "subject.h"
#include "event_type_list.h"
class ConcreteSubject
{
public:
ConcreteSubject();
template <typename T>
void attach(Observer<T>* observer)
{
typedef typename TypelistOf<EventTypeList, T>::Result TL;
subject_.Subject<TL>::attach(observer);
}
template <typename T>
void detach(Observer<T>* observer)
{
typedef typename TypelistOf<EventTypeList, T>::Result TL;
subject_.Subject<TL>::detach(observer);
}
void action1();
void action2();
private:
Subject<EventTypeList> subject_;
};
ここで TypelistOfは以前に紹介した Typelistの部分集合を取り出す操作になります。Again, I would like to take a look at Typelist. Making use of what has been adopted
until now, I would like to come up with Observer pattern which is quite useful among
various Design Patterns.
First, let's take a look at Observer class. Since we would like to handle various event
types, we are supposed to make the Observer class as parameterized class
(template class) as follows.
#ifndef OBSERVER_H_
#define OBSERVER_H_
#include "typelist.h"
template <typename T>
class Observer
{
public:
virtual void update(const T& event) = 0;
virtual ~Observer() {}
};
#endif // OBSERVER_H_
Secondly, we are going to build up Subject class by making use of Typelist as
follows.
#ifndef SUBJECT_H_
#define SUBJECT_H_
#include <vector>
#include "typelist.h"
#include "observer.h"
template <typename TL>
class Subject;
template <typename T, typename U>
class Subject< Typelist<T, U> > : public Subject<U>
{
typedef std::vector<Observer<T>*> Vector;
public:
Subject() : observers_()
{
}
~Subject()
{
}
using Subject<U>::notify;
void notify(const T& event)
{
typename Vector::iterator it = observers_.begin(), end_it = observers_.end();
for (; it != end_it; ++it) {
(*it)->update(event);
}
}
using Subject<U>::attach;
void attach(Observer<T>* observer_ptr)
{
observers_.push_back(observer_ptr);
}
using Subject<U>::detach;
void detach(Observer<T>* observer_ptr)
{
typename Vector::iterator it = observers_.begin(), end_it = observers_.end();
for (; it != end_it; ++it) {
if ((*it) == observer_ptr) {
break;
}
}
if (it != end_it) {
observers_.erase(it);
}
}
private:
Vector observers_;
};
template <>
class Subject<NullType>
{
public:
void notify(const NullType& /* no use */)
{
}
void attach(const NullType& /* no use */)
{
}
void detach(const NullType& /* no use */)
{
}
};
#endif // SUBJECT_H_
This parameterized Subject class would intake Typelist as template parameter
and then support attach, detach and notify operations towards respective
Typelist element types. In client code, we are supposed to composite the Subject
class object and then write some appropriate attach and detach method as
delegation.
Assuming that EventTypeList is the Typelist for various event types we are going to
support here, delegation methods for attach and detach will be like the following.
#ifndef CONCRETE_SUBJECT_H
#define CONCRETE_SUBJECT_H
#include "subject.h"
#include "event_type_list.h"
class ConcreteSubject
{
public:
ConcreteSubject();
template <typename T>
void attach(Observer<T>* observer)
{
typedef typename TypelistOf<EventTypeList, T>::Result TL;
subject_.Subject<TL>::attach(observer);
}
template <typename T>
void detach(Observer<T>* observer)
{
typedef typename TypelistOf<EventTypeList, T>::Result TL;
subject_.Subject<TL>::detach(observer);
}
void action1();
void action2();
private:
Subject<EventTypeList> subject_;
};
Here, TypelistOf operation is what was introduced the other day.