橋接模式(Bridge)是一種結構型設計模式。它是用組合關係代替繼承關係來實現,能夠處理多維度變化的場景(http://www.javashuo.com/article/p-esidbcmx-gv.html)。它的主要特色是把抽象(Abstraction)與行爲實現(Implementation)分離開來,從而能夠保持各部分的獨立性以及應對他們的功能擴展。html
橋接模式的UML圖以下:設計模式
app
classdef Implementor < handle methods(Abstract) operation(~); end end
ConcreateImplementorA.m測試
classdef ConcreateImplementorA < Implementor methods function operation(~) disp("this is concreteImplementorA's operation..."); end end end
ConcreateImplementorB.mthis
classdef ConcreateImplementorB < Implementor methods function operation(~) disp("this is concreteImplementorA's operation..."); end end end
Abstraction.mspa
classdef Abstraction < handle properties implementor end methods function imp = getImplementor(obj) imp = obj.implementor; end function setImplementor(obj,imp) obj.implementor = imp; end function operation(obj) obj.implementor.operation(); end end end
RefinedAbstraction.m.net
classdef RefinedAbstraction < Abstraction methods function operation(obj) disp("this is RefinedAbstraction...") operation@Abstraction(obj); end end end
測試代碼:設計
abstraction = RefinedAbstraction(); abstraction.setImplementor(ConcreateImplementorA()); abstraction.operation(); abstraction.setImplementor(ConcreateImplementorB()); abstraction.operation();
參考資料:htm