Matlab适配器模式

适配器模式是连接两个不兼容接口的桥梁,主要分为三种:类适配器、对象适配器以及接口适配器,本文根据https://blog.csdn.net/u012359453/article/details/79165080所给的例子使用matlab语言对三种适配器进行实现。

已有的接口和类(AC220V):

IAC220V.m

classdef IAC220V < handle
   methods(Abstract)
       getAC220V(~);
   end
end

AC220V.m

classdef AC220V < IAC220V
    properties
        isAC = true;
        voltage = 220;
    end
    methods
        function obj = AC220V(voltage,isAC)
            obj.isAC = isAC;
            obj.voltage = voltage;
        end        
        function [voltage,isAC] = getAC220V(obj)
            voltage = obj.voltage;
            isAC = obj.isAC;
        end
    end
end

目标接口:(DC5V,注意两者的方法签名是不同的)

classdef IDC5V < handle
    methods(Abstract)
       getDC5V(~);
   end
end

类适配器(将AC220V转化成DC5V):

classdef ClassAdapter < AC220V & IDC5V
    methods
        function obj = ClassAdapter(voltage,isAC)
            obj = obj@AC220V(voltage,isAC);
        end       
        function [new_voltage,new_isAC] = getDC5V(obj)
           [voltage,isAC] = obj.getAC220V();
           new_voltage = 0;
           new_isAC = false;
           if(isAC)
               new_voltage = voltage / 44;
               new_isAC = false;
           end
        end
    end
end

对象适配器:

classdef ObjAdapter < IDC5V
    properties
        pAC220
    end  
    methods
        function obj = ObjAdapter(pAC220)
            if(metaclass(pAC220) <= ?IAC220V)
                obj.pAC220 = pAC220;
            end
        end       
        function [new_voltage,new_isAC] = getDC5V(obj)
           new_voltage = 0;
           new_isAC = false;
           if(~isempty(obj.pAC220))
               [voltage,isAC] = obj.pAC220.getAC220V();
               if(isAC)
                   new_voltage = voltage / 44;
                   new_isAC = false;
               end
           end
        end
    end
end

接口适配器:

IDCOutput.m (定义通用输出接口)

classdef IDCOutput < handle
    methods(Abstract)
        getDC5V(~);
        getDC12V(~);
    end
end

IAdapter.m(定义默认适配器接口)

classdef IAdapter < IDCOutput
    properties
        power
    end
    methods
        function obj = IAdapter(power)
            obj.power = power;
        end        
        function [voltage,isAC] = getDC5V(~)
            voltage = 0;
            isAC = false;
        end
        function [voltage,isAC] = getDC12V(~)
            voltage = 0;
            isAC = false;
        end
    end
end

AC220VAdapter.m (定义具体适配器方法,AC220V输入为例)

classdef AC220VAdapter < IAdapter
    methods
        function obj = AC220VAdapter(pAC220V)
            obj = obj@IAdapter(pAC220V);
        end      
        function [new_voltage,new_isAC] = getDC5V(obj)
           new_voltage = 0;
           new_isAC = false;
           if(~isempty(obj.power))
               [voltage,isAC] = obj.power.getAC220V();
               if(isAC)
                   new_voltage = voltage / 44;
                   new_isAC = false;
               end
           end
        end
    end
end

测试代码

a = ClassAdapter(220,true);
disp(a.getDC5V());
 
b = ObjAdapter(AC220V(223,true));
disp(b.getDC5V());
 
c = AC220VAdapter(AC220V(221,true));
disp(c.getDC5V())
原文地址:https://www.cnblogs.com/usaddew/p/10927950.html