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 6770353
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T15:18:08+00:00 2026-05-26T15:18:08+00:00

I had a hard time understand exactly what an observer pattern was, but I

  • 0

I had a hard time understand exactly what an observer pattern was, but I produced the following code for my project. It uses SDL. I am using the boost library to implement signals and therefore implementing my observer pattern. Does this look correct?

/* This is setting up our signal for sending observations */
boost::signals2::signal<void (char, int, int)> sig;

/* Subjects the Observer will connect with */
sig.connect(&setChest);
sig.connect(&setNonTraverse);
sig.connect(&setEntry);
sig.connect(&setExit);

std::cout << "Waiting for user-interaction. Press on the 'X' to quit" << std::endl;

while ( !quit ) {               
    status = SDL_WaitEvent(&event);   //wait for an event to occur
    switch (event.type) {           //check the event type
        case SDL_KEYDOWN:           //Check if a key was pressed.     
        key = SDL_GetKeyName(event.key.keysym.sym);
        break;
        case SDL_MOUSEBUTTONUP:
        sig(key[0],event.button.x/32,event.button.y/32);
        break;
        case SDL_QUIT:          // Click on the 'X' to close the window.
        exit ( 1 );
        break;
    }
  } //while
  return true;
}
  • 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-26T15:18:08+00:00Added an answer on May 26, 2026 at 3:18 pm

    Your posted code is that of the Observer.

    In the Observer pattern, the observer doesn’t react directly to the subjects’ state changes. Instead, the subject informs the observer of any changes by invoking the observer’s callback. This is why the observer must register with the subject, instead of merely polling (checking the state in a while loop) the subject.

    I’m not too familiar with C++, but here is some Java-like pseudocode that outlines the basic idea:

    class Observer{
        public Observer(Subject subject){
            subject.register(this);
        }
        public void updateFromSubject(Subject subject){
            //respond to change
        }
    }
    
    class Subject{
        List<Observer> observers;
        public void register(Observer observer){
            observers.add(observer);
        }
        private void notifyObservers(){
            for(Observer obs : observers){
                obs.updateFromSubject(this);
            }
        }
        public void changeStateToNewState(Object o){
            .... //business logic
            notifyObservers();
    }
    

    Notice the lack of a while loop, which means that the observer simply doesn’t do any work until an actual event occurs, instead of checking a flag a million times a second just to see if it changed.

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

Sidebar

Related Questions

This might be a simple question, but I've had a hard time pinning down
I have this simple problem but I had a hard time of finding a
I've had a hard time trying to find good examples of how to manage
I've had a hard time sitting in front of UML and getting value out
I have a malformed page to scrape, and have had a hard time getting
If this was previously talked about, I'm sorry, I had a hard time searching
I recently had a hard drive crashed and lost all of my source code.
I like to study languages outside my comfort zone, but I've had a hard
I had hard time to figure out why I've been getting unknown action error
I had a hard time framing that question. I hope it gets the gist

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.