设计模式--观察者模式(C++)

本文介绍了一种使用C++实现的观察者模式,该模式允许一个主题对象管理多个观察者对象,并在状态发生变化时通知所有观察者。通过具体的Weather类和Satellite类演示了如何注册、更新和移除观察者。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值