当前位置:   article > 正文

23设计模式之桥接模式(Bridge)

23设计模式之桥接模式(Bridge)


一、概述

        Bridge属于结构型模式中的一种,将抽象部分与它的实现部分分离,使它们都可以独立地变化。

二、适用性

        1. 你不希望在抽象和它的实现部分之间有一个固定的绑定关系。例如这种情况可能是因为,在程序运行时刻实现部分应可以被选择或者切换。
        2. 类的抽象以及它的实现都应该可以通过生成子类的方法加以扩充。这时Bridge模式使你可以对不同的抽象接口和实现部分进行组合,并分别对它们进行扩充。
        3. 对一个抽象的实现部分的修改应对客户不产生影响,即客户的代码不必重新编译。
        4. 正如在意图一节的第一个类图中所示的那样,有许多类要生成。这样一种类层次结构说明你必须将一个对象分解成两个部分。
        5. 你想在多个对象间共享实现(可能使用引用计数),但同时要求客户并不知道这一点。

三、参与者

        1.Abstraction:定义抽象类的接口。维护一个指向Implementor类型对象的指针。
        2.RefinedAbstraction:扩充由Abstraction定义的接口。
        3.Implementor:定义实现类的接口,该接口不一定要与Abstraction的接口完全一致。事实上这两个接口可以完全不同。一般来讲,Implementor接口仅提供基本操作,而Abstraction则定义了基于这些基本操作的较高层次的操作。
        4.ConcreteImplementor:实现Implementor接口并定义它的具体实现。

四、类图 



五、代码实现 

1. Abstraction:定义抽象类的接口。维护一个指向Implementor类型对象的指针。

PersonBr.swift

  1. //
  2. // PersonBr.swift
  3. // 23设计模式
  4. //
  5. // Created by 阳君 on 14/12/2.
  6. // Copyright (c) 2014年 六月. All rights reserved.
  7. //
  8. import Foundation
  9. /// PersonBr定义抽象类的接口。
  10. class PersonBr {
  11. /// 服装
  12. var clothing:Clothing?
  13. /// 标示
  14. var type:String = ""
  15. init() {
  16. }
  17. /** 桥接的动作*/
  18. func dress() {
  19. }
  20. }

2. RefinedAbstraction:扩充由Abstraction定义的接口。

a) Lady.swift

  1. //
  2. // Lady.swift
  3. // 23设计模式
  4. //
  5. // Created by 阳君 on 14/12/2.
  6. // Copyright (c) 2014年 六月. All rights reserved.
  7. //
  8. import Foundation
  9. /// LadyBr扩充由PersonBr定义的接口
  10. class LadyBr: PersonBr {
  11. override init() {
  12. super.init()
  13. super.type = "女人"
  14. }
  15. override func dress() {
  16. super.clothing?.personDressCloth(self)
  17. }
  18. }


b)ManBr.swift

  1. //
  2. // Man.swift
  3. // 23设计模式
  4. //
  5. // Created by 阳君 on 14/12/2.
  6. // Copyright (c) 2014年 六月. All rights reserved.
  7. //
  8. import Foundation
  9. /// ManBr扩充由PersonBr定义的接口
  10. class ManBr: PersonBr {
  11. override init() {
  12. super.init()
  13. super.type = "男人"
  14. }
  15. override func dress() {
  16. super.clothing?.personDressCloth(self)
  17. }
  18. }

3. Implementor:定义实现类的接口,该接口不一定要与Abstraction的接口完全一致。

Clothing.swift

  1. //
  2. // Clothing.swift
  3. // 23设计模式
  4. //
  5. // Created by 阳君 on 14/12/2.
  6. // Copyright (c) 2014年 六月. All rights reserved.
  7. //
  8. import Foundation
  9. /// Clothing定义实现类的接口,该接口不一定要与PersonBr的接口完全一致
  10. protocol Clothing {
  11. func personDressCloth(personBr:PersonBr)
  12. }

4. ConcreteImplementor:实现Implementor接口并定义它的具体实现。

a) Jacket.swift

  1. //
  2. // Jacket.swift
  3. // 23设计模式
  4. //
  5. // Created by 阳君 on 14/12/2.
  6. // Copyright (c) 2014年 六月. All rights reserved.
  7. //
  8. import Foundation
  9. /// Clothes实现Clothing接口并定义它的具体实现
  10. class Clothes: Clothing {
  11. func personDressCloth(personBr: PersonBr) {
  12. print("\(personBr.type)穿衣服");
  13. }
  14. }


b) Trouser.swift

  1. //
  2. // Trouser.swift
  3. // 23设计模式
  4. //
  5. // Created by 阳君 on 14/12/2.
  6. // Copyright (c) 2014年 六月. All rights reserved.
  7. //
  8. import Foundation
  9. /// Trouser实现Clothing接口并定义它的具体实现。
  10. class Trouser: Clothing {
  11. func personDressCloth(personBr: PersonBr) {
  12. print("\(personBr.type)穿裤子");
  13. }
  14. }

六、测试

1. 代码

  1. // 桥接模式
  2. let man:PersonBr = ManBr()
  3. let lady:PersonBr = LadyBr()
  4. let clothes:Clothing = Clothes()
  5. let trouser:Clothing = Trouser()
  6. clothes.personDressCloth(man)
  7. trouser.personDressCloth(man)
  8. clothes.personDressCloth(lady)
  9. trouser.personDressCloth(lady)

2. 运行结果

男人穿衣服

男人穿裤子

女人穿衣服

女人穿裤子


声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Cpp五条/article/detail/710420
推荐阅读
相关标签
  

闽ICP备14008679号