Matlab介面卡模式 sw lab

2022-09-23 23:21:11 字數 2903 閱讀 6020

matlab介面卡模式

介面卡模式是連線兩個不相容介面的橋梁,主要分為三種:類介面卡、物件介面卡以及介面介面卡,本文根據所給的例子使用matlab語言對三種介面卡進行實現。

已有的介面和類(ac220v):

iac220v.m

classdef iac220v < handle

methods(abstract)

getac220v(~);

endendac220v.m

classdef ac220v < iac220v

properties

isac = true;

voltage = 220;

endmethods

function obj = ac220v(voltage,isac)

obj.isac = isac;

obj.voltage = voltage;

end

function [voltage,isac] = getac220v(obj)

voltage = obj.voltage;

isac = obj.isac;

endend

end目標介面:(dc5v,注意兩者的方法簽名是不同的)

classdef idc5v < handle

methods(abstract)

getdc5v(~);

endend類介面卡(將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;

endend

endend物件介面卡:

classdef objadapter < idc5v

properties

pac220

end

methods

function obj = objadapter(pac220)

if(metaclass(pac220) <= ?iac220v)

obj.pac220 = pac220;

endend

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;

endend

endend

end介面介面卡:

idcoutput.m (定義通用輸出介面)

classdef idcoutput < handle

methods(abstract)

getdc5v(~);

getdc12v(~);

endendiadapter.m(定義預設介面卡介面)

classdef iadapter < idcoutput

properties

power

endmethods

function obj = iadapter(power)

obj.power = power;

end

function [voltage,isac] = getdc5v(~)

voltage = 0;

isac = false;

endfunction [voltage,isac] = getdc12v(~)

voltage = 0;

isac = false;

endend

endac220vadapter.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;

endend

endend

end測試**

a = classadapter(220,true);

disp(a.getdc5v());

b = objadapter(ac220v(223,true));

disp(b.getdc5v());

c = ac220vadapter(ac220v(221,true));

disp(c.getdc5v())

Matlab介面卡模式

介面卡模式是連線兩個不相容介面的橋梁,主要分為三種 類介面卡 物件介面卡以及介面介面卡,本文根據所給的例子使用matlab語言對三種介面卡進行實現。已有的介面和類 ac220v iac220v.m classdef iac220v handle methods abstract getac220v ...

介面卡模式(類介面卡 物件介面卡)

做個筆記 引用 public inte ce usb public inte ce psp public class usber implements usb 類介面卡 psp適用usb介面 public class usbadapter extends usber implements psp 物...

介面卡模式 預設介面卡,類介面卡,物件介面卡

模式思想 改變乙個類的對外介面 增加或減少 以滿足不同外部呼叫者的需求 角色成員 目標介面 target 客戶所期待的介面。目標可以是具體的或抽象的類,也可以是介面。需要適配的類 adaptee 需要適配的類或適配者類。介面卡 adapter 通過包裝乙個需要適配的物件,把原介面轉換成目標介面。適配...