需要有关观察者 c++ 更新的帮助
我有一个订购饮料类型的程序。我正在尝试实现观察者模式,因此当下订单时,手机/观察者将显示订单,基本上让他们知道更新。
我只是不知道该怎么做。如果我在 update()order 类中传递 TheOrder 类..我会收到很多错误。太多了,无法发布.. update() 位于观察者和手机以及 TheOrder 类中..
这是我的观察者类:
#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
这是观察者具体类,
#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
这里是主题类,
#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
这里是主题具体类
#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
,这里是观察者具体类
#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()
<< 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;
}
我只是想将订单发送到 cellphone1 类中,以便我可以显示每个不同的订单..我想我需要通过更新功能发送它..
因为在 cellphone1 中是观察者,所以我想显示在那里订购..
在TheOrder类中有一个notifyObservers()函数我需要传递这个指针吗?还
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()
<< 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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
有两种常见的方法来实现观察者模式。第一种情况下,每个观察者都持有对可观察对象的引用。在您的情况下,您需要告诉每个 Cellphone 对象它正在等待哪个 Order 对象。如果你的每部手机最多只有一个订单,那么这不会太棘手。如果你的每部手机的订单数量超过了订单数量,那么你就必须在手机类中以某种方式进行管理。
第二种常见方法(以及我在您的情况下会做的事情)是在
update()
方法中将 Observable 传递给观察者。在您的情况下,您可以将Observer
及其所有子类中的update
签名更改为:然后您将可以访问手机
中的特定已完成订单更新
方法。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 ofupdate
inObserver
and all of its subclasses to:Then you would have access to the specific completed order in the cellphone's
update
method.需要解决的一件事:
One thing to fix: