Question: The following C++ program implements an observer pattern to enable residents of a house, apartment or recreational vehicle to monitor the state of a bedroom

The following C++ program implements an observer pattern to enable residents of a house, apartment or recreational vehicle to monitor the state of a bedroom window (the state of a window is open or closed). You may assume the program has all the necessary includes and statements necessary to compile it and execute it without errors.

Use the program to answer the following sub-questions.

#include

#include

#include

#include

class IObserver {

public:

virtual void Update(bool state) = 0;

};

class Resident : IObserver { //Concrete Observer Class

private:

std::string name_; // Resident name

public:

Resident(std::string name) { this->name_ = name; }

void Update(bool state);

};

void Resident::Update(bool state) { //Implementation

if (state)

std::cout

else

std::cout

}

//-----------------------------------------------------------------------

class ASubject { // declaration / definiton

private:

std::vector list; // list of pointers to Resident observers*>

public:

void Attach(Resident* occupant);

void Detach(Resident* occupant);

void Notify(bool state);

};

void ASubject::Attach(Resident* occupant) { // implementation

list.push_back(occupant);

}

void ASubject::Detach(Resident* occupant) { // implemenatation

list.erase(std::remove(list.begin(), list.end(), occupant), list.end());

}

void ASubject::Notify(bool state) { // implementation

for (Resident* resident : list){

if (resident != NULL)

resident ->Update(state);

}

}

class Window : public ASubject { //Concrete Subject - Window

private:

bool state_;

public:

Window(bool initialstate) { state_ = initialstate; }

void ChangeState(bool state);

};

void Window::ChangeState(bool state) { // implementation

state_ = state;

Notify(state);

}

// create and exercise subject and observers

int main(int argc, char* argv[]) {

Window bedroomWindow(false); // Subject - bedroom window is closed

Resident Owner(\"Sally\"); // Observer, the owner who is a resident

Resident Dog(\"Bro\"); //Observer, dog who wants to stick nose out window

return 0;

} //end main

Assume a new subject class named Door, which has methods that are declared/ defined and function similarly to the methods in the Window class specified in the program above has been correctly declared and defined and added to the Ch+ code above. For items 1, 2, 3, 4, and 5 below, write correct Ch+statements that could be added to the main function in the program above to accomplish the following behaviors: 1. Create a door subject identified by the name backdoor, whose initial state is closed 2. Create a resident observer named Brooke 3. Brooke should observe the subject backdoor 4. Bro should observe the backdoor 5. Close the backdoor

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!