Matlab桥接模式

时间:2019-06-14
本文章向大家介绍Matlab桥接模式,主要包括Matlab桥接模式使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

桥接模式(Bridge)是一种结构型设计模式。它是用组合关系代替继承关系来实现,可以处理多维度变化的场景(https://blog.csdn.net/qq_31156277/article/details/80659537)。它的主要特点是把抽象(Abstraction)与行为实现(Implementation)分离开来,从而可以保持各部分的独立性以及应对他们的功能扩展。

桥接模式的UML图如下:

​Implementor.m 

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.m

classdef ConcreateImplementorB < Implementor
    methods
        function operation(~)
            disp("this is concreteImplementorA's operation...");
        end
    end
end

Abstraction.m

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

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();

参考资料:

https://www.cnblogs.com/lixiuyu/p/5923160.html

https://blog.csdn.net/qq_31156277/article/details/80659537

原文地址:https://www.cnblogs.com/usaddew/p/11020625.html