:复合模式:duck-LMLPHP

#ifndef __QUAKEABLE_H__
#define __QUAKEABLE_H__
#include <iostream>
#include <vector>
using namespace std;
class QuackObservable;
class Observer
{
public:
Observer(){}
virtual~Observer(){}
virtual void update(QuackObservable *d)
{
cout << "Observer update " << endl;
}
}; class QuackObservable
{
public:
QuackObservable(){}
virtual ~QuackObservable(){}
virtual void RegisterObserver(Observer *q1)
{ }
virtual void NotifyObserver()
{
cout << "Notify Observer update " << endl;
}
}; class QuakeLogist:public Observer
{
public:
QuakeLogist(){}
virtual ~QuakeLogist(){}
virtual void update(QuackObservable *d)
{
cout << "QuakeLogist update " << endl;
}
}; class ObserveAble : public QuackObservable
{
private:
QuackObservable *q;
vector<Observer*>olist;
public:
ObserveAble(QuackObservable *qq){ q = qq; }
virtual ~ObserveAble(){}
virtual void RegisterObserver(Observer *q1)
{
olist.push_back(q1);
}
virtual void NotifyObserver()
{
vector<Observer*>::iterator it = olist.begin();
while (it!= olist.end())
{
(*it++)->update(q);
}
}
}; class Quakeable :public QuackObservable
{
public:
Quakeable(){}
virtual~Quakeable(){}
virtual void Quake(){}
}; class MallardDuck :public Quakeable
{
private:
ObserveAble *obseAble;
public:
MallardDuck()
{
obseAble = new ObserveAble(this);
}
virtual ~MallardDuck(){}
virtual void Quake()
{
cout << "MallardDuck Quake" << endl;
NotifyObserver();
}
virtual void RegisterObserver(Observer *os)
{
obseAble->RegisterObserver(os);
}
virtual void NotifyObserver()
{
obseAble->NotifyObserver();
}
}; class BlueDuck :public Quakeable
{
public:
BlueDuck(){}
virtual~BlueDuck(){}
virtual void Quake()
{
cout << "Blue Duck Quake" << endl;
}
}; class RedDuck :public Quakeable
{
public:
RedDuck(){}
virtual~RedDuck(){}
virtual void Quake()
{
cout << "Red Duck Quake" << endl;
}
}; class DuckCall :public Quakeable
{
public:
DuckCall(){}
virtual~DuckCall(){}
virtual void Quake()
{
cout << "DuckCall Quake" << endl;
}
}; class RubberDuck :public Quakeable
{
public:
RubberDuck(){}
virtual~RubberDuck(){}
virtual void Quake()
{
cout << "Rubber Duck Quake" << endl;
}
}; class QuackCounter :public Quakeable
{
private:
Quakeable *duck;
static int number;
public:
QuackCounter(Quakeable *q)
{
duck = q;
}
virtual~QuackCounter(){}
virtual void Quake()
{
duck->Quake();
number++;
}
static int getNum()
{
return number;
}
}; int QuackCounter::number = 0;
#endif
#ifndef __GOOSE_H__
#define __GOOSE_H__
#include "Quakable.h" class Goose
{
public:
Goose(){}
virtual~Goose(){}
virtual void honk()
{
cout << "Goose honk" << endl;
}
}; class GooseAdapter :public Quakeable
{
private:
Goose *goose;
public:
GooseAdapter(Goose *g)
{
goose = g;
} virtual~GooseAdapter(){}
virtual void Quake()
{
goose->honk();
}
}; class AbDuckFactory
{
public:
AbDuckFactory(){}
virtual~AbDuckFactory(){}
virtual Quakeable * CreateBlueDuck() = 0;
virtual Quakeable * CreateRedDuck() = 0;
virtual Quakeable * CreateCallDuck() = 0;
virtual Quakeable * CreateRubberDuck() = 0;
}; class DuckFactory : public AbDuckFactory
{
public:
DuckFactory(){}
virtual~DuckFactory(){}
virtual Quakeable * CreateBlueDuck() { return new BlueDuck(); }
virtual Quakeable * CreateRedDuck(){ return new RedDuck(); }
virtual Quakeable * CreateCallDuck() { return new DuckCall(); }
virtual Quakeable * CreateRubberDuck() { return new RubberDuck(); }
}; class CountFactory : public AbDuckFactory
{
public:
CountFactory(){}
virtual~CountFactory(){}
virtual Quakeable * CreateBlueDuck() { return new QuackCounter(new BlueDuck()); }
virtual Quakeable * CreateRedDuck(){ return new QuackCounter(new RedDuck()); }
virtual Quakeable * CreateCallDuck() { return new QuackCounter(new DuckCall()); }
virtual Quakeable * CreateRubberDuck() { return new QuackCounter(new RubberDuck()); }
}; class DuckFlock :public Quakeable
{
private:
ObserveAble *obs;
vector<Quakeable *> ducklist;
public:
DuckFlock()
{
obs = new ObserveAble(this);
}
virtual~DuckFlock(){}
void add(Quakeable *t)
{
ducklist.push_back(t);
}
virtual void Quake()
{
vector<Quakeable*>::iterator it = ducklist.begin();
while (it != ducklist.end())
{
(*it++)->Quake();
NotifyObserver();
} } virtual void RegisterObserver(Observer *ob)
{
obs->RegisterObserver(ob);
} virtual void NotifyObserver()
{
obs->NotifyObserver();
}
}; #endif
#include <iostream>
#include "Quakable.h"
#include "Goose.h"
using namespace std;
void simulate()
{
AbDuckFactory *f = new CountFactory(); Quakeable *r = f->CreateBlueDuck();
Quakeable *b =f->CreateCallDuck();
Quakeable *d = f->CreateRedDuck();
Quakeable *rub = f->CreateRubberDuck();
Quakeable *g = new QuackCounter(new GooseAdapter(new Goose())); r->Quake();
b->Quake();
d->Quake();
rub->Quake();
g->Quake();
cout << "Fuck " << QuackCounter::getNum() << endl; Quakeable *r1 = f->CreateBlueDuck();
Quakeable *r2 = f->CreateBlueDuck();
Quakeable *r3 = f->CreateBlueDuck();
Quakeable *r4 = f->CreateBlueDuck(); DuckFlock *df = new DuckFlock();
df->add(r1);
df->add(r2);
df->add(r3);
df->add(r4); cout << "Fuck " << QuackCounter::getNum() << endl;
QuakeLogist *ql = new QuakeLogist();
// Quakeable *qa = new MallardDuck();
// qa->RegisterObserver(ql);
// qa->Quake(); df->RegisterObserver(ql);
df->Quake(); } int main()
{ simulate(); return 0;
}
05-11 14:11