Matlab intermediary model

Intermediary model (Mediator Pattern) is used to reduce the complexity of communications between a plurality of objects and classes. This mode provides an intermediate class, which generally handles the communication between the different classes and the loosely coupled, making the code easier to maintain. Imagine a mess of the development team working condition, although between team members work together, but it is difficult to unify the views, always directing each other, leading to work behind schedule. This time, we need an arbiter. All team members will report their cases to the arbitrator, the arbitrator will consider the team as a whole, and then issued a directive. Arbiter responsible for centralizing your views. In this way, the team's communication process becomes a form of team members, the arbitrator issued a directive to report to the members arbiter, rather than asking and communication between team members, this is the Mediator pattern.

The examples given herein https://blog.csdn.net/niunai112/article/details/79913833 achieve intermediary model with the Matlab language

Mediator.m (abstract Mediator class)

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

ChatPlatform.m (specific mediator class, ConcreteMediator)

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 (abstract class colleagues, Colleague)

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 (specific class colleagues, 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

Test code:

= ChatPlatform chatPlatform (); 
A = NormalUser ( "A", chatPlatform); 
B = NormalUser ( "B", chatPlatform); 
C = NormalUser ( "C", chatPlatform); 
a.join (); 
b.join () ; 
c.join (); 
DISP ( "group A sends a message ----------------- ------------------" ); 
a.sendToAll ( "A: we hear me?"); 
the DISP ( "A to B ----------------- ------- private message ----------- "); 
a.sendToPerson (" B "," A: B, I just want to say to you "); 
the DISP (" ----------- ------ B to A private message ------------------ "); 
b.sendToPerson (" A "," B: Yes, please say ") ; 
DISP ( "chat room leaving ----------------- A ------------------"); 
a.leave ( ); 
DISP ( "group B sends a message ----------------- ------------------"); 
B.sendToAll ( "B: A can hear it");

Reference material

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

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

 

Guess you like

Origin www.cnblogs.com/usaddew/p/10960584.html