#include<iostream>
#include<string.h>
using namespace std;
class Observer;//观察者的 虚基类
class Subject //被观察者的 虚基类
{
public:
virtual void attach(Observer *o)=0;
virtual void remove(Observer *o)=0;
virtual void change()=0;
virtual void setTemperature(int degree)=0;
virtual int getTemperature()=0;
virtual char* getLocal()=0;
};
class Observer
{
public:
virtual void update(Subject *s)=0;
};
class Satellite:public Observer
{
private:
char name[20];
public:
Satellite(char *temp){
strcpy(name,temp);
}
void update(Subject *s){
cout<<"I am:"<<name<<endl;
cout<<s->getLocal()<<" temperature is "<<s->getTemperature()<<endl;
}
};
class Weather:public Subject
{
private:
char local[20];
int temperature;
Observer* list[10];
int count;
public:
Weather(char *temp_local,int degree){
strcpy(local,temp_local);
temperature=degree;
count=0;
}
char* getLocal(){
return local;
}
void attach(Observer *o){
list[count++]=o;
}
void remove(Observer *o){
for(int i=0;i<count;i++){
if(list[i]==o){
for(int j=i;j<count-1;j++){
list[j]=list[j+1];
}
count--;
break;
}
}
}
void change(){
Observer *temp;
for(int i=0;i<count;i++){
temp=list[i];
temp->update(this);
}
}
void setTemperature(int degree){
temperature=degree;
change();
}
int getTemperature(){
return temperature;
}
};
int main(){
Weather tody("shenzhen",10);//观察者对象
Satellite *s1 = new Satellite("first");
Satellite *s2 = new Satellite("second");
Satellite *s3 = new Satellite("third");
tody.attach(s1);//注册三个观察者
tody.attach(s2);
tody.attach(s3);
cout<<tody.getLocal()<<" "<<tody.getTemperature()<<endl;
tody.setTemperature(5);
cout<<endl;
tody.remove(s1);//删除掉观察者
tody.setTemperature(20);
return 0;
}
*************下面是之前的程序,差别主要是在list,错误原因可能是“数组指针和指针数组”,请后续关注****************
#include<iostream>
using namespace std;
#include<string.h>
class Observer;
class Subject
{
public:
virtual void attach(Observer *o)=0;
virtual void remove(Observer *o)=0;
virtual void change()=0;
virtual void setTemperature(int degree)=0;
virtual int getTemperature()=0;
virtual char* getLocal()=0;
};
class Observer
{
public:
virtual void update(Subject *s)=0;
};
class Satellite:public Observer
{
private:
char name[20];
public:
Satellite(char *temp){
strcpy(name,temp);
}
void update(Subject *s){
cout<<"I am:"<<name<<endl;
cout<<s->getLocal()<<" temperature is"<<s->getTemperature()<<endl;
}
};
class Weather:public Subject
{
private:
char local[20];
int temperature;
Observer **list;
int count;
public:
Weather(char *temp_local,int degree){
strcpy(local,temp_local);
temperature=degree;
list=new Observer*[10];
count=0;
}
char* getLocal(){
return local;
}
void attach(Observer *o){
list[count++]=o;
}
void remove(Observer *o){
for(int i=0;i<count;i++){
if(list[i]==o){
for(int j=i;j<count-1;j++){
list[j]=list[j+1];
}
count--;
break;
}
}
}
void change(){
Observer *temp;
for(int i=0;i<count;i++){
temp=list[i];
temp->update(this);
}
}
void setTemperature(int degree){
temperature=degree;
}
int getTemperature(){
return temperature;
}
};
int main(){
Weather tody("shenzhen",10);
Satellite *s1 = new Satellite("first");
Satellite *s2 = new Satellite("second");
Satellite *s3 = new Satellite("third");
tody.attach(s1);
tody.attach(s2);
tody.attach(s3);
cout<<tody.getLocal()<<" "<<tody.getTemperature()<<endl;
tody.setTemperature(5);
return 0;
}