要點:
1、服務端(subject)維護乙個觀察者的列表,以便能夠向所有的觀察者(observer)推送資訊
2、觀察者可以獲取服務端的狀態
3、服務端和觀察者可抽象,可以有多個不同實現
subject.m
classdef subject < handle
properties(access = private)
observers = observer.empty();
endmethods
function add(obj,observer)
obj.observers(end + 1) = observer;
endfunction rm(obj,observer)
index = [obj.observers] ~= observer;
obj.observers = obj.observers(index);
endfunction notify(obj)
for i= 1:length(obj.observers)
obj.observers(i).update(obj);
endend
endend
concretesubject.m
classdef concretesubject < subject
properties(access = public)
news
end
methods
function set.news(obj,news)
obj.news = news;
end
function news = get.news(obj)
news = obj.news;
endend
end
observer.m
classdef observer < handle & matlab.mixin.heterogeneous
methods(abstract)
update(~,~);
endmethods(sealed)
function res = eq(obj1,obj2)
res = eq@handle(obj1,obj2);
endfunction res = ne(obj1,obj2)
res = ne@handle(obj1,obj2);
endend
end
concreteobserver1.m
classdef concreteobserver1 < observer
properties
name
end
methods
function obj = concreteobserver1(name)
obj.name = name;
endfunction update(obj,subject)
disp([obj.name, ' has got subject news: ',subject.news]);
endend
end
concreteobserver2.m
classdef concreteobserver2 < observer
properties
name
endmethods
function obj = concreteobserver2(name)
obj.name = name;
endfunction update(obj,subject)
disp([obj.name, ' has received subject news: ',subject.news ]);
endend
end
測試**:
sub = concretesubject();
sub.news = 'hello matlab';
o1 = concreteobserver1('obs:a');
o2 = concreteobserver2('obs:b');
sub.add(o1);
sub.add(o2);
sub.notify();
sub.rm(o1);
sub.news = 'hello observer pattern';
sub.notify();
測試結果:
matlab自帶event事件使用的是發布-訂閱者模式,與觀察者模式類似(區別參考:**如下:
eventobserver.m
classdef eventsubscriber < handle
properties
name
endmethods
function obj = eventsubscriber(name)
obj.name = name;
end%可以是普通函式、靜態函式和成員函式
function dispmsg(obj,src,data)
disp("["+data.time+"]"+obj.name + ":"+data.msg+" from "+src.name);
endend
end
eventpublisher.m
classdef eventpublisher < handle
events
msgrecv
endproperties
name
endmethods
function obj = eventpublisher(name)
obj.name = name;
endfunction sendmsg(obj,msg)
obj.notify('msgrecv',eventmessage(msg));
endend
end
eventmessage.m
classdef eventmessage < event.eventdata
properties
msgtime
endmethods
function obj = eventmessage(msg)
obj.time = datestr(now);
obj.msg = msg;
endend
end
測試**:
pub = eventpublisher("pub1");
sub = eventsubscriber("sub1");
h = pub.addlistener('msgrecv',@sub.dispmsg);
pub.sendmsg("matlab event");
delete(h);
pub.sendmsg("matlab event");
執行結果: python觀察者模式 python 觀察者模式
python 觀察者模式 前言e 寫的倉促就不截uml類圖了,書本chapter10,p313能看到圖 一旦觀察的主題有更新,就會通知到觀察者們,下面的例子是最簡單的乙個觀察者範例,假設這是一群投機分子密切關注 軍 火 倉庫的產品與數量變動 class inventory def init self...
觀察者模式
觀察者模式 observer 完美的將觀察者和被觀察的物件分離開。舉個例子,使用者介面可以作為乙個觀察者,業務資料是被觀察者,使用者介面觀察業務資料的變化,發現資料變化後,就顯示在介面上。物件導向設計的乙個原則是 系統中的每個類將重點放在某乙個功能上,而不是其他方面。乙個物件只做一件事情,並且將他做...
觀察者模式
觀察者模式定義了一種一對多的依賴關係,讓多個觀察者物件同時監聽某乙個主題物件。這個主題物件在狀態上發生變化時,會通知所有觀察者物件,讓他們能夠自動更新自己 任何乙個模式都是離不開角色的,這裡也會有幾種角色 抽象主題角色 把所有對觀察者物件的引用儲存在乙個集合中,每個抽象主題角色都可以有任意數量的觀察...