当前位置: 移动技术网 > IT编程>软件设计>设计模式 > Matlab适配器模式

Matlab适配器模式

2019年05月27日  | 移动技术网IT编程  | 我要评论

适配器模式是连接两个不兼容接口的桥梁,主要分为三种:类适配器、对象适配器以及接口适配器,本文根据所给的例子使用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())

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网