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

posted on 2019-06-14 01:30  sw-lab  阅读(295)  评论(0编辑  收藏  举报

导航