I am Swift newcomer from C#. I started designing the Swift program from the abstract level - declaring entities signatures with relationships between them. I caught that protocol in swift is analog of the interface in C# or Java. From my (as Swift newcomer from C#) point of view, generics in protocols implemented in Swift by an uncommon way. I researched about associated types but continuously didn't know how to do the following:
typealias Observer<T> = (T) -> ()
protocol Subscription {
func cancel()
}
protocol Observable<T> {
func subscribe(observer: Observer<T>) -> Subscription
}
protocol A {}
protocol B {}
protocol C {}
protocol AbcObservable {
var aAdded : Observable<A> { get }
var bAdded : Observable<B> { get }
var cAdded : Observable<C> { get }
}
The code above is from the imaginary world where generic protocols have the same struct as generic classes. Is it possible to write the code above compilable by associated types using? How? If it is impossible, what is an alternative?
Remark 1
I need described architecture to provide something like the following:
class First: AbcObservable { ... }
class Second: AbcObservable { ... }
class Client {
let lightDependency: AbcObservable
init(lightDependency: AbcObservable) {
self.lightDependency = lightDependency
}
}
class IoCContainer1 {
let client: Client
init() {
let abc: AbcObservable = First()
client = Client(abc)
}
}
class IoCContainer2 {
let client: Client
init() {
let abc: AbcObservable = Second()
client = Client(abc)
}
}
class Mock: AbcObservable { ... }
class Test {
func test() {
let abcMock = Mock()
let client = Client(abcMock)
...
}
}
This moment is very painfully for me; I can't understand how I can provide D of SOLID in my code in the case when I need generics.
Remark 2
Observable will not have many implementations in the real situation and it is stupid to do it as abstract. I wanted to write the essence of my problem, and I used Observable in the example to simplify. In a real situation, I had DataManager instead of Observable, that has many implementations.
Remark 3
The question was changed from "How to use protocol with associatedtype in another protocol?" to "What is equivalent for C# or Java generic interface in Swift?"