Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3876670
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T22:24:32+00:00 2026-05-19T22:24:32+00:00

I have a order beverage type program. I am tryig to implement observer pattern

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T22:24:32+00:00Added an answer on May 19, 2026 at 10:24 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What is the best practice to implement the following: we have a classes Order
I have overridden the windows ListBox in order to display an image and a
I have a small program to order and sort email messages, outputting to a
let's say i have an order and order details. the view will contains the
Using LINQ to SQL, I have an Order class with a collection of OrderDetails.
I have a 'Purchase Order' class. It contains information about a single purchase order.
After user places an order I have to send detailed email message containing order
I have a large database of normalized order data that is becoming very slow
In order to debug an asp.net web app I have to have IE Script
I have created an HttpCookie in order to share data across a subdomain :

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.