Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

c++ Purpose Demonstrate the ability to create a program that utilizes the Observ

ID: 3779738 • Letter: C

Question

c++

Purpose Demonstrate the ability to create a program that utilizes the Observer design pattern. Demonstrate the ability to create abstract classes and implement derived classes. Demonstrate the ability to create and iterate over an STL list that contains callback functions. Assignment You will be creating a program that implements the Observer design pattern This design pattern is utilized in almost all GUI systems and is the basis for distributed event handling. The goal of the program is to create a class the Subject class for this assignment) that has a private variable (address) that can be modified via a standard mutator function (setAddress). This additional member functions that allow other classes (the observers to register and deregister themselves with the Subject. If observers are registered with the subject, they will receive notifications (via a callback function) ifthe subject's address ever changes. You are to create these observer classes BankObserver, SchoolObserver, CreditObserver. Each of the observers must be derived from this abstract base class: class Abstractobserver public virtual void subjectChanged (string 0; virtual ~Abstractobserver()th Each should override the subjectChanged method by printing the string argument to the screen along with the name of it's class. For instance, the Bankobserver might print the following: The BankObserver received an address change notification:

Explanation / Answer

#include<iostream>
#include<list>
#include<string.h>

using namespace std;

class AbstractObserver
{
public:
virtual void subjectChanged(string) = 0;
virtual ~AbstractObserver(){}
};

class BankObserver : public AbstractObserver
{
public:
void subjectChanged(string str)
{
cout<<"The Bank Observer received notification "<<str<<endl;
}
};

class SchoolObserver : public AbstractObserver
{
public:
void subjectChanged(string str)
{
cout<<"The School Observer received notification "<<str<<endl;
}
};

class CreditObserver : public AbstractObserver
{
public:
void subjectChanged(string str)
{
cout<<"The Credit Observer received notification "<<str<<endl;
}
};

class Subject
{
string address;
list<AbstractObserver *> observers;
void notify();
public:
Subject(string adr);
void addObserver(AbstractObserver& observer);
void removeObserver(AbstractObserver& observer);
string getAddress();
void setAddress(string newAddress);
};

Subject::Subject(string adr)
{
address = adr;
}

void Subject::addObserver(AbstractObserver& observer)
{
observers.push_back(&observer);
}

void Subject::removeObserver(AbstractObserver& observer)
{
observers.remove(&observer);
}

string Subject::getAddress()
{
return address;
}

void Subject::notify()
{

}

void Subject::setAddress(string newAddress)
{
address = newAddress;
  
list<AbstractObserver*>::iterator it;
  
for(it = observers.begin();it != observers.end();it++)
(*it)->subjectChanged(newAddress);
}


int main()
{
AbstractObserver *ob1, *ob2, *ob3;
ob1 = new BankObserver();
ob2 = new SchoolObserver();
ob3 = new CreditObserver();
  
Subject *s = new Subject("hello");
s->addObserver(*ob1);
s->addObserver(*ob2);
s->addObserver(*ob3);
s->setAddress("Oberver");
s->removeObserver(*ob1);
s->setAddress("Observer removed");
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote