桥接模式(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