I am a novice in swift protocol and try to use it by considering Solid principles. I have taken two protocols (A, B) and combined them with the third protocol (D). Idea is to choose between two protocol from another class based on need. Check the code to understand more.
protocol A {
func fetchA()
}
protocol B {
func fetchB()
}
protocol D : A,B { }
extension D {
func fetchB() { }
func fetchA() {}
}
class B1 : D {
func fetchB() {
print("B")
}
}
class A1 : D {
func fetchA() {
print("A")
}
}
protocol C {
func fetchC()
}
class C1 : C {
func fetchC() {
print("C")
}
}
enum Ab {
case a
case b
}
struct Hello {
let first : D
let second : C
init(first : D , second :C) {
self.first = first
self.second = second
}
func show(type:Ab){
switch type {
case .a:
first.fetchA()
case .b:
first.fetchB()
}
second.fetchC()
}
}
let obj = Hello.init(first: A1(), second: C1())
obj.show(type:.a)
So current code print "A". Now if I can change first parameter to B1() and type .b and it prints "B". I want to improve the code base and remove the enum type and want to get the same result with help of protocol. What changes need to be done here.? Thanks in advance.
Concrete Goal : I have NetworkManger(Class A1), FirebaseManger(Class B1), and LocalDatabaseManger(Class C1). I want to do a network call either with NetworkManger or FirebaseManger and if it fails call LocalDatabaseManger.
A1even conform toB(as a result of conforming toD) in the first place? It feels like it shouldn't.