2016-09-28 51 views
5

我想知道什么是最佳实践,当我需要一些函数公开和一些内部使用协议时。

我正在写一个AudioManagerSwift 3包装AVPlayer作为一个框架。

我想要一些方法是公开的,例如使用AudioManager的ViewController可以访问某些方法,但某些方法不会暴露在框架
- >即访问修饰符internal而不是public之外。

我正在用协议驱动设计编写框架,几乎每个部分都应该有一个协议。
因此协议正在与框架内的协议交谈。
例如主类 - AudioManager - 具有AudioPlayer,并且应该能够调用其上的一些internal函数,例如
pause(reason:)但该方法应该是internal而不是暴露在框架之外。

这里是一个例子。具有内部函数和属性的Swift公共协议

internal enum PauseReason { 
    case byUser 
    case routeChange 
} 

// Compilation error: `Public protocol cannot refine an internal protocol` 
public protocol AudioPlayerProtocol: InternalAudioPlayerProtocol { 
    func pause() // I want 
} 

internal protocol InternalAudioPlayerProtocol { 
    func pause(reason: PauseReason) // Should only be accessible within the framework 
} 

public class AudioPlayer: AudioPlayerProtocol { 
    public func pause() { 
     pause(reason: .byUser) 
    } 

    // This would probably not compile because it is inside a public class... 
    internal func pause(reason: PauseReason) { //I want this to be internal 
     // save reason and to stuff with it later on 
    } 
} 

public protocol AudioManagerProtocol { 
    var audioPlayer: AudioPlayerProtocol { get } 
} 

public class AudioManager: AudioManagerProtocol { 
    public let audioPlayer: AudioPlayerProtocol 

    init() { 
     audioPlayer = AudioPlayer() 
     NotificationCenter.default.addObserver(self, selector: #selector(handleRouteChange(_:)), name: NSNotification.Name.AVAudioSessionRouteChange, object: nil) 
    } 

    func handleRouteChange(_ notification: Notification) { 
     guard 
     let userInfo = notification.userInfo, 
     let reasonRaw = userInfo[AVAudioSessionRouteChangeReasonKey] as? NSNumber, 
     let reason = AVAudioSessionRouteChangeReason(rawValue: reasonRaw.uintValue) 
     else { print("what could not get route change") } 
     switch reason { 
     case .oldDeviceUnavailable: 
      pauseBecauseOfRouteChange() 
     default: 
      break 
     } 
    } 
} 

private extension AudioManager { 
    func pauseBecauseOfRouteChange() { 
     audioPlayer.pause(reason: .routeChange) 
    } 
} 

// Outside of Audio framework 
class PlayerViewController: UIViewController { 
    fileprivate let audioManager: AudioManagerProtocol 
    @IBAction didPressPauseButton(_ sender: UIButton) { 
     // I want the `user of the Audio framwwork` (in this case a ViewController) 
     // to only be able to `see` `pause()` and not `pause(reason:)` 
     audioManager.audioPlayer.pause() 
    } 
} 

我知道我可以得到它通过改变方法pauseBecauseOfRouteChange工作看起来像这样:

func pauseBecauseOfRouteChange() { 
    guard let internalPlayer = audioPlayer as? InternalAudioPlayerProtocol else { return } 
    internalPlayer.pause(reason: .routeChange) 
} 

但我想知道如果有一个更好的解决方案?
类似于标记AudioPlayerProtocol细化了InternalAudioPlayerProtocol ...

或者你们是如何做到这一点的?
如果该框架不公开用于内部使用的方法和变量,则该框架更美观!

谢谢!

回答

0

没有,有没有更好的解决方案这一点,考虑到协议时,这里至少是为什么:

设想一个场景,使用你的框架有人想要写的AudioPlayerProtocol的延伸,又如何pause(reason:)方法可以实现,如果它是内部的?

你可以只继承和这段代码实际上将编译实现它:

public class AudioPlayer: AudioPlayerProtocol { 
    public func pause() { 
     pause(reason: .byUser) 
    } 

    internal func pause(reason: PauseReason) { 
    } 
} 

有了协议,这是不是这样的,因为你根本,如果有人用公共访问级别要不能保证实施内部功能使用您的混合公共/内部协议。

0

如果将协议拆分为内部和公共,然后让公共实现类委托给内部实现,那么该怎么办?像这样

internal protocol InternalAudioPlayerProtocol { 
    func pause(reason: PauseReason) 
} 

public protocol AudioPlayerProtocol { 
    func pause() 
} 

internal class InternalAudioPlayer: InternalAudioPlayerProtocol { 
    internal func pause(reason: PauseReason) { 
    } 
} 

public class AudioPlayer: AudioPlayerProtocol { 
    internal var base: InternalAudioPlayerProtocol 

    internal init(base: InternalAudioPlayerProtocol) { 
     self.base = base 
    } 

    public func pause() { 
     base.pause(reason: .byUser) 
    } 
} 

public protocol AudioManagerProtocol { 
    var audioPlayer: AudioPlayerProtocol { get } 
} 

public class AudioManager: AudioManagerProtocol { 
    internal let base = InternalAudioPlayer() 
    public let audioPlayer: AudioPlayerProtocol 

    public init() { 
     audioPlayer = AudioPlayer(base: base) 
    } 

    internal func handleSomeNotification() {    
     pauseBecauseOfRouteChange() //amongst other things 
    } 

    internal func pauseBecauseOfRouteChange() { 
     base.pause(reason: .routeChange) 
    } 
}