需要有关观察者c ++更新的帮助

时间:2011-01-24 02:34:15

标签: c++ design-patterns

我有一个订单饮料类型计划。我尝试实现观察者模式,所以当下订单时,手机/观察者会显示订单,基本上让他们知道更新..

我只是不知道该怎么做。如果我在update()命令类中传递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;
}

我只想将订单发送到mobile1类,这样我就可以显示每个不同的订单..我想我需要通过更新功能发送它..

因为在mobile1中是观察者所以我想在那里显示订单..

在TheOrder类中有一个notifyObservers()函数我需要传递这个指针吗?还

2 个答案:

答案 0 :(得分:0)

实现Observer模式有两种常用方法。在第一个中,每个Observer都持有对Observable的引用。在您的情况下,您需要告诉每个Cellphone对象它正在等待哪些Order对象。如果你每个手机最多只有一个订单,这不会太棘手。如果你的每部手机不仅可以订购,那么你必须在手机课上以某种方式管理它。

第二种常见的方式(以及我在你的情况下会做的)是将{Observable}传递给update()方法中的Observer。在您的情况下,您可以将update及其所有子类中的Observer签名更改为:

void update(TheOrder *order);

然后,您可以通过手机的update方法访问特定的已完成订单。

答案 1 :(得分:0)

要解决的一件事:

protected: virtual ~Observer() = 0 { };