设计模式之桥接模式:Swift 实现

桥接模式 Bridge

It depart one class or some closing class into two independent layer, the abstract and the implementation. The concrete class A contains an abstract interface B, and this interface can be implement in different ways. The A relies on the interface B not the concrete class. We inject different concrete class(has implemented interface B) for A or subclass of A, so we can get an instance of A or subclass with different functionality.

桥接模式将一个大类或者一系列紧密相关的类拆分为抽象实现两个独立的层次结构。实体类 A 包含有接口 B,这个接口可以通过不同的方式来实现。实体类 A 依赖于接口 B 而不是具体的类。 我们使用不同的实现了接口 B 的实体类注入到 A 类实体或者 A类的子类的实体中,从而可以得到具有不同功能的 A 实体 或者 A 子类实体。

桥接模式

// Abstract interface
protocol Device {
    func isEnable() -> Bool
    func enable()
    func disable()
    func getVolume() -> Int
    func setVolume(_ volumn: Int)
    func getChannel() -> Int
    func setChannel(_ channel: Int)
}

// Concrete class that contains the interface
class Remote {
    var device: Device
    init(_ device: Device) {
        self.device = device
    }
    func togglePower() {
        if device.isEnable() {
            device.disable()
        } else {
            device.enable()
        }
    }
    func volumeUp() {
        device.setVolume(device.getVolume() + 1)
    }
    
    func voluomeDown() {
        device.setVolume(device.getVolume() - 1)
    }
    func channelUp() {
        device.setChannel(device.getChannel() + 1)
    }
    
    func channelDown() {
        device.setChannel(device.getChannel() - 1)
    }
}

// Sub class
class AdvanceRemote: Remote {
    
    override init(_ device: Device) {
        super.init(device)
    }
    func mute() {
        device.setVolume(0)
    }
}

// Specific implementation of the interface
class TV: Device {
    private var displaySize = (1080, 960)
    private var on: Bool = false
    private var volume = 0
    private var channel = 0
    func isEnable() -> Bool {
        return on
    }

    func enable() {
        self.on = true
    }

    func disable() {
        self.on = false
    }

    func getVolume() -> Int {
        return volume
    }

    func setVolume(_ volumn: Int) {
        self.volume = volumn
    }

    func getChannel() -> Int {
        return channel
    }

    func setChannel(_ channel: Int) {
        self.channel = channel
    }
}

// Specific implementation of the interface
class Radio: Device {
    private var on: Bool = false
    private var volume = 0
    private var channel = 0
    func isEnable() -> Bool {
        return on
    }

    func enable() {
        on = true
    }

    func disable() {
        on = false
    }

    func getVolume() -> Int {
        return volume
    }

    func setVolume(_ volumn: Int) {
        self.volume = volumn
    }

    func getChannel() -> Int {
        return channel
    }

    func setChannel(_ channel: Int) {
        self.channel = channel
    }
}

let tv = TV()                     // create a TV
let remote = AdvanceRemote(tv)    // inject a concrete Device to the remote.
remote.togglePower()
remote.channelUp()
remote.volumeUp()
remote.mute()

let radio = Radio()
let remoteForRadio = Remote(radio)
remoteForRadio.channelUp()
remoteForRadio.togglePower()

一套 method1(), method2(), method3() 是一种实现,那么也可以换另外的实现,而不会影响上一部分的抽象的结构。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值