java和c++观察者模式实现

观察者模式是一种比较常用的设计模式,,采用接口,封装类中动态变化的方法,定义对象间的依赖关系,一边但一个对象状态发生改变时,所有以来他的对象都发生改变。
简单的说,就是一管多,即关键就是观察者和被观察者,学习这一部分看其他博客这样解释,就是多个屌丝追一个白富美的模式,多个屌丝就是所谓的观察者,白富美就是被观察者,白富美将追他的屌丝存到她的准男友表中,要是白富美生气,屌丝们都被通知到,然后想方设法讨好白富美,让其开心,要是白富美不喜欢那个屌丝了,就将其从准男友表中去掉。就大概这个意思。我对其理解是来源于java的解释,看c++老师讲课想了长时间没想清楚,最后在网上看了java的实现,在理解的基础上,用java实现了,让人比较舒服!然后尝试用c++来实现,c++真生硬,但是很强的语言!!!。
现在看代码吧,不扯淡了!!!
c++实现

#include<iostream>
#include<string>
#include<list>
#include<stdio.h>
using namespace std;
//定义观察者行为接口
class watcherInter{
public:
    void WatcherInter(){}
    virtual ~watcherInter(){}
    virtual void update(string str){}
};
//接口实现
class watcher:watcherInter{
public:
    ~watcher(){}
    void update(string str){
        //通知计数
        static int i = 0 ;
        cout<<"men  No."<<i<<"   "<<str<<endl ;
        i++ ;
    }
};
//被观察者接口,在开发中可以进行添加更多的需求
class watchedInter{
public:
    virtual ~watchedInter(){
    }
    virtual void add(){}
    virtual void remove(){}
    virtual void nidify(string str){}
};
//被观察者接口方法实现
class watched:watchedInter{

private:
	//观察者链表
    list<watcher*>ls ;
    string str;
public:
    watched():str(NULL){}
    watched(string strs){
        str =strs;
    }
    ~watched(){}
    //将观察者加入到链表中
    void add(watcher *ob){
        ls.push_back(ob);
    }
	//更新通知信息	
    void update(string strs){
        str = strs;
    }
    //移除指定的观察者
    void remove(watcher*ob){
        ls.remove(ob);
    }
//将信息通知给每个观察者	
    void nodify(){
        list<watcher*>::iterator iter = ls.begin();  
        for(iter;iter!=ls.end();iter++)
           (*iter)->update(str);
    }
};
int main(){
//在main函数中只需创建一个被观察者,多个观察者
    watched *ww = new watched("hello,sir");
    watcher *w1 = new watcher();
    watcher *w2 = new watcher();
    watcher *w3 = new watcher();
    ww->add(w1);
    ww->add(w2);
    ww->add(w3);
    ww->nodify();
    cout<<"-----------------------------------------"<<endl;
    ww->update("you are so handsome!!!");
    ww->nodify();
    delete ww;
    delete w1;
    delete w2;
    delete w3;

}

运行截图:
在这里插入图片描述
java实现


import java.util.ArrayList;
import java.util.List;

interface WatcherInter{
	
	public void update(String str);
	
}
class Watcher implements WatcherInter{
		
	
	public void update(String str) {
		System.out.println(str);
		}
}

interface WatchedInter{
	
	public void addWatch(Watcher ww);
	public void removeWatch(Watcher ww);
	public void nodifyWatch(String str);
}

class Watched implements WatchedInter{
	
	private List<Watcher> ll = new ArrayList<Watcher>();
	public void addWatch(Watcher ww) {
		ll.add(ww);
	}
	public void removeWatch(Watcher ww) {
		ll.remove(ww);
	}
	
	public void nodifyWatch(String str) {
		int i = 1 ; 
		for(Watcher s : ll ) {
			
			s.update("Watch NO "+i+"   "+str);
			i++ ;
		}
	}
}
public class Observer {

	public static void main(String []args) {
		
		Watched wed = new Watched();
		Watcher wh = new Watcher();
		Watcher wh1 = new Watcher();
		Watcher wh2 = new Watcher();
		wed.addWatch(wh);
		wed.addWatch(wh1);
		wed.addWatch(wh2);
		wed.nodifyWatch("你好!!!");
		wed.nodifyWatch("hello!");
	}
}

在这里插入图片描述
观察者实现了面向接口编程,而不是面向实现编程,使得程序可复用性更高,在实际软件开发中,实现了程序可扩展,在不改变程序架构和功能的情况下,提高了代码的耦合性,在需求不断变化的情况下只需在程序中作微小的改动。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值