Matlab代理模式(Proxy)

编程入门 行业动态 更新时间:2024-10-10 09:15:38

Matlab代理<a href=https://www.elefans.com/category/jswz/34/1771241.html style=模式(Proxy)"/>

Matlab代理模式(Proxy)

 

代理模式(Proxy)就是给一个对象提供一个代理对象,并有代理对象来控制对原有对象的引用。代理模式和装饰模式非常类似,但最主要的区别是代理模式中,代理类对被代理的对象有控制权,决定其执行或者不执行。本文根据.html给出的结构图,使用Matlab语言实现代理代理模式。

Subject.m

classdef Subjectmethods(Abstract)request(~);end
end

RealSubject.m

classdef RealSubject < Subject    methodsfunction request(obj)mc = metaclass(obj);disp(mc.Name + ":request");endend 
end

Proxy.m

classdef Proxy < Subjectpropertiessubendmethodsfunction obj = Proxy(sub)obj.sub = sub;endfunction request(obj)if(randi([0,1]))obj.before();obj.sub.request();obj.after();elsemc = metaclass(obj);disp(mc.Name + ":no request");endendfunction before(obj)mc = metaclass(obj);disp(mc.Name + ":start request");endfunction after(obj)mc = metaclass(obj);disp(mc.Name + ":end request");endend
end

测试代码:

a = RealSubject();
a.request();b = Proxy(a);
b.request();

结果:

更多推荐

Matlab代理模式(Proxy)

本文发布于:2024-02-05 10:21:38,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1744644.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:模式   Matlab   Proxy

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!