2015-12-08 61 views
1

我正在嘗試編寫一個通用中介基類(Mediator),它將對等類註冊到實例化中定義的特定協議(在對等類擴展上)。你如何使用多個類型參數 - Swift 2?

調解員和同伴的子類不應該彼此瞭解。當所有人聯繫起來時,他們的關係就被定義了這是爲了使每個組件都是模塊化的。

如果我只使用一種類型的參數,例如:

class Mediator<T, U> { 
    private var peers = [T]() 
    func registerPeer(peer: T) { 
     self.peers.append(peer) 
    } 
} 

然後一個對等體正確地登記。

我希望T或U都能夠被附加到同位體[]上。

我找的,只有改變Mediator的同行[]和registerPeer()允許混合或T或U

// Mediator 
class Mediator<T, U> { 

// I want this to be an Array that can be T OR U 
private var peers = Array<T|U>() 

// I want peer: to be either T OR U 
func registerPeer(peer: <T|U>) { 

    self.peers.append(peer) 
    } 
} 

class RootModuleMediator<T, U> : Mediator<T, U> {} 

protocol RootModuleMediatorsIOInterface { func someFunction() } 
extension RootModuleMediator : RootModuleMediatorsIOInterface { func someFunction() { print("RootModuleMediator someFunction() printed")}} 
protocol RootModuleMediatorsViewInterface { func aFunction() } 
extension RootModuleMediator : RootModuleMediatorsViewInterface { func aFunction() { print("RootModuleMediator aFunction() printed")}} 

// Peer 
class Peer<T> { 

    private var mediator : T? 
    func registerMediator(mediator: T) { self.mediator = mediator } 

} 

// View Peer 
class RootModuleView<T> : Peer<T> {} 
protocol RootModuleViewsMediatorInterface { func someFunction() } 
extension RootModuleView : RootModuleViewsMediatorInterface { func someFunction() { print("RootModuleView someFunction() printed") }} 

// IO Peer 
class RootModuleIO<T> : Peer<T> {} 
protocol RootModuleIOsMediatorInterface { func someFunction() } 
extension RootModuleIO : RootModuleIOsMediatorInterface { func someFunction() { print("RootModuleIO someFunction() printed") }} 

// Wiring components together 
let rootModuleIO = RootModuleIO<RootModuleMediatorsIOInterface>() 
let rootModuleView = RootModuleView<RootModuleMediatorsViewInterface>() 

let rootModuleMediator = RootModuleMediator<RootModuleIOsMediatorInterface, RootModuleViewsMediatorInterface>() 

rootModuleIO.registerMediator(rootModuleMediator) 
rootModuleView.registerMediator(rootModuleMediator) 

rootModuleMediator.registerPeer(rootModuleIO) 
rootModuleMediator.registerPeer(rootModuleView) 

// I want the following function to print "RootModuleIO someFunction() printed" 
rootModuleMediator.peers[0].someFunction() 

回答

1

我沒有看到實施的其餘部分,以驗證它都解決方案按預期工作,但你的意思是這樣:

class Mediator<T, U> { 
    private var peers = [(T, U)]() 

    func registerPeer(peer: (T, U)) { 
     self.peers.append(peer) 
    } 

    func registerPeer(left: T, _ right: U) { 
     registerPeer((left, right)) 
    } 

} 

缺少的步驟是使用tuples作爲數組元素。

0

有可能具有符合多種協議的通用類型。它是這樣做的:

class Mediator<T where T: RootModuleMediatorsIOInterface, T: RootModuleIOsMediatorInterface> { 

    private var peers = Array<T>() 

    func registerPeer(peer: T) { 

     self.peers.append(peer) 
    } 

    func exercisePeers() { 
     for peer in peers { 
      peer.someFunction() 
      peer.someOtherFunction() 
     } 
    } 
} 

class RootModuleMediator : RootModuleMediatorsIOInterface, RootModuleIOsMediatorInterface {} 


// ############ EXAMPLE PROTOCOL CONSTRAINED EXTENSIONS IMPLEMENTATION ############ 


protocol RootModuleMediatorsIOInterface { func someFunction() } 

extension RootModuleMediatorsIOInterface { func someFunction() { print("Something")}} 

protocol RootModuleIOsMediatorInterface { func someOtherFunction() } 

extension RootModuleIOsMediatorInterface { func someOtherFunction() { print("Something else") }} 

let root = RootModuleMediator() 
let mediator = Mediator<RootModuleMediator>() 

mediator.registerPeer(root) 
mediator.exercisePeers() 

我添加了一個練習函數,所以你可以看到你可以調用協議擴展實現的函數。我也簡化了你的協議和擴展定義。輸出如你期望的那樣:

Something 
Something else 
+0

謝謝,不是我在尋找解決方案。因爲我希望Mediator對RootModuleMediatorsIOInterface或RootModuleMediatorsViewInterface沒有直接的瞭解。我更新了我的問題並添加了更多詳細信息。 –

0

最後,我無法獲得T或U型參數與數組或函數一起工作。而是選擇了一種更適合我鬆散耦合組件並使其通過定義的接口進行通信的用例的解決方案。

// Mediator base 
class Mediator<IOTypeParam, ViewTypeParam> { 

    private var IO : IOTypeParam? 
    private var view : ViewTypeParam? 

    func registerIOPeer(peer: IOTypeParam) { self.IO = peer } 
    func registerViewPeer(peer: ViewTypeParam) { self.view = peer } 

} 

// Mediator subclass 
class RootModuleMediator<IOTypeParam, ViewTypeParam> : Mediator<IOTypeParam, ViewTypeParam> {} 

protocol RootModuleMediatorsIOInterface { func someFunction() } 
extension RootModuleMediator : RootModuleMediatorsIOInterface { func someFunction() { print("RootModuleMediator someFunction() printed")}} 
protocol RootModuleMediatorsViewInterface { func aFunction() } 
extension RootModuleMediator : RootModuleMediatorsViewInterface { func aFunction() { print("RootModuleMediator aFunction() printed")}} 

// Peer base 
class Peer<MediatorTypeParam> { 

    private var mediator : MediatorTypeParam? 
    func registerMediator(mediator: MediatorTypeParam) { self.mediator = mediator } 

} 

// View Peer 
class RootModuleView<MediatorTypeParam> : Peer<MediatorTypeParam> {} 
protocol RootModuleViewsMediatorInterface { func someFunction() } 
extension RootModuleView : RootModuleViewsMediatorInterface { func someFunction() { print("RootModuleView someFunction() printed") }} 

// IO Peer 
class RootModuleIO<MediatorTypeParam> : Peer<MediatorTypeParam> {} 
protocol RootModuleIOsMediatorInterface { func someFunction() } 
extension RootModuleIO : RootModuleIOsMediatorInterface { func someFunction() { print("RootModuleIO someFunction() printed") }} 

// Instances 
let rootModuleIO = RootModuleIO<RootModuleMediatorsIOInterface>() 
let rootModuleView = RootModuleView<RootModuleMediatorsViewInterface>() 
let rootModuleMediator = RootModuleMediator<RootModuleIOsMediatorInterface, RootModuleViewsMediatorInterface>() 

// Interface registration 
rootModuleIO.registerMediator(rootModuleMediator) 
rootModuleView.registerMediator(rootModuleMediator) 
rootModuleMediator.registerIOPeer(rootModuleIO) 
rootModuleMediator.registerViewPeer(rootModuleView) 

// Communication constrained to defined interfaces 
rootModuleMediator.IO!.someFunction() 
rootModuleMediator.view!.someFunction() 
rootModuleIO.mediator!.someFunction() 
rootModuleView.mediator!.aFunction() 
相關問題