void NotificationCenterTest::test4()
{
	NotificationCenter nc;
	Observer<NotificationCenterTest, Notification> o1(*this, &NotificationCenterTest::handle1);
	Observer<NotificationCenterTest, Notification> o2(*this, &NotificationCenterTest::handle2);
	nc.addObserver(o1);
	assert (nc.hasObserver(o1));
	nc.addObserver(o2);
	assert (nc.hasObserver(o2));
	nc.postNotification(new Notification);
	assert (_set.size() == 2);
	assert (_set.find("handle1") != _set.end());
	assert (_set.find("handle2") != _set.end());
	nc.removeObserver(Observer<NotificationCenterTest, Notification>(*this, &NotificationCenterTest::handle1));
	assert (!nc.hasObserver(o1));
	nc.removeObserver(Observer<NotificationCenterTest, Notification>(*this, &NotificationCenterTest::handle2));
	assert (!nc.hasObserver(o2));
	_set.clear();
	nc.postNotification(new Notification);
	assert (_set.empty());
	Observer<NotificationCenterTest, Notification> o3(*this, &NotificationCenterTest::handle3);
	nc.addObserver(o3);
	assert (nc.hasObserver(o3));
	nc.postNotification(new Notification);
	assert (_set.size() == 1);
	assert (_set.find("handle3") != _set.end());
	nc.removeObserver(Observer<NotificationCenterTest, Notification>(*this, &NotificationCenterTest::handle3));
	assert (!nc.hasObserver(o3));
}
Beispiel #2
0
int main_1(int argc, char** argv) {

    NotificationCenter nc;
    Target target;

    // Observer works with plain pointers to Notification objects.
    nc.addObserver(
        Observer<Target, BaseNotification>(target, &Target::handleBase));
    // NObserver works with AutoPtr<Notification>.
    // handleBase and handleSub both got this
    nc.addObserver(
        NObserver<Target, SubNotification>(target, &Target::handleSub));

    std::cout << "* postNotification: BaseNotification" << std::endl;
    nc.postNotification(new BaseNotification("Base Class Notification"));

    // Targets subscribed for a particular notification class also receive
    // notifications that are subclasses of that class.
    std::cout << "* postNotification: SubNotification" << std::endl;
    nc.postNotification(new SubNotification("Sub Class Notification"));

    nc.removeObserver(
        Observer<Target, BaseNotification>(target, &Target::handleBase));
    nc.removeObserver(
        NObserver<Target, SubNotification>(target, &Target::handleSub));

    return 0;
}
// 중재자(Mediator) 패턴
// - 복잡한 객체간의 관계를 단순화 시켜주는 클래스
int main()
{
  NotificationCenter& globalCenter = NotificationCenter::defaultCenter();

  NotificationCenter nc;
  nc.addObserver("LOWBATTERY", &foo);
  nc.addObserver("LOWBATTERY", bind(&goo, 5));

  Dialog dialog;
  nc.addObserver("LOWBATTERY", bind(&Dialog::warning, &dialog));


  // 이제 배터리를 체크하는 모듈에서
  nc.postNotificationWithName("LOWBATTERY");
}
void NotificationCenterTest::test5()
{
	NotificationCenter nc;
	nc.addObserver(Observer<NotificationCenterTest, Notification>(*this, &NotificationCenterTest::handle1));
	nc.addObserver(Observer<NotificationCenterTest, TestNotification>(*this, &NotificationCenterTest::handleTest));
	nc.postNotification(new Notification);
	assert (_set.size() == 1);
	assert (_set.find("handle1") != _set.end());
	_set.clear();
	nc.postNotification(new TestNotification);
	assert (_set.size() == 2);
	assert (_set.find("handle1") != _set.end());
	assert (_set.find("handleTest") != _set.end());
	nc.removeObserver(Observer<NotificationCenterTest, Notification>(*this, &NotificationCenterTest::handle1));
	nc.removeObserver(Observer<NotificationCenterTest, TestNotification>(*this, &NotificationCenterTest::handleTest));
}
void NotificationCenterTest::testAuto()
{
	NotificationCenter nc;
	nc.addObserver(NObserver<NotificationCenterTest, Notification>(*this, &NotificationCenterTest::handleAuto));
	nc.postNotification(new Notification);
	assert (_set.size() == 1);
	assert (_set.find("handleAuto") != _set.end());
	nc.removeObserver(NObserver<NotificationCenterTest, Notification>(*this, &NotificationCenterTest::handleAuto));
}
void NotificationCenterTest::test2()
{
	NotificationCenter nc;
	nc.addObserver(Observer<NotificationCenterTest, Notification>(*this, &NotificationCenterTest::handle1));
	assert (nc.hasObservers());
	assert (nc.countObservers() == 1);
	nc.postNotification(new Notification);
	assert (_set.size() == 1);
	assert (_set.find("handle1") != _set.end());
	nc.removeObserver(Observer<NotificationCenterTest, Notification>(*this, &NotificationCenterTest::handle1));
	assert (!nc.hasObservers());
	assert (nc.countObservers() == 0);
}