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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started