I have a order beverage type program. I am tryig to implement observer pattern so when the order is placed the cellphone /observer will display the order, basically letting them know of an update..
I just dont know how to do this. If i pass TheOrder class in update()order classes..i get a slew of errors. too many to post.. update() is in observer and cellphone and in the TheOrder class..
here is my observer classes:
#ifndef _OBSERVER_
#define _OBSERVER_
#include <string>
//#include "TheOrder.h"
namespace CoffeeHouse {
namespace Observers {
//class Subject;
class Observer {
protected: virtual ~Observer() = 0 {
};
public:
// virtual
virtual void update()= 0;
};
} // namespace Observer
} // namespace HeadFirstDesignPatterns
#endif
here is the observer concrete class
#ifndef _CELLPHONE1_
#define _CELLPHONE1_
#include <iostream>
using namespace std;
namespace CoffeeHouse {
namespace Observers {
class CellPhone1: public Observer {
public:
std::string _number;
CellPhone1(std::string number){
_number = number;
}
void update()
{
std::cout << "BUZZZZZZZ - CellPhone #" << _number << " your order is ready " << endl;
}
};
} // namespace Observer
} //
#endif
here is the subject class
#ifndef _SUBJECT_
#define _SUBJECT_
#include "Starbuzz.h"
//#include "Starbuzz2.h"
#include "Observer.h"
#include <list>
#include "Beverage.h"
namespace CoffeeHouse {
namespace Observers {
class Subject {
protected: virtual ~Subject() = 0 {
};
public: virtual void registerObserver( Observer* o ) = 0;
public: virtual void removeObserver( Observer* o ) = 0;
public: virtual void notifyObservers() = 0;
};
} // namespace Observer
}
#endif
here is the subjects concrete class
#ifndef _THE_ORDER_
#define _THE_ORDER_
#include "Beverage.h"
#include <list>
#include <iostream>
#include "Order.h"
#pragma once;
//class Order;
using namespace CoffeeHouse::Decorator;
namespace CoffeeHouse {
namespace Observers {
class TheOrder : public Subject {
private: mutable std::list< Observer* > _observers;
private: mutable std::list< Order* > _orders;
//public: virtual ~Order() = 0
//public: ~TheOrder();
public: void NewOrder(Beverage* bev, Observer* cellphone)
{
// _orders.push_front(new Order(bev, cellphone));
//_//observers.push_front(new Order(bev));
}
public: void registerObserver( Observer* o ) { assert( o );
_observers.push_back(o);
}
public: void removeObserver( Observer* o ) { assert( o );
_observers.remove(o);
}
public: void notifyObservers() {
for( std::list< Observer* >::iterator iterator = _observers.begin(); _observers.end() != iterator; ++iterator ) {
Observer* observer = *iterator;
observer->update();
}
}
};
//}
} // namespace Observer
} // namespace CoffeeHouse
#endif
here is the observer concrete class
#ifndef _CELLPHONE1_
#define _CELLPHONE1_
#include <iostream>
using namespace std;
namespace CoffeeHouse {
namespace Observers {
class CellPhone1: public Observer {
public:
std::string _number;
CellPhone1(std::string number){
_number = number;
}
void update()
{
std::cout << "BUZZZZZZZ - CellPhone #" << _number << " your order is ready " << endl;
}
};
} // namespace Observer
} //
#endif
main()
#include "Starbuzz.h" //just header files
#include "Starbuzz2.h" // just header files
#include "Subject.h"
#include "TheOrder.h"
#include "CellPhone2.h"
#include "CellPhone1.h"
using namespace CoffeeHouse::Decorator;
using namespace CoffeeHouse::Observers;
int main( int argc, char* argv[] )
{
Beverage* beverage2 = new DarkRoast();
beverage2 = new Mocha(beverage2);
beverage2 = new Mocha(beverage2);
beverage2 = new Whip(beverage2);
std::cout << "Current Orders: " << endl;
std::cout << beverage2->getDescription()
<< " $"
<< beverage2->cost()
<< std::endl;
Beverage* beverage3 = new HouseBlend();
beverage3 = new Soy(beverage3);
beverage3 = new Mocha(beverage3);
beverage3 = new Whip(beverage3);
std::cout << beverage3->getDescription()
<< " $"
<< beverage3->cost()
<< std::endl;
delete beverage3;
delete beverage2;
//delete beverage;
Bagel* bagel = new Plain();
std::cout.setf( std::ios::showpoint);
std::cout.precision(3);
std::cout << bagel->getDescription()
<< " $"
<< bagel->cost()
<< std::endl;
Bagel* bagel2 = new Raisen();
bagel2 = new Myhummus(bagel2);
bagel2 = new SesemeSeed(bagel2);
bagel2 = new CreameCheese(bagel2);
std::cout << bagel2->getDescription()
<< " $"
<< bagel2->cost()
<< std::endl;
Bagel* bagel3 = new Onion();
bagel3 = new Myhummus(bagel3);
bagel3 = new SesemeSeed(bagel3);
bagel3 = new CreameCheese(bagel3);
std::cout << bagel3->getDescription()
<< " $"
<< bagel3->cost()
<&开发者_C百科lt; std::endl;
TheOrder* orders = new TheOrder();
CellPhone1* cellphone1 = new CellPhone1("1");
orders->registerObserver(cellphone1);
orders->notifyObservers();
TheOrder* order = new TheOrder();
CellPhone1* obj2 = new CellPhone1("3");
order->registerObserver(obj2);
order->notifyObservers();
return 0;
}
i just would like to send the order into cellphone1 class so i can display each different order.. i think i need to send it in through the update function..
because in cellphone1 is the observer so i wold like to display the order there..
In the TheOrder class there is a notifyObservers() function do i need to pass this pointer? also
There are two common ways to implement the Observer pattern. In the first, each Observer holds on to a reference to the Observable. In your case, you would need to tell each Cellphone object which Order objects it's waiting on. If you only had at most one order per cellphone, this wouldn't be too tricky. If you could have more than order per cellphone, you would have to manage that somehow in the cellphone class.
The second common way (and what I would do in your case), is to pass the Observable to the Observer in the update()
method. In your case, you would change the signature of update
in Observer
and all of its subclasses to:
void update(TheOrder *order);
Then you would have access to the specific completed order in the cellphone's update
method.
One thing to fix:
protected: virtual ~Observer() = 0 { };
精彩评论