经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » Java相关 » 设计模式 » 查看文章
Matlab桥接模式 - sw-lab
来源:cnblogs  作者:sw-lab  时间:2019/6/14 9:41:28  对本文有异议

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

桥接模式的UML图如下:

??

?Implementor.m 

  1. classdef Implementor < handle
  2. methods(Abstract)
  3. operation(~);
  4. end
  5. end

ConcreateImplementorA.m

  1. classdef ConcreateImplementorA < Implementor
  2. methods
  3. function operation(~)
  4. disp("this is concreteImplementorA's operation...");
  5. end
  6. end
  7. end

ConcreateImplementorB.m

  1. classdef ConcreateImplementorB < Implementor
  2. methods
  3. function operation(~)
  4. disp("this is concreteImplementorA's operation...");
  5. end
  6. end
  7. end

Abstraction.m

  1. classdef Abstraction < handle
  2. properties
  3. implementor
  4. end
  5. methods
  6. function imp = getImplementor(obj)
  7. imp = obj.implementor;
  8. end
  9. function setImplementor(obj,imp)
  10. obj.implementor = imp;
  11. end
  12. function operation(obj)
  13. obj.implementor.operation();
  14. end
  15. end
  16. end

RefinedAbstraction.m

  1. classdef RefinedAbstraction < Abstraction
  2. methods
  3. function operation(obj)
  4. disp("this is RefinedAbstraction...")
  5. operation@Abstraction(obj);
  6. end
  7. end
  8. end

测试代码:

  1. abstraction = RefinedAbstraction();
  2. abstraction.setImplementor(ConcreateImplementorA());
  3. abstraction.operation();
  4. abstraction.setImplementor(ConcreateImplementorB());
  5. abstraction.operation();

参考资料:

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

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

原文链接:http://www.cnblogs.com/usaddew/p/11020625.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号