Matlab中介者模式

中介者模式(Mediator Pattern)是用來下降多個對象和類之間的通訊複雜性。這種模式提供了一箇中介類,該類一般處理不一樣類之間的通訊,並支持鬆耦合,使代碼易於維護。想象一個亂糟糟的開發小組的工做狀態,組員之間雖然在一塊兒協同工做,可是意見難以統一,老是相互指揮,致使工做進度滯後。這時候,就須要一個仲裁者。全部的組員將本身的狀況彙報給仲裁者,仲裁者會從團隊的總體考慮,而後下達指令。仲裁者負責統一你們的意見。這樣,團隊的交流過程就變成了組員向仲裁者報告,仲裁者向組員下達指令的形式,而組員之間再也不詢問和溝通,這就是Mediator模式。html

本文根據https://blog.csdn.net/niunai112/article/details/79913833所給的實例,用Matlab語言實現中介者模式測試

Mediator.m (抽象的中介者類).net

classdef Mediator < handle 
    properties
        userlist = User.empty();
    end
    methods(Abstract)
        sendToAll(~,~);
        sendToPerson(~,~,~);
        join(~,~);
        leave(~,~);
    end
end

ChatPlatform.m (具體的中介者類,ConcreteMediator)orm

classdef ChatPlatform < Mediator
    methods
        function sendToAll(obj,msg)
           for i=1:length(obj.userlist)
               obj.userlist(i).accept(msg);
           end
        end
        function sendToPerson(obj,name,msg)
            user = obj.userlist([obj.userlist.name] == name);
            for i = 1:length(user)
                user.accept(msg);
            end
        end
        function join(obj,user)
            obj.userlist(end + 1) = user;
        end
        function leave(obj,user)
            obj.userlist = obj.userlist(obj.userlist ~= user);
        end
    end
end

User.m (抽象的同事類, Colleague)htm

classdef User < handle & matlab.mixin.Heterogeneous
    properties
        mediator
        name
    end
    
    methods
        function obj = User(name, mediator)
            obj.name = name;
            obj.mediator = mediator;
        end
        function sendToAll(obj,msg)
            obj.mediator.sendToAll(msg);
        end
        function sendToPerson(obj,name,msg)
            obj.mediator.sendToPerson(name,msg);
        end
        function join(obj)
            obj.mediator.join(obj);
        end
        function leave(obj)
            obj.mediator.leave(obj);
        end
    end
    methods(Abstract)    
        accept(~,~);
    end
end

NormalUser.m (具體的同事類, ConcreteColleague)對象

classdef NormalUser < User
    methods
        function obj = NormalUser(name, mediator)
            obj = obj@User(name, mediator);
        end      
        function accept(obj,msg)
            disp("[" + datestr(now) + "]" + obj.name + " accepted msg: " + msg);
        end
    end
end

測試代碼:blog

chatPlatform = ChatPlatform();
a = NormalUser("A", chatPlatform);
b = NormalUser("B", chatPlatform);
c = NormalUser("C", chatPlatform);
a.join();
b.join();
c.join();
disp("-----------------A羣發送消息------------------");
a.sendToAll("A:你們聽獲得嗎?");
disp("-----------------A給B私發消息------------------");
a.sendToPerson("B", "A:B,我只想和你說");
disp("-----------------B給A私發消息------------------");
b.sendToPerson("A","B:能夠,請說" );
disp("-----------------A離開聊天室------------------");
a.leave();
disp("-----------------B羣發送消息------------------");
b.sendToAll("B:A能聽到嗎");

參考資料開發

https://www.runoob.com/design-pattern/mediator-pattern.html
https://blog.csdn.net/qq_39384184/article/details/80631275io

https://blog.csdn.net/niunai112/article/details/79913833function

相關文章
相關標籤/搜索